2022-02-27 21:45:23 +00:00
|
|
|
import React, { Fragment, useCallback, useEffect, useState } from 'react';
|
2022-02-27 21:15:11 +00:00
|
|
|
import http, {
|
|
|
|
FractalPaginatedResponse,
|
|
|
|
PaginatedResult,
|
|
|
|
PaginationDataSet,
|
|
|
|
QueryBuilderParams,
|
|
|
|
toPaginatedSet,
|
|
|
|
withQueryBuilderParams,
|
|
|
|
} from '@/api/http';
|
2022-02-27 18:32:22 +00:00
|
|
|
import { UUID } from '@/api/definitions';
|
2022-02-27 16:40:51 +00:00
|
|
|
import { Transformers, User } from '@definitions/admin';
|
2022-02-27 18:32:22 +00:00
|
|
|
import { Transition } from '@/components/elements/transitions';
|
2022-02-27 16:04:57 +00:00
|
|
|
import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
|
2022-02-26 22:05:30 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2022-02-27 02:51:40 +00:00
|
|
|
import { Checkbox, InputField } from '@/components/elements/inputs';
|
2022-02-27 16:04:57 +00:00
|
|
|
import UserTableRow from '@/components/admin/users/UserTableRow';
|
2022-02-27 21:15:11 +00:00
|
|
|
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
|
|
|
|
import { AxiosError } from 'axios';
|
2022-02-27 21:45:23 +00:00
|
|
|
import debounce from 'debounce';
|
|
|
|
|
|
|
|
const filters = [ 'id', 'uuid', 'external_id', 'username', 'email' ] as const;
|
|
|
|
type Filters = typeof filters[number];
|
2022-02-21 00:10:58 +00:00
|
|
|
|
2022-02-27 21:15:11 +00:00
|
|
|
const useGetUsers = (
|
2022-02-27 21:45:23 +00:00
|
|
|
params?: QueryBuilderParams<Filters>,
|
2022-02-27 21:15:11 +00:00
|
|
|
config?: SWRConfiguration,
|
|
|
|
): SWRResponse<PaginatedResult<User>, AxiosError> => {
|
2022-02-27 21:45:23 +00:00
|
|
|
return useSWR<PaginatedResult<User>>([ '/api/application/users', JSON.stringify(params) ], async () => {
|
2022-02-27 21:15:11 +00:00
|
|
|
const { data } = await http.get<FractalPaginatedResponse>(
|
|
|
|
'/api/application/users',
|
|
|
|
{ params: withQueryBuilderParams(params) },
|
|
|
|
);
|
|
|
|
|
|
|
|
return toPaginatedSet(data, Transformers.toUser);
|
|
|
|
}, config || { revalidateOnMount: true, revalidateOnFocus: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
const PaginationFooter = ({ pagination, span }: { span: number; pagination: PaginationDataSet }) => {
|
|
|
|
const start = (pagination.currentPage - 1) * pagination.perPage;
|
|
|
|
const end = ((pagination.currentPage - 1) * pagination.perPage) + pagination.count;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tfoot>
|
|
|
|
<tr className={'bg-neutral-800'}>
|
|
|
|
<td scope={'col'} colSpan={span} className={'px-4 py-2'}>
|
|
|
|
<p className={'text-sm text-neutral-500'}>
|
2022-02-27 21:45:23 +00:00
|
|
|
Showing <span className={'font-semibold text-neutral-400'}>{Math.max(start, Math.min(pagination.total, 1))}</span> to
|
2022-02-27 21:15:11 +00:00
|
|
|
<span className={'font-semibold text-neutral-400'}>{end}</span> of
|
|
|
|
<span className={'font-semibold text-neutral-400'}>{pagination.total}</span> results.
|
|
|
|
</p>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tfoot>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-27 21:45:23 +00:00
|
|
|
const extractFiltersFromString = (str: string, params: (keyof Filters)[]): QueryBuilderParams => {
|
|
|
|
const filters: Partial<Record<string, string[]>> = {};
|
|
|
|
|
|
|
|
const parts = str.split(' ');
|
|
|
|
for (const segment of parts) {
|
|
|
|
const [ filter, value ] = segment.split(':', 2);
|
|
|
|
// @ts-ignore
|
|
|
|
if (!filter || !value || !params.includes(filter)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = filter as string;
|
|
|
|
filters[key] = [ ...(filters[key] || []), value ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(filters).length) {
|
|
|
|
return { filters: { email: str } };
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
return { filters };
|
|
|
|
};
|
|
|
|
|
2022-02-27 21:15:11 +00:00
|
|
|
const UsersContainer = () => {
|
2022-02-27 21:45:23 +00:00
|
|
|
const [ search, setSearch ] = useState('');
|
2022-02-27 18:32:22 +00:00
|
|
|
const [ selected, setSelected ] = useState<UUID[]>([]);
|
2022-02-27 21:45:23 +00:00
|
|
|
const { data: users } = useGetUsers(extractFiltersFromString(search, filters as unknown as (keyof Filters)[]));
|
2022-02-27 18:32:22 +00:00
|
|
|
|
2022-02-21 00:10:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
document.title = 'Admin | Users';
|
|
|
|
}, []);
|
|
|
|
|
2022-02-27 18:32:22 +00:00
|
|
|
const onRowChange = (user: User, checked: boolean) => {
|
|
|
|
setSelected((state) => {
|
|
|
|
return checked ? [ ...state, user.uuid ] : selected.filter((uuid) => uuid !== user.uuid);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-27 21:15:11 +00:00
|
|
|
const selectAllChecked = users && users.items.length > 0 && selected.length > 0;
|
|
|
|
const onSelectAll = () => setSelected((state) => state.length > 0 ? [] : users?.items.map(({ uuid }) => uuid) || []);
|
2022-02-27 18:32:22 +00:00
|
|
|
|
2022-02-27 21:45:23 +00:00
|
|
|
const setSearchTerm = useCallback(debounce((term: string) => {
|
|
|
|
setSearch(term);
|
|
|
|
}, 200), []);
|
|
|
|
|
2022-02-21 00:10:58 +00:00
|
|
|
return (
|
2022-02-26 22:05:30 +00:00
|
|
|
<div>
|
|
|
|
<div className={'flex justify-end mb-4'}>
|
|
|
|
<Button className={'shadow focus:ring-offset-2 focus:ring-offset-neutral-800'}>
|
|
|
|
Add User <PlusIcon className={'ml-2 w-5 h-5'}/>
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-02-27 03:06:47 +00:00
|
|
|
<div className={'relative flex items-center rounded-t bg-neutral-700 px-4 py-2'}>
|
2022-02-27 02:51:40 +00:00
|
|
|
<div className={'mr-6'}>
|
2022-02-27 18:32:22 +00:00
|
|
|
<Checkbox
|
|
|
|
checked={selectAllChecked}
|
2022-02-27 21:15:11 +00:00
|
|
|
indeterminate={selected.length !== users?.items.length}
|
2022-02-27 18:32:22 +00:00
|
|
|
onChange={onSelectAll}
|
|
|
|
/>
|
2022-02-27 02:51:40 +00:00
|
|
|
</div>
|
|
|
|
<div className={'flex-1'}>
|
2022-02-27 16:04:57 +00:00
|
|
|
<InputField
|
|
|
|
type={'text'}
|
|
|
|
name={'filter'}
|
|
|
|
placeholder={'Begin typing to filter...'}
|
|
|
|
className={'w-56 focus:w-96'}
|
2022-02-27 21:45:23 +00:00
|
|
|
onChange={e => setSearchTerm(e.currentTarget.value)}
|
2022-02-27 16:04:57 +00:00
|
|
|
/>
|
2022-02-27 02:51:40 +00:00
|
|
|
</div>
|
2022-02-27 18:32:22 +00:00
|
|
|
<Transition.Fade as={Fragment} show={selected.length > 0} duration={'duration-75'}>
|
|
|
|
<div className={'absolute rounded-t bg-neutral-700 w-full h-full top-0 left-0 flex items-center justify-end space-x-4 px-4'}>
|
|
|
|
<div className={'flex-1'}>
|
|
|
|
<Checkbox
|
|
|
|
checked={selectAllChecked}
|
2022-02-27 21:15:11 +00:00
|
|
|
indeterminate={selected.length !== users?.items.length}
|
2022-02-27 18:32:22 +00:00
|
|
|
onChange={onSelectAll}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button.Text square>
|
|
|
|
<SupportIcon className={'w-4 h-4'}/>
|
|
|
|
</Button.Text>
|
|
|
|
<Button.Text square>
|
|
|
|
<LockOpenIcon className={'w-4 h-4'}/>
|
|
|
|
</Button.Text>
|
|
|
|
<Button.Text square>
|
|
|
|
<TrashIcon className={'w-4 h-4'}/>
|
|
|
|
</Button.Text>
|
2022-02-27 03:06:47 +00:00
|
|
|
</div>
|
2022-02-27 18:32:22 +00:00
|
|
|
</Transition.Fade>
|
2022-02-27 02:51:40 +00:00
|
|
|
</div>
|
2022-02-26 22:05:30 +00:00
|
|
|
<table className={'min-w-full rounded bg-neutral-700'}>
|
2022-02-21 00:10:58 +00:00
|
|
|
<thead className={'bg-neutral-900'}>
|
|
|
|
<tr>
|
|
|
|
<th scope={'col'} className={'w-8'}/>
|
|
|
|
<th scope={'col'} className={'text-left px-6 py-2 w-full'}>Email</th>
|
|
|
|
<th scope={'col'}/>
|
2022-02-27 02:26:53 +00:00
|
|
|
<th scope={'col'}/>
|
2022-02-21 00:10:58 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-02-27 21:15:11 +00:00
|
|
|
{users?.items.map(user => (
|
2022-02-27 18:32:22 +00:00
|
|
|
<UserTableRow
|
|
|
|
key={user.uuid}
|
|
|
|
user={user}
|
|
|
|
selected={selected.includes(user.uuid)}
|
|
|
|
onRowChange={onRowChange}
|
|
|
|
/>
|
|
|
|
))}
|
2022-02-21 00:10:58 +00:00
|
|
|
</tbody>
|
2022-02-27 21:15:11 +00:00
|
|
|
{users && <PaginationFooter span={4} pagination={users.pagination}/>}
|
2022-02-21 00:10:58 +00:00
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-27 21:15:11 +00:00
|
|
|
export default UsersContainer;
|