2022-12-16 02:06:14 +00:00
|
|
|
import http, { getPaginationSet, PaginatedResult } from '@/api/http';
|
|
|
|
import { Transformers, UserRole } from '@definitions/admin';
|
|
|
|
import { useContext } from 'react';
|
|
|
|
import useSWR from 'swr';
|
|
|
|
import { createContext } from '@/api/admin/index';
|
|
|
|
|
|
|
|
export interface Filters {
|
|
|
|
id?: string;
|
|
|
|
name?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContext<Filters>();
|
|
|
|
|
|
|
|
const createRole = (name: string, description: string | null, include: string[] = []): Promise<UserRole> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-01-12 19:31:47 +00:00
|
|
|
http.post(
|
|
|
|
'/api/application/roles',
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
},
|
|
|
|
{ params: { include: include.join(',') } },
|
|
|
|
)
|
2022-12-16 02:06:14 +00:00
|
|
|
.then(({ data }) => resolve(Transformers.toUserRole(data)))
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteRole = (id: number): Promise<void> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.delete(`/api/application/roles/${id}`)
|
|
|
|
.then(() => resolve())
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getRole = (id: number, include: string[] = []): Promise<UserRole> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.get(`/api/application/roles/${id}`, { params: { include: include.join(',') } })
|
|
|
|
.then(({ data }) => resolve(Transformers.toUserRole(data)))
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const searchRoles = (filters?: { name?: string }): Promise<UserRole[]> => {
|
|
|
|
const params = {};
|
|
|
|
if (filters !== undefined) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
2023-01-12 19:45:07 +00:00
|
|
|
// @ts-expect-error todo
|
2022-12-16 02:06:14 +00:00
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.get('/api/application/roles', { params })
|
2023-01-12 19:31:47 +00:00
|
|
|
.then(response => resolve((response.data.data || []).map(Transformers.toUserRole)))
|
2022-12-16 02:06:14 +00:00
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
const updateRole = (
|
|
|
|
id: number,
|
|
|
|
name: string,
|
|
|
|
description: string | null,
|
|
|
|
include: string[] = [],
|
|
|
|
): Promise<UserRole> => {
|
2022-12-16 02:06:14 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-01-12 19:31:47 +00:00
|
|
|
http.patch(
|
|
|
|
`/api/application/roles/${id}`,
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
},
|
|
|
|
{ params: { include: include.join(',') } },
|
|
|
|
)
|
2022-12-16 02:06:14 +00:00
|
|
|
.then(({ data }) => resolve(Transformers.toUserRole(data)))
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getRoles = (include: string[] = []) => {
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const { page, filters, sort, sortDirection } = useContext(Context);
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
if (filters !== null) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
2023-01-12 19:45:07 +00:00
|
|
|
// @ts-expect-error todo
|
2022-12-16 02:06:14 +00:00
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort !== null) {
|
2023-01-12 19:45:07 +00:00
|
|
|
// @ts-expect-error todo
|
2022-12-16 02:06:14 +00:00
|
|
|
params.sort = (sortDirection ? '-' : '') + sort;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
2023-01-12 19:31:47 +00:00
|
|
|
return useSWR<PaginatedResult<UserRole>>(['roles', page, filters, sort, sortDirection], async () => {
|
|
|
|
const { data } = await http.get('/api/application/roles', {
|
|
|
|
params: { include: include.join(','), page, ...params },
|
|
|
|
});
|
2022-12-16 02:06:14 +00:00
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
return {
|
2022-12-16 02:06:14 +00:00
|
|
|
items: (data.data || []).map(Transformers.toUserRole),
|
|
|
|
pagination: getPaginationSet(data.meta.pagination),
|
2023-01-12 19:31:47 +00:00
|
|
|
};
|
2022-12-16 02:06:14 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
export { createRole, deleteRole, getRole, searchRoles, updateRole, getRoles };
|