mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add users management to site settings (#126)
This commit is contained in:
committed by
GitHub
parent
bc15140512
commit
37fb99a868
60
app/javascript/actions/User/requestUsers.ts
Normal file
60
app/javascript/actions/User/requestUsers.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
|
||||
import IUserJSON from '../../interfaces/json/IUser';
|
||||
|
||||
import { State } from '../../reducers/rootReducer';
|
||||
|
||||
export const USERS_REQUEST_START = 'USERS_REQUEST_START';
|
||||
interface UsersRequestStartAction {
|
||||
type: typeof USERS_REQUEST_START;
|
||||
}
|
||||
|
||||
export const USERS_REQUEST_SUCCESS = 'USERS_REQUEST_SUCCESS';
|
||||
interface UsersRequestSuccessAction {
|
||||
type: typeof USERS_REQUEST_SUCCESS;
|
||||
users: Array<IUserJSON>;
|
||||
}
|
||||
|
||||
export const USERS_REQUEST_FAILURE = 'USERS_REQUEST_FAILURE';
|
||||
interface UsersRequestFailureAction {
|
||||
type: typeof USERS_REQUEST_FAILURE;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type UsersRequestActionTypes =
|
||||
UsersRequestStartAction |
|
||||
UsersRequestSuccessAction |
|
||||
UsersRequestFailureAction;
|
||||
|
||||
|
||||
const usersRequestStart = (): UsersRequestActionTypes => ({
|
||||
type: USERS_REQUEST_START,
|
||||
});
|
||||
|
||||
const usersRequestSuccess = (
|
||||
users: Array<IUserJSON>
|
||||
): UsersRequestActionTypes => ({
|
||||
type: USERS_REQUEST_SUCCESS,
|
||||
users,
|
||||
});
|
||||
|
||||
const usersRequestFailure = (error: string): UsersRequestActionTypes => ({
|
||||
type: USERS_REQUEST_FAILURE,
|
||||
error,
|
||||
});
|
||||
|
||||
export const requestUsers = (): ThunkAction<void, State, null, Action<string>> => (
|
||||
async (dispatch) => {
|
||||
dispatch(usersRequestStart());
|
||||
|
||||
try {
|
||||
const response = await fetch('/users');
|
||||
const json = await response.json();
|
||||
|
||||
dispatch(usersRequestSuccess(json));
|
||||
} catch (e) {
|
||||
dispatch(usersRequestFailure(e));
|
||||
}
|
||||
}
|
||||
)
|
||||
87
app/javascript/actions/User/updateUser.ts
Normal file
87
app/javascript/actions/User/updateUser.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { Action } from "redux";
|
||||
import { ThunkAction } from "redux-thunk";
|
||||
|
||||
import HttpStatus from "../../constants/http_status";
|
||||
import buildRequestHeaders from "../../helpers/buildRequestHeaders";
|
||||
import IUserJSON from "../../interfaces/json/IUser";
|
||||
import { State } from "../../reducers/rootReducer";
|
||||
|
||||
export const USER_UPDATE_START = 'USER_UPDATE_START';
|
||||
interface UserUpdateStartAction {
|
||||
type: typeof USER_UPDATE_START;
|
||||
}
|
||||
|
||||
export const USER_UPDATE_SUCCESS = 'USER_UPDATE_SUCCESS';
|
||||
interface UserUpdateSuccessAction {
|
||||
type: typeof USER_UPDATE_SUCCESS;
|
||||
user: IUserJSON;
|
||||
}
|
||||
|
||||
export const USER_UPDATE_FAILURE = 'USER_UPDATE_FAILURE';
|
||||
interface UserUpdateFailureAction {
|
||||
type: typeof USER_UPDATE_FAILURE;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type UserUpdateActionTypes =
|
||||
UserUpdateStartAction |
|
||||
UserUpdateSuccessAction |
|
||||
UserUpdateFailureAction;
|
||||
|
||||
const userUpdateStart = (): UserUpdateStartAction => ({
|
||||
type: USER_UPDATE_START,
|
||||
});
|
||||
|
||||
const userUpdateSuccess = (
|
||||
userJSON: IUserJSON,
|
||||
): UserUpdateSuccessAction => ({
|
||||
type: USER_UPDATE_SUCCESS,
|
||||
user: userJSON,
|
||||
});
|
||||
|
||||
const userUpdateFailure = (error: string): UserUpdateFailureAction => ({
|
||||
type: USER_UPDATE_FAILURE,
|
||||
error,
|
||||
});
|
||||
|
||||
interface UpdateUserParams {
|
||||
id: number;
|
||||
role?: string;
|
||||
status?: string;
|
||||
authenticityToken: string;
|
||||
}
|
||||
|
||||
export const updateUser = ({
|
||||
id,
|
||||
role = null,
|
||||
status = null,
|
||||
authenticityToken,
|
||||
}: UpdateUserParams): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
dispatch(userUpdateStart());
|
||||
|
||||
const user = Object.assign({},
|
||||
role !== null ? { role } : null,
|
||||
status !== null ? { status } : null,
|
||||
);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/users/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: buildRequestHeaders(authenticityToken),
|
||||
body: JSON.stringify({ user }),
|
||||
});
|
||||
const json = await res.json();
|
||||
|
||||
if (res.status === HttpStatus.OK) {
|
||||
dispatch(userUpdateSuccess(json));
|
||||
} else {
|
||||
dispatch(userUpdateFailure(json.error));
|
||||
}
|
||||
|
||||
return Promise.resolve(res);
|
||||
} catch (e) {
|
||||
dispatch(userUpdateFailure(e));
|
||||
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user