1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-23 01:16:27 +02:00
unleash.unleash/frontend/src/hooks/useAdminUsersApi.ts
Fredrik Strand Oseberg f0d6e45361 Feat/bootstrap (#281)
* feat: add bootstrap endpoint redux integration

* fix: remove useEffect from app

* feat: add path provider

* feat: browser router

* fix: delete path formatter

* fix: return absolute path if no basepath

* fix: format seenURI

* feat: get bootstrap uri from html

* fix: remove unused imports

* fix: remove initial loading call

* fix: wrap logout in formatApiPath

* feat: import logo

* feat: remove accessor from receiveConfig

* fix: update tests

* fix: update asset paths

* fix: remove data from app

* fix: revert moving access provider

* fix: remove build watch

* fix: remove console logs

* fix: update asset paths

* fix: remove path logic from base64

* fix: remove unused import

* set uiconfig

* change notification text

* fix: match uiConfig with expected format

* feat: add proclamation

* fix: move proclamation

* fix: remove unused imports

* fix: add target _blank

* fix: allow optional toast

* fix: return empty string if default value is present

* fix: set basepath to empty string if it matches default
2021-05-04 09:59:42 +02:00

179 lines
4.7 KiB
TypeScript

import { useState } from 'react';
import {
BAD_REQUEST,
FORBIDDEN,
NOT_FOUND,
OK,
UNAUTHORIZED,
} from '../constants/statusCodes';
import { IUserPayload } from '../interfaces/user';
import {
AuthenticationError,
ForbiddenError,
headers,
NotFoundError,
} from '../store/api-helper';
import { formatApiPath } from '../utils/format-path';
export interface IUserApiErrors {
addUser?: string;
removeUser?: string;
updateUser?: string;
changePassword?: string;
validatePassword?: string;
}
export const ADD_USER_ERROR = 'addUser';
export const UPDATE_USER_ERROR = 'updateUser';
export const REMOVE_USER_ERROR = 'removeUser';
export const CHANGE_PASSWORD_ERROR = 'changePassword';
export const VALIDATE_PASSWORD_ERROR = 'validatePassword';
const useAdminUsersApi = () => {
const [userApiErrors, setUserApiErrors] = useState({});
const [userLoading, setUserLoading] = useState(false);
const defaultOptions: RequestInit = {
headers,
credentials: 'include',
};
const makeRequest = async (
apiCaller: any,
type: string
): Promise<Response> => {
setUserLoading(true);
try {
const res = await apiCaller();
setUserLoading(false);
if (res.status > 299) {
await handleResponses(res, type);
}
if (res.status === OK) {
setUserApiErrors({});
}
return res;
} catch (e) {
setUserLoading(false);
throw e;
}
};
const addUser = async (user: IUserPayload) => {
return makeRequest(() => {
const path = formatApiPath('api/admin/user-admin');
return fetch(path, {
...defaultOptions,
method: 'POST',
body: JSON.stringify(user),
});
}, 'addUser');
};
const removeUser = async (user: IUserPayload) => {
return makeRequest(() => {
const path = formatApiPath(`api/admin/user-admin/${user.id}`);
return fetch(path, {
...defaultOptions,
method: 'DELETE',
});
}, 'removeUser');
};
const updateUser = async (user: IUserPayload) => {
return makeRequest(() => {
const path = formatApiPath(`api/admin/user-admin/${user.id}`);
return fetch(path, {
...defaultOptions,
method: 'PUT',
body: JSON.stringify(user),
});
}, 'updateUser');
};
const changePassword = async (user: IUserPayload, password: string) => {
return makeRequest(() => {
const path = formatApiPath(
`api/admin/user-admin/${user.id}/change-password`
);
return fetch(path, {
...defaultOptions,
method: 'POST',
body: JSON.stringify({ password }),
});
}, 'changePassword');
};
const validatePassword = async (password: string) => {
return makeRequest(() => {
const path = formatApiPath(
`api/admin/user-admin/validate-password`
);
return fetch(path, {
...defaultOptions,
method: 'POST',
body: JSON.stringify({ password }),
});
}, 'validatePassword');
};
const handleResponses = async (res: Response, type: string) => {
if (res.status === BAD_REQUEST) {
const data = await res.json();
setUserApiErrors(prev => ({
...prev,
[type]: data[0].msg,
}));
throw new Error();
}
if (res.status === NOT_FOUND) {
setUserApiErrors(prev => ({
...prev,
[type]: 'Could not find the requested resource.',
}));
throw new NotFoundError(res.status);
}
if (res.status === UNAUTHORIZED) {
const data = await res.json();
setUserApiErrors(prev => ({
...prev,
[type]: data[0].msg,
}));
throw new AuthenticationError(res.status);
}
if (res.status === FORBIDDEN) {
const data = await res.json();
setUserApiErrors(prev => ({
...prev,
[type]: data[0].msg,
}));
throw new ForbiddenError(res.status);
}
};
return {
addUser,
updateUser,
removeUser,
changePassword,
validatePassword,
userApiErrors,
userLoading,
};
};
export default useAdminUsersApi;