2021-01-05 16:17:44 +00:00
|
|
|
import http, { FractalResponseData, getPaginationSet, PaginatedResult } from '@/api/http';
|
2021-07-19 20:34:10 +00:00
|
|
|
import { useContext } from 'react';
|
2021-01-04 18:50:43 +00:00
|
|
|
import useSWR from 'swr';
|
2021-07-19 20:37:09 +00:00
|
|
|
import { createContext } from '@/api/admin';
|
2021-07-25 21:51:39 +00:00
|
|
|
import { rawDataToDatabase } from '@/api/admin/databases/getDatabases';
|
|
|
|
import { Role } from '@/api/admin/roles/getRoles';
|
2021-01-04 18:50:43 +00:00
|
|
|
|
|
|
|
export interface User {
|
|
|
|
id: number;
|
|
|
|
externalId: string;
|
|
|
|
uuid: string;
|
|
|
|
username: string;
|
|
|
|
email: string;
|
|
|
|
language: string;
|
2021-07-25 21:51:39 +00:00
|
|
|
adminRoleId: number | null;
|
2021-01-04 18:50:43 +00:00
|
|
|
rootAdmin: boolean;
|
|
|
|
tfa: boolean;
|
2021-01-07 16:55:04 +00:00
|
|
|
avatarURL: string;
|
|
|
|
roleName: string | null;
|
2021-01-04 18:50:43 +00:00
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
2021-07-25 21:51:39 +00:00
|
|
|
|
|
|
|
relationships: {
|
|
|
|
role: Role | undefined;
|
|
|
|
};
|
2021-01-04 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:17:44 +00:00
|
|
|
export const rawDataToUser = ({ attributes }: FractalResponseData): User => ({
|
|
|
|
id: attributes.id,
|
|
|
|
externalId: attributes.external_id,
|
|
|
|
uuid: attributes.uuid,
|
|
|
|
username: attributes.username,
|
|
|
|
email: attributes.email,
|
|
|
|
language: attributes.language,
|
2021-07-25 21:51:39 +00:00
|
|
|
adminRoleId: attributes.admin_role_id,
|
2021-01-05 16:17:44 +00:00
|
|
|
rootAdmin: attributes.root_admin,
|
|
|
|
tfa: attributes['2fa'],
|
2021-01-07 16:55:04 +00:00
|
|
|
avatarURL: attributes.avatar_url,
|
2021-01-05 16:17:44 +00:00
|
|
|
roleName: attributes.role_name,
|
|
|
|
createdAt: new Date(attributes.created_at),
|
|
|
|
updatedAt: new Date(attributes.updated_at),
|
2021-07-25 21:51:39 +00:00
|
|
|
|
|
|
|
relationships: {
|
|
|
|
role: attributes.relationships?.role !== undefined && attributes.relationships?.role.object !== 'null_resource' ? rawDataToDatabase(attributes.relationships.role as FractalResponseData) : undefined,
|
|
|
|
},
|
2021-01-05 16:17:44 +00:00
|
|
|
});
|
|
|
|
|
2021-07-14 22:43:59 +00:00
|
|
|
export interface Filters {
|
|
|
|
id?: string;
|
|
|
|
uuid?: string;
|
|
|
|
username?: string;
|
|
|
|
email?: string;
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:34:10 +00:00
|
|
|
export const Context = createContext<Filters>();
|
2021-01-04 18:50:43 +00:00
|
|
|
|
2021-01-15 16:41:15 +00:00
|
|
|
export default (include: string[] = []) => {
|
2021-07-14 22:43:59 +00:00
|
|
|
const { page, filters, sort, sortDirection } = useContext(Context);
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
if (filters !== null) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
|
|
|
// @ts-ignore
|
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort !== null) {
|
|
|
|
// @ts-ignore
|
|
|
|
params.sort = (sortDirection ? '-' : '') + sort;
|
|
|
|
}
|
2021-01-04 18:50:43 +00:00
|
|
|
|
2021-07-14 22:43:59 +00:00
|
|
|
return useSWR<PaginatedResult<User>>([ 'users', page, filters, sort, sortDirection ], async () => {
|
|
|
|
const { data } = await http.get('/api/application/users', { params: { include: include.join(','), page, ...params } });
|
2021-01-04 18:50:43 +00:00
|
|
|
|
|
|
|
return ({
|
|
|
|
items: (data.data || []).map(rawDataToUser),
|
|
|
|
pagination: getPaginationSet(data.meta.pagination),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|