From 8b7e550a704f1f30edfd0c36edb76e55bbf4a3c5 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sun, 28 Jan 2024 21:05:17 +0000 Subject: [PATCH] Created a new page for ticket handling as well as submission --- src/components/High/TicketDisplay.tsx | 253 ++++++++++++++ src/components/High/TicketSubmission.tsx | 134 +++++++ src/components/Low/Select.tsx | 68 ++++ src/components/MobileMenu.tsx | 312 ++++++++++------- src/components/Navbar.tsx | 216 ++++++++---- src/components/Sidebar.tsx | 428 +++++++++++++++-------- src/hooks/useTickets.tsx | 22 ++ src/interfaces/ticket.ts | 34 ++ src/pages/api/tickets/[id].ts | 81 +++++ src/pages/api/tickets/index.ts | 47 +++ src/pages/tickets.tsx | 316 +++++++++++++++++ 11 files changed, 1546 insertions(+), 365 deletions(-) create mode 100644 src/components/High/TicketDisplay.tsx create mode 100644 src/components/High/TicketSubmission.tsx create mode 100644 src/components/Low/Select.tsx create mode 100644 src/hooks/useTickets.tsx create mode 100644 src/interfaces/ticket.ts create mode 100644 src/pages/api/tickets/[id].ts create mode 100644 src/pages/api/tickets/index.ts create mode 100644 src/pages/tickets.tsx diff --git a/src/components/High/TicketDisplay.tsx b/src/components/High/TicketDisplay.tsx new file mode 100644 index 00000000..da4fad9e --- /dev/null +++ b/src/components/High/TicketDisplay.tsx @@ -0,0 +1,253 @@ +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 axios from "axios"; +import moment from "moment"; +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"; + +interface Props { + 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); + + const { users } = useUsers(); + + 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)); + }; + + 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)); + }; + + 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..." + /> +
+
+ +
+ + 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 + /> +
+ +