diff --git a/resources/scripts/api/admin/users.ts b/resources/scripts/api/admin/users.ts index 6eaa092dd..c7fca8ac7 100644 --- a/resources/scripts/api/admin/users.ts +++ b/resources/scripts/api/admin/users.ts @@ -1,5 +1,12 @@ -import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; +import http, { + FractalPaginatedResponse, + PaginatedResult, + QueryBuilderParams, toPaginatedSet, + withQueryBuilderParams, +} from '@/api/http'; import { Transformers, User } from '@definitions/admin'; +import useSWR, { SWRConfiguration, SWRResponse } from 'swr'; +import { AxiosError } from 'axios'; export interface UpdateUserValues { externalId: string; @@ -10,6 +17,23 @@ export interface UpdateUserValues { rootAdmin: boolean; } +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 getUser = (id: number, include: string[] = []): Promise => { return new Promise((resolve, reject) => { http.get(`/api/application/users/${id}`, { params: { include: include.join(',') } }) @@ -66,6 +90,7 @@ const deleteUser = (id: number): Promise => { }; export { + useGetUsers, getUser, searchUserAccounts, createUser, diff --git a/resources/scripts/components/admin/users/UsersContainer.tsx b/resources/scripts/components/admin/users/UsersContainer.tsx index 835a6d8c4..5889c2e66 100644 --- a/resources/scripts/components/admin/users/UsersContainer.tsx +++ b/resources/scripts/components/admin/users/UsersContainer.tsx @@ -1,59 +1,19 @@ import React, { Fragment, useCallback, useEffect, useState } from 'react'; -import http, { - FractalPaginatedResponse, - PaginatedResult, - PaginationDataSet, - QueryBuilderParams, - toPaginatedSet, - withQueryBuilderParams, -} from '@/api/http'; +import { QueryBuilderParams } from '@/api/http'; import { UUID } from '@/api/definitions'; -import { Transformers, User } from '@definitions/admin'; +import { 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'; +import { useGetUsers } from '@/api/admin/users'; +import TFootPaginated from '@/components/elements/table/TFootPaginated'; 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> = {}; @@ -163,7 +123,7 @@ const UsersContainer = () => { /> ))} - {users && } + {users && } ); diff --git a/resources/scripts/components/elements/table/TFootPaginated.tsx b/resources/scripts/components/elements/table/TFootPaginated.tsx new file mode 100644 index 000000000..61810e9bb --- /dev/null +++ b/resources/scripts/components/elements/table/TFootPaginated.tsx @@ -0,0 +1,23 @@ +import React from 'react'; +import { PaginationDataSet } from '@/api/http'; + +const TFootPaginated = ({ 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. +

+ + + + ); +}; + +export default TFootPaginated;