Compare commits

...

3 Commits

Author SHA1 Message Date
carlos.mesquita
22209ee1c1 Merged main into feature/training-content 2024-09-09 00:40:36 +00:00
Carlos Mesquita
0e2f53db0a Search on user list with mongo search query 2024-09-09 01:38:11 +01:00
Carlos Mesquita
9177a6b2ac Pagination on UserList 2024-09-09 01:22:13 +01:00
4 changed files with 107 additions and 104 deletions

View File

@@ -9,7 +9,7 @@ export const userHashStudent = {type: "student"} as {type: Type};
export const userHashTeacher = {type: "teacher"} as {type: Type};
export const userHashCorporate = {type: "corporate"} as {type: Type};
export default function useUsers(props?: {type?: string; page?: number; size?: number; orderBy?: string; direction?: "asc" | "desc"}) {
export default function useUsers(props?: {type?: string; page?: number; size?: number; orderBy?: string; direction?: "asc" | "desc", searchTerm?: string | undefined}) {
const [users, setUsers] = useState<User[]>([]);
const [total, setTotal] = useState(0);
const [isLoading, setIsLoading] = useState(false);
@@ -35,7 +35,7 @@ export default function useUsers(props?: {type?: string; page?: number; size?: n
.finally(() => setIsLoading(false));
};
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(getData, [props?.page, props?.size, props?.type, props?.orderBy, props?.direction]);
useEffect(getData, [props?.page, props?.size, props?.type, props?.orderBy, props?.direction, props?.searchTerm]);
return {users, total, isLoading, isError, reload: getData};
}

View File

@@ -28,6 +28,7 @@ import {checkAccess} from "@/utils/permissions";
import { PermissionType } from "@/interfaces/permissions";
import usePermissions from "@/hooks/usePermissions";
import useUserBalance from "@/hooks/useUserBalance";
import Input from "@/components/Low/Input";
const columnHelper = createColumnHelper<User>();
const searchFields = [["name"], ["email"], ["corporateInformation", "companyInformation", "name"]];
@@ -63,17 +64,10 @@ export default function UserList({
const [displayUsers, setDisplayUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User>();
const [page, setPage] = useState(0);
const [searchTerm, setSearchTerm] = useState<string | undefined>(undefined);
const userHash = useMemo(
() => ({
type,
size: 16,
page,
}),
[type, page],
);
const { users, total, isLoading, reload } = useUsers({type: type, size: 16, page: page, searchTerm: searchTerm});
const {users, total, isLoading, reload} = useUsers(userHash);
const {users: corporates} = useUsers(corporatesHash);
const totalUsers = useMemo(() => [...users, ...corporates], [users, corporates]);
@@ -143,8 +137,7 @@ export default function UserList({
const toggleDisableAccount = (user: User) => {
if (
!confirm(
`Are you sure you want to ${user.status === "disabled" ? "enable" : "disable"} ${
user.name
`Are you sure you want to ${user.status === "disabled" ? "enable" : "disable"} ${user.name
}'s account? This change is usually related to their payment state.`,
)
)
@@ -238,8 +231,7 @@ export default function UserList({
) as any,
cell: (info) =>
info.getValue()
? `${countryCodes.findOne("countryCode" as any, info.getValue())?.flag} ${
countries[info.getValue() as unknown as keyof TCountries]?.name
? `${countryCodes.findOne("countryCode" as any, info.getValue())?.flag} ${countries[info.getValue() as unknown as keyof TCountries]?.name
} (+${countryCodes.findOne("countryCode" as any, info.getValue())?.countryCallingCode})`
: "N/A",
}),
@@ -513,16 +505,14 @@ export default function UserList({
return a.id.localeCompare(b.id);
};
const {rows: filteredRows, renderSearch} = useListSearch<User>(searchFields, displayUsers);
const table = useReactTable({
data: filteredRows,
data: displayUsers,
columns: (!showDemographicInformation ? defaultColumns : demographicColumns) as any,
getCoreRowModel: getCoreRowModel(),
});
const downloadExcel = () => {
const csv = exportListToExcel(filteredRows, users, groups);
const csv = exportListToExcel(displayUsers, users, groups);
const element = document.createElement("a");
const file = new Blob([csv], { type: "text/csv" });
@@ -627,7 +617,7 @@ export default function UserList({
</Modal>
<div className="w-full flex flex-col gap-2">
<div className="w-full flex gap-2 items-end">
{renderSearch()}
<Input label="Search" type="text" name="search" onChange={setSearchTerm} placeholder="Enter search text" value={searchTerm} />
<Button className="w-full max-w-[200px] mb-1" variant="outline" onClick={downloadExcel}>
Download List
</Button>

View File

@@ -19,7 +19,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
page,
orderBy,
direction = "desc",
} = req.query as {size?: string; type?: Type; page?: string; orderBy?: string; direction?: "asc" | "desc"};
searchTerm
} = req.query as {size?: string; type?: Type; page?: string; orderBy?: string; direction?: "asc" | "desc"; searchTerm?: string | undefined};
const {users, total} = await getLinkedUsers(
req.session.user?.id,
@@ -29,6 +30,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
size !== undefined ? parseInt(size) : undefined,
orderBy,
direction,
searchTerm
);
res.status(200).json({users, total});

View File

@@ -33,10 +33,21 @@ export async function getLinkedUsers(
size?: number,
sort?: string,
direction?: "asc" | "desc",
searchTerm?: string | undefined,
) {
const filters = {
...(!!type ? {type} : {}),
};
const filters: any = {};
if (type) {
filters.type = type;
}
if (searchTerm) {
filters.$or = [
{ name: { $regex: searchTerm, $options: 'i' } },
{ email: { $regex: searchTerm, $options: 'i' } },
{ company: { $regex: searchTerm, $options: 'i' } },
];
}
if (!userID || userType === "admin" || userType === "developer") {
const users = await db