From 58300e32ffeb4fac117afe79fc4284ac72c5ef92 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Mon, 12 Aug 2024 19:35:11 +0100 Subject: [PATCH] Solved an issue where, for developers, because of the amount of permissions, the cookie was too big, so I separated the permissions logic into a hook --- src/components/High/TicketDisplay.tsx | 384 +++--- src/components/Sidebar.tsx | 24 +- src/components/UserCard.tsx | 1422 +++++++++------------ src/dashboards/Teacher.tsx | 793 +++++------- src/hooks/usePermissions.tsx | 29 + src/interfaces/permissions.ts | 97 +- src/pages/(admin)/BatchCodeGenerator.tsx | 595 ++++----- src/pages/(admin)/CodeGenerator.tsx | 322 +++-- src/pages/(admin)/Lists/CodeList.tsx | 596 ++++----- src/pages/(admin)/Lists/GroupList.tsx | 727 +++++------ src/pages/(admin)/Lists/UserList.tsx | 1493 +++++++++------------- src/pages/(admin)/Lists/index.tsx | 249 ++-- src/pages/api/permissions/[id].ts | 54 +- src/pages/api/tickets/index.ts | 5 +- src/pages/api/user.ts | 216 ++-- src/pages/settings.tsx | 12 +- src/utils/permissions.ts | 67 +- 17 files changed, 3060 insertions(+), 4025 deletions(-) create mode 100644 src/hooks/usePermissions.tsx diff --git a/src/components/High/TicketDisplay.tsx b/src/components/High/TicketDisplay.tsx index 582cae5d..4c5bccb7 100644 --- a/src/components/High/TicketDisplay.tsx +++ b/src/components/High/TicketDisplay.tsx @@ -1,255 +1,179 @@ import useUsers from "@/hooks/useUsers"; -import { - Ticket, - TicketStatus, - TicketStatusLabel, - TicketType, - TicketTypeLabel, -} from "@/interfaces/ticket"; -import { User } from "@/interfaces/user"; -import { USER_TYPE_LABELS } from "@/resources/user"; +import {Ticket, TicketStatus, TicketStatusLabel, TicketType, TicketTypeLabel} from "@/interfaces/ticket"; +import {User} from "@/interfaces/user"; +import {USER_TYPE_LABELS} from "@/resources/user"; import axios from "axios"; import moment from "moment"; -import { useState } from "react"; -import { toast } from "react-toastify"; +import {useState} from "react"; +import {toast} from "react-toastify"; import ShortUniqueId from "short-unique-id"; import Button from "../Low/Button"; import Input from "../Low/Input"; import Select from "../Low/Select"; -import { checkAccess } from "@/utils/permissions"; +import {checkAccess} from "@/utils/permissions"; interface Props { - user: User; - ticket: Ticket; - onClose: () => void; + user: User; + ticket: Ticket; + onClose: () => void; } -export default function TicketDisplay({ user, ticket, onClose }: Props) { - const [subject] = useState(ticket.subject); - const [type, setType] = useState(ticket.type); - const [description] = useState(ticket.description); - const [reporter] = useState(ticket.reporter); - const [reportedFrom] = useState(ticket.reportedFrom); - const [status, setStatus] = useState(ticket.status); - const [assignedTo, setAssignedTo] = useState( - ticket.assignedTo || null, - ); - const [isLoading, setIsLoading] = useState(false); +export default function TicketDisplay({user, ticket, onClose}: Props) { + const [subject] = useState(ticket.subject); + const [type, setType] = useState(ticket.type); + const [description] = useState(ticket.description); + const [reporter] = useState(ticket.reporter); + const [reportedFrom] = useState(ticket.reportedFrom); + const [status, setStatus] = useState(ticket.status); + const [assignedTo, setAssignedTo] = useState(ticket.assignedTo || null); + const [isLoading, setIsLoading] = useState(false); - const { users } = useUsers(); + const {users} = useUsers(); - const submit = () => { - if (!type) - return toast.error("Please choose a type!", { toastId: "missing-type" }); + const submit = () => { + if (!type) return toast.error("Please choose a type!", {toastId: "missing-type"}); - setIsLoading(true); - axios - .patch(`/api/tickets/${ticket.id}`, { - subject, - type, - description, - reporter, - reportedFrom, - status, - assignedTo, - }) - .then(() => { - toast.success(`The ticket has been updated!`, { toastId: "submitted" }); - onClose(); - }) - .catch((e) => { - console.error(e); - toast.error("Something went wrong, please try again later!", { - toastId: "error", - }); - }) - .finally(() => setIsLoading(false)); - }; + setIsLoading(true); + axios + .patch(`/api/tickets/${ticket.id}`, { + subject, + type, + description, + reporter, + reportedFrom, + status, + assignedTo, + }) + .then(() => { + toast.success(`The ticket has been updated!`, {toastId: "submitted"}); + onClose(); + }) + .catch((e) => { + console.error(e); + toast.error("Something went wrong, please try again later!", { + toastId: "error", + }); + }) + .finally(() => setIsLoading(false)); + }; - const del = () => { - if (!confirm("Are you sure you want to delete this ticket?")) return; + const del = () => { + if (!confirm("Are you sure you want to delete this ticket?")) return; - setIsLoading(true); - axios - .delete(`/api/tickets/${ticket.id}`) - .then(() => { - toast.success(`The ticket has been deleted!`, { toastId: "submitted" }); - onClose(); - }) - .catch((e) => { - console.error(e); - toast.error("Something went wrong, please try again later!", { - toastId: "error", - }); - }) - .finally(() => setIsLoading(false)); - }; + setIsLoading(true); + axios + .delete(`/api/tickets/${ticket.id}`) + .then(() => { + toast.success(`The ticket has been deleted!`, {toastId: "submitted"}); + onClose(); + }) + .catch((e) => { + console.error(e); + toast.error("Something went wrong, please try again later!", { + toastId: "error", + }); + }) + .finally(() => setIsLoading(false)); + }; - return ( -
- null} - disabled - /> + return ( + + null} disabled /> -
-
- - ({ - value: x, - label: TicketTypeLabel[x as keyof typeof TicketTypeLabel], - }))} - value={{ value: type, label: TicketTypeLabel[type] }} - onChange={(value) => setType(value!.value as TicketType)} - placeholder="Type..." - /> -
-
+
+
+ + ({ + value: x, + label: TicketTypeLabel[x as keyof typeof TicketTypeLabel], + }))} + value={{value: type, label: TicketTypeLabel[type]}} + onChange={(value) => setType(value!.value as TicketType)} + placeholder="Type..." + /> +
+
-
- - checkAccess(x, ["admin", "developer", "agent"])) + .map((u) => ({ + value: u.id, + label: `${u.name} - ${u.email}`, + })), + ]} + disabled={checkAccess(user, ["agent"])} + value={ + assignedTo + ? { + value: assignedTo, + label: `${users.find((u) => u.id === assignedTo)?.name} - ${users.find((u) => u.id === assignedTo)?.email}`, + } + : null + } + onChange={(value) => (value ? setAssignedTo(value.value === "me" ? user.id : value.value) : setAssignedTo(null))} + placeholder="Assignee..." + isClearable + /> +
-
- null} - value={reportedFrom} - disabled - /> - null} - value={moment(ticket.date).format("DD/MM/YYYY - HH:mm")} - disabled - /> -
+
+ null} value={reportedFrom} disabled /> + null} value={moment(ticket.date).format("DD/MM/YYYY - HH:mm")} disabled /> +
-
- null} - value={reporter.name} - disabled - /> - null} - value={reporter.email} - disabled - /> - null} - value={USER_TYPE_LABELS[reporter.type]} - disabled - /> -
+
+ null} value={reporter.name} disabled /> + null} value={reporter.email} disabled /> + null} + value={USER_TYPE_LABELS[reporter.type]} + disabled + /> +
-