From a269c3714c66f3aee32d34e985ad1a8096afc519 Mon Sep 17 00:00:00 2001 From: Benson Cho <100653148+bcho892@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:26:21 +1200 Subject: [PATCH] Allow sorting users by role on admin member view (#575) * implement sorting by row * split filters --- .../Admin/AdminMemberView/AdminMemberView.tsx | 62 ++++++++++++++++--- client/src/models/User.ts | 2 + 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/client/src/components/composite/Admin/AdminMemberView/AdminMemberView.tsx b/client/src/components/composite/Admin/AdminMemberView/AdminMemberView.tsx index cbe261b15..a303b4d88 100644 --- a/client/src/components/composite/Admin/AdminMemberView/AdminMemberView.tsx +++ b/client/src/components/composite/Admin/AdminMemberView/AdminMemberView.tsx @@ -5,7 +5,8 @@ import { TABLE_ROW_IDENTIFIER_KEY, TableRowOperation } from "components/generic/ReusableTable/TableUtils" -import { useEffect, useState } from "react" +import { AccountType } from "models/User" +import { useCallback, useEffect, useState } from "react" export type MemberColumnFormat = { /** @@ -68,6 +69,8 @@ const defaultData = { const ADMIN_MEMBER_VIEW_MIN_SEARCH_QUERY_LENGTH = 2 as const +type AccountTypeFilter = AccountType | "all" + /** * The view to be displayed on the `admin` route when the admin wants to: * - Add new users @@ -91,17 +94,51 @@ export const AdminMemberView = ({ * For use with `AdminSearchBar` */ const [currentSearchQuery, setCurrentSearchQuery] = useState("") + + const [filteredAccountType, setFilteredAccountType] = + useState("all") + + /** + * Sets the current role filter to the next one. + * Note that the sequence should repeat circularly + */ + const nextAccountFilter = useCallback(() => { + switch (filteredAccountType) { + case "admin": + setFilteredAccountType("member") + return + case "member": + setFilteredAccountType("guest") + return + case "guest": + setFilteredAccountType("all") + return + case "all": + setFilteredAccountType("admin") + } + }, [filteredAccountType]) + const [isLastPage, setIsLastPage] = useState(false) const isValidSearchQuery = currentSearchQuery.length > ADMIN_MEMBER_VIEW_MIN_SEARCH_QUERY_LENGTH - const dataFilter = (oldData: MemberColumnFormat[]) => - isValidSearchQuery - ? oldData.filter( - (item) => - item.Email?.toLowerCase().includes(currentSearchQuery) || - item.Name?.toLowerCase().includes(currentSearchQuery) - ) - : oldData + + const dataFilter = useCallback( + (oldData: MemberColumnFormat[]) => { + const shouldFilterByAccount = filteredAccountType !== "all" + + return isValidSearchQuery || shouldFilterByAccount + ? oldData.filter( + (item) => + (isValidSearchQuery && + (item.Email?.toLowerCase().includes(currentSearchQuery) || + item.Name?.toLowerCase().includes(currentSearchQuery))) || + (shouldFilterByAccount && + filteredAccountType === item.Status?.toLowerCase()) + ) + : oldData + }, + [isValidSearchQuery, currentSearchQuery, filteredAccountType] + ) useEffect(() => { /** @@ -123,7 +160,12 @@ export const AdminMemberView = ({ - +