Compare commits
1 Commits
2bfb94d01b
...
feature/le
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4282c1d021 |
@@ -1,15 +1,15 @@
|
|||||||
import {Type, User} from "@/interfaces/user";
|
import { Type, User } from "@/interfaces/user";
|
||||||
import Axios from "axios";
|
import Axios from "axios";
|
||||||
import {useEffect, useState} from "react";
|
import { useEffect, useState } from "react";
|
||||||
import {setupCache} from "axios-cache-interceptor";
|
import { setupCache } from "axios-cache-interceptor";
|
||||||
const instance = Axios.create();
|
const instance = Axios.create();
|
||||||
const axios = setupCache(instance);
|
const axios = setupCache(instance);
|
||||||
|
|
||||||
export const userHashStudent = {type: "student"} as {type: Type};
|
export const userHashStudent = { query: { type: "student" } } as { query: { type: Type } };
|
||||||
export const userHashTeacher = {type: "teacher"} as {type: Type};
|
export const userHashTeacher = { query: { type: "teacher" } } as { query: { type: Type } };
|
||||||
export const userHashCorporate = {type: "corporate"} as {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 [users, setUsers] = useState<User[]>([]);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
@@ -18,16 +18,21 @@ export default function useUsers(props?: {type?: string; page?: number; size?: n
|
|||||||
const getData = () => {
|
const getData = () => {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
if (!!props)
|
if (!!props) {
|
||||||
Object.keys(props).forEach((key) => {
|
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);
|
setIsLoading(true);
|
||||||
axios
|
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) => {
|
.then((response) => {
|
||||||
setUsers(response.data.users);
|
setUsers(response.data.users);
|
||||||
setTotal(response.data.total);
|
setTotal(response.data.total);
|
||||||
@@ -35,7 +40,7 @@ export default function useUsers(props?: {type?: string; page?: number; size?: n
|
|||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
};
|
};
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// 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 };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
code?: string;
|
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) {
|
if (code && code.length > 0 && !!codeDoc) {
|
||||||
res.status(400).json({error: "Invalid Code!"});
|
res.status(400).json({error: "Invalid Code!"});
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetUser = await db.collection("users").findOne<User>({id});
|
const targetUser = await db.collection("users").findOne<User>({id: id});
|
||||||
if (!targetUser) {
|
if (!targetUser) {
|
||||||
res.status(404).json({ok: false});
|
res.status(404).json({ok: false});
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type {NextApiRequest, NextApiResponse} from "next";
|
|||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {getLinkedUsers} from "@/utils/users.be";
|
import {getLinkedUsers} from "@/utils/users.be";
|
||||||
import {Type} from "@/interfaces/user";
|
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -15,16 +14,16 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
size,
|
size,
|
||||||
type,
|
|
||||||
page,
|
page,
|
||||||
orderBy,
|
orderBy,
|
||||||
direction = "desc",
|
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(
|
const {users, total} = await getLinkedUsers(
|
||||||
req.session.user?.id,
|
req.session.user?.id,
|
||||||
req.session.user?.type,
|
req.session.user?.type,
|
||||||
type,
|
query,
|
||||||
page !== undefined ? parseInt(page) : undefined,
|
page !== undefined ? parseInt(page) : undefined,
|
||||||
size !== undefined ? parseInt(size) : undefined,
|
size !== undefined ? parseInt(size) : undefined,
|
||||||
orderBy,
|
orderBy,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import Layout from "@/components/High/Layout";
|
import Layout from "@/components/High/Layout";
|
||||||
import useUser from "@/hooks/useUser";
|
import useUser from "@/hooks/useUser";
|
||||||
import useUsers from "@/hooks/useUsers";
|
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import useFilterStore from "@/stores/listFilterStore";
|
import useFilterStore from "@/stores/listFilterStore";
|
||||||
import {withIronSessionSsr} from "iron-session/next";
|
import {withIronSessionSsr} from "iron-session/next";
|
||||||
|
|||||||
@@ -25,17 +25,20 @@ export async function getSpecificUsers(ids: string[]) {
|
|||||||
.toArray();
|
.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Query = { [key: string]: string | string[] | undefined };
|
||||||
|
|
||||||
|
|
||||||
export async function getLinkedUsers(
|
export async function getLinkedUsers(
|
||||||
userID?: string,
|
userID?: string,
|
||||||
userType?: Type,
|
userType?: Type,
|
||||||
type?: Type,
|
query?: Query,
|
||||||
page?: number,
|
page?: number,
|
||||||
size?: number,
|
size?: number,
|
||||||
sort?: string,
|
sort?: string,
|
||||||
direction?: "asc" | "desc",
|
direction?: "asc" | "desc",
|
||||||
) {
|
) {
|
||||||
const filters = {
|
const filters = {
|
||||||
...(!!type ? {type} : {}),
|
...(!!query ? query : {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!userID || userType === "admin" || userType === "developer") {
|
if (!userID || userType === "admin" || userType === "developer") {
|
||||||
|
|||||||
Reference in New Issue
Block a user