Skip to content

Commit

Permalink
Typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
rique223 committed Mar 22, 2024
1 parent ed29479 commit 5b74149
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions apps/meteor/client/views/admin/users/AdminUsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type UsersFilters = {
text: string;
};

export type UsersTableSortingOptions = 'name' | 'username' | 'emails.address' | 'status' | 'active';

const AdminUsersPage = (): ReactElement => {
const t = useTranslation();

Expand Down
31 changes: 22 additions & 9 deletions apps/meteor/client/views/admin/users/hooks/useFilteredUsers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import type { IAdminUserTabs } from '@rocket.chat/core-typings';
import type { UsersListStatusParamsGET } from '@rocket.chat/rest-typings';
import { useEndpoint, useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import type { MutableRefObject } from 'react';
import { useMemo } from 'react';

import type { usePagination } from '../../../../components/GenericTable/hooks/usePagination';
import type { useSort } from '../../../../components/GenericTable/hooks/useSort';
import type { UsersTableSortingOptions } from '../AdminUsersPage';

type useFilteredUsersOptions = {
type UseFilteredUsersOptions = {
searchTerm: string;
prevSearchTerm: MutableRefObject<string>;
tab: IAdminUserTabs;
paginationData: ReturnType<typeof usePagination>;
sortData: ReturnType<typeof useSort<'name' | 'username' | 'emails.address' | 'status'>>;
sortData: ReturnType<typeof useSort<UsersTableSortingOptions>>;
};

const useFilteredUsers = ({ searchTerm, prevSearchTerm, sortData, paginationData, tab }: useFilteredUsersOptions) => {
const useFilteredUsers = ({ searchTerm, prevSearchTerm, sortData, paginationData, tab }: UseFilteredUsersOptions) => {
const { setCurrent, itemsPerPage, current } = paginationData;
const { sortBy, sortDirection } = sortData;

Expand All @@ -24,26 +26,37 @@ const useFilteredUsers = ({ searchTerm, prevSearchTerm, sortData, paginationData
setCurrent(0);
}

const listUsersPayload: Partial<Record<IAdminUserTabs, UsersListStatusParamsGET>> = {
all: {},
pending: {
hasLoggedIn: false,
type: 'user',
},
active: {
hasLoggedIn: true,
status: 'active',
},
deactivated: {
hasLoggedIn: true,
status: 'deactivated',
},
};

return {
status: tab,
...listUsersPayload[tab],
searchTerm,
sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`,
count: itemsPerPage,
offset: searchTerm === prevSearchTerm.current ? current : 0,
};
}, [current, itemsPerPage, prevSearchTerm, searchTerm, setCurrent, sortBy, sortDirection, tab]);

const getUsers = useEndpoint('GET', '/v1/users.listByStatus');

const dispatchToastMessage = useToastMessageDispatch();

const usersListQueryResult = useQuery(['users.list', payload, tab], async () => getUsers(payload), {
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
},
});

return usersListQueryResult;
};

export default useFilteredUsers;

0 comments on commit 5b74149

Please sign in to comment.