import React, { Fragment, useCallback, useEffect, useState } from 'react'; import http, { FractalPaginatedResponse, PaginatedResult, PaginationDataSet, QueryBuilderParams, toPaginatedSet, withQueryBuilderParams, } from '@/api/http'; import { UUID } from '@/api/definitions'; import { Transformers, User } from '@definitions/admin'; import { Transition } from '@/components/elements/transitions'; import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid'; import { Button } from '@/components/elements/button/index'; import { Checkbox, InputField } from '@/components/elements/inputs'; import UserTableRow from '@/components/admin/users/UserTableRow'; import useSWR, { SWRConfiguration, SWRResponse } from 'swr'; import { AxiosError } from 'axios'; import debounce from 'debounce'; const filters = [ 'id', 'uuid', 'external_id', 'username', 'email' ] as const; type Filters = typeof filters[number]; const useGetUsers = ( params?: QueryBuilderParams, config?: SWRConfiguration, ): SWRResponse, AxiosError> => { return useSWR>([ '/api/application/users', JSON.stringify(params) ], async () => { const { data } = await http.get( '/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 (

Showing {Math.max(start, Math.min(pagination.total, 1))} to  {end} of  {pagination.total} results.

); }; const extractFiltersFromString = (str: string, params: (keyof Filters)[]): QueryBuilderParams => { const filters: Partial> = {}; 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 }; }; const UsersContainer = () => { const [ search, setSearch ] = useState(''); const [ selected, setSelected ] = useState([]); const { data: users } = useGetUsers(extractFiltersFromString(search, filters as unknown as (keyof Filters)[])); useEffect(() => { document.title = 'Admin | Users'; }, []); const onRowChange = (user: User, checked: boolean) => { setSelected((state) => { return checked ? [ ...state, user.uuid ] : selected.filter((uuid) => uuid !== user.uuid); }); }; const selectAllChecked = users && users.items.length > 0 && selected.length > 0; const onSelectAll = () => setSelected((state) => state.length > 0 ? [] : users?.items.map(({ uuid }) => uuid) || []); const setSearchTerm = useCallback(debounce((term: string) => { setSearch(term); }, 200), []); return (
setSearchTerm(e.currentTarget.value)} />
0} duration={'duration-75'}>
{users?.items.map(user => ( ))} {users && }
Email
); }; export default UsersContainer;