Compare commits

...

1 Commits

Author SHA1 Message Date
Carlos Mesquita
4282c1d021 useUsers refactor 2024-09-08 09:37:19 +01:00
6 changed files with 29 additions and 23 deletions

View File

@@ -1,15 +1,15 @@
import {Type, User} from "@/interfaces/user";
import { Type, User } from "@/interfaces/user";
import Axios from "axios";
import {useEffect, useState} from "react";
import {setupCache} from "axios-cache-interceptor";
import { useEffect, useState } from "react";
import { setupCache } from "axios-cache-interceptor";
const instance = Axios.create();
const axios = setupCache(instance);
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 const userHashStudent = { query: { type: "student" } } as { query: { type: Type } };
export const userHashTeacher = { query: { type: "teacher" } } as { query: { type: Type } };
export const userHashCorporate = { query: { type: "corporate" } } as { query: { type: Type } };
export default function useUsers(props?: {type?: string; page?: number; size?: number; orderBy?: string; direction?: "asc" | "desc"}) {
export default function useUsers(props?: { query?: Record<string, string>; page?: number; size?: number; orderBy?: string; direction?: "asc" | "desc" }) {
const [users, setUsers] = useState<User[]>([]);
const [total, setTotal] = useState(0);
const [isLoading, setIsLoading] = useState(false);
@@ -18,16 +18,21 @@ export default function useUsers(props?: {type?: string; page?: number; size?: n
const getData = () => {
const params = new URLSearchParams();
if (!!props)
if (!!props) {
Object.keys(props).forEach((key) => {
if (props[key as keyof typeof props] !== undefined) params.append(key, props[key as keyof typeof props]!.toString());
if (key === 'query' && props.query) {
Object.entries(props.query).forEach(([queryKey, queryValue]) => {
if (queryValue !== undefined) params.append(queryKey, queryValue);
});
} else if (props[key as keyof typeof props] !== undefined) {
params.append(key, props[key as keyof typeof props]!.toString());
}
});
console.log(params.toString());
}
setIsLoading(true);
axios
.get<{users: User[]; total: number}>(`/api/users/list?${params.toString()}`, {headers: {page: "register"}})
.get<{ users: User[]; total: number }>(`/api/users/list?${params.toString()}`, { headers: { page: "register" } })
.then((response) => {
setUsers(response.data.users);
setTotal(response.data.total);
@@ -35,7 +40,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?.query, props?.orderBy, props?.direction]);
return {users, total, isLoading, isError, reload: getData};
return { users, total, isLoading, isError, reload: getData };
}

View File

@@ -45,7 +45,7 @@ async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
code?: string;
};
const codeDoc = await db.collection("codes").findOne<Code>({code});
const codeDoc = await db.collection("codes").findOne<Code>({id: code});
if (code && code.length > 0 && !!codeDoc) {
res.status(400).json({error: "Invalid Code!"});

View File

@@ -35,7 +35,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
return;
}
const targetUser = await db.collection("users").findOne<User>({id});
const targetUser = await db.collection("users").findOne<User>({id: id});
if (!targetUser) {
res.status(404).json({ok: false});
return;

View File

@@ -3,7 +3,6 @@ import type {NextApiRequest, NextApiResponse} from "next";
import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {getLinkedUsers} from "@/utils/users.be";
import {Type} from "@/interfaces/user";
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -15,16 +14,16 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const {
size,
type,
page,
orderBy,
direction = "desc",
} = req.query as {size?: string; type?: Type; page?: string; orderBy?: string; direction?: "asc" | "desc"};
...query
} = req.query as {size?: string; page?: string; orderBy?: string; direction?: "asc" | "desc"; [key: string]: string | string[] | undefined};
const {users, total} = await getLinkedUsers(
req.session.user?.id,
req.session.user?.type,
type,
query,
page !== undefined ? parseInt(page) : undefined,
size !== undefined ? parseInt(size) : undefined,
orderBy,

View File

@@ -1,6 +1,5 @@
import Layout from "@/components/High/Layout";
import useUser from "@/hooks/useUser";
import useUsers from "@/hooks/useUsers";
import {sessionOptions} from "@/lib/session";
import useFilterStore from "@/stores/listFilterStore";
import {withIronSessionSsr} from "iron-session/next";

View File

@@ -25,17 +25,20 @@ export async function getSpecificUsers(ids: string[]) {
.toArray();
}
type Query = { [key: string]: string | string[] | undefined };
export async function getLinkedUsers(
userID?: string,
userType?: Type,
type?: Type,
query?: Query,
page?: number,
size?: number,
sort?: string,
direction?: "asc" | "desc",
) {
const filters = {
...(!!type ? {type} : {}),
...(!!query ? query : {}),
};
if (!userID || userType === "admin" || userType === "developer") {