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 />