diff --git a/app/(panel)/tickets/update/[id]/page.tsx b/app/(panel)/tickets/update/[id]/page.tsx index 0dc52bf..30f63cd 100644 --- a/app/(panel)/tickets/update/[id]/page.tsx +++ b/app/(panel)/tickets/update/[id]/page.tsx @@ -1,11 +1,19 @@ import { API_URL } from "@/core/constant"; +import UpdateTicketForm from "@/ui/form/UpdateTicketForm"; +import { cookies } from "next/headers"; interface PageProps { params: Promise<{ id: string }>; } async function getTicket(id: string) { + const cookieStore = await cookies(); + const tokenCookie = cookieStore.get("userToken"); // گرفتن کوکی از درخواست کاربر const res = await fetch(`${API_URL}/ticket/get/${id}`, { - cache: "no-store", // برای اینکه همیشه دیتای تازه از سرور بگیرد + cache: "no-store", + credentials: "include", + headers: { + Cookie: `${tokenCookie?.name}=${tokenCookie?.value}`, + }, }); if (!res.ok) { throw new Error("Error"); @@ -15,9 +23,63 @@ async function getTicket(id: string) { return data; } +async function getDepartmentsData() { + const cookieStore = await cookies(); + const tokenCookie = cookieStore.get("userToken"); // گرفتن کوکی از درخواست کاربر + + const res = await fetch(`${API_URL}/department/all`, { + cache: "no-cache", + credentials: "include", + headers: { + "Content-Type": "application/json", + Cookie: `${tokenCookie?.name}=${tokenCookie?.value}`, + }, + }); + + if (!res.ok) { + throw new Error("خطا در واكشي ديتاي واحد ها"); + } + + const data = await res.json(); + + return data; +} + +async function getUsersData() { + const cookieStore = await cookies(); + const tokenCookie = cookieStore.get("userToken"); // گرفتن کوکی از درخواست کاربر + const res = await fetch(`${API_URL}/user/all`, { + cache: "no-cache", + credentials: "include", + headers: { + "Content-Type": "application/json", + Cookie: `${tokenCookie?.name}=${tokenCookie?.value}`, + }, + }); + + if (!res.ok) { + throw new Error("خطا در واكشي ديتاي واحد ها"); + } + + const data = await res.json(); + + return data; +} + export default async function Page({ params }: PageProps) { const { id } = await params; const getdata = await getTicket(id); - console.log(getdata) - return
; + + const getdepartmentsdata = await getDepartmentsData(); + const getusersdata = await getUsersData(); + + const departments = getdepartmentsdata?.data; + const users = getusersdata?.data; + + const selectedTicket = getdata?.data; + return ( + <> + {selectedTicket ? : ""} + + ); } diff --git a/services/api/ticket.api.ts b/services/api/ticket.api.ts index 8453396..9272f54 100644 --- a/services/api/ticket.api.ts +++ b/services/api/ticket.api.ts @@ -4,6 +4,10 @@ export async function createTicket(data: any) { return await callAPI.post("/ticket/create", data).then((res) => res.data); } +export async function updateTicket(id:string,data: any) { + return await callAPI.put(`/ticket/update/${id}`, data).then((res) => res.data); +} + export async function getAllTickets(params: any) { return await callAPI.get("/ticket/all", { params }).then((res) => res.data); } diff --git a/ui/form/TicketForm.tsx b/ui/form/TicketForm.tsx index a484aad..4ad52d0 100644 --- a/ui/form/TicketForm.tsx +++ b/ui/form/TicketForm.tsx @@ -227,7 +227,6 @@ export default function TicketForm({ ))} -
diff --git a/ui/form/UpdateTicketForm.tsx b/ui/form/UpdateTicketForm.tsx new file mode 100644 index 0000000..21f0db7 --- /dev/null +++ b/ui/form/UpdateTicketForm.tsx @@ -0,0 +1,262 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { TextField, MenuItem, Button } from "@mui/material"; +import { + hospitalSoftwares, + requestType, + ticketPriorities, + ticketStatuses, +} from "@/core/constant"; +import { toast } from "react-toastify"; +import { handleAxiosError } from "@/core/utils"; +import { createTicket, updateTicket } from "@/services/api/ticket.api"; +import { TicketInterface } from "@/core/types"; +interface DepartmentType { + id: string; + slug: string; + displayName: string; +} +export interface IUserListItem { + id: string; + fullname: string; + nationalCode: string; + mobile: string; + roleId: string; + createdAt: Date; + updatedAt: Date; +} + +export default function UpdateTicketForm({ + departments, + users, + ticket +}: { + departments: DepartmentType[]; + users: IUserListItem[]; + ticket:TicketInterface +}) { + const [showFieldRelatedSystem, setShowFieldRelatedSystem] = useState(false); + const [form, setForm] = useState({ + createdBy: ticket?.createdBy, + departmentId: ticket?.departmentId, + internalPhone: ticket?.internalPhone, + requestType: ticket?.requestType, + priority: ticket?.priority ?? "medium", + description: ticket?.description, + relatedSystem: ticket?.relatedSystem, + location: ticket.location, + helpdeskAction: ticket.helpdeskAction, + assignedTo: ticket?.assignedTo, + status: ticket?.status, + finalNotes: ticket?.finalNotes, + }); + + const handleChange = (e: any) => { + setForm({ + ...form, + [e.target.name]: e.target.value, + }); + }; + + const handleSubmit = async (e: any) => { + e.preventDefault(); + + try { + const { message } = await updateTicket(ticket.id,form); + toast.success(message); + } catch (error) { + toast.error(handleAxiosError(error)); + } + }; + + useEffect(() => { + if (form.requestType === "1") { + form.relatedSystem = ""; + setShowFieldRelatedSystem(true); + } else { + form.relatedSystem = ""; + setShowFieldRelatedSystem(false); + } + }, [form.requestType]); + return ( +
+
+
فرم ويرايش تيكت
+ +
+
+
+ + + {departments.map((dep) => ( + + {dep.displayName} + + ))} + + +
+ +
+ + {requestType.map((dep) => ( + + {dep.displayName} + + ))} + + {showFieldRelatedSystem && ( + + {hospitalSoftwares.map((item) => ( + {item.displayName} + ))} + + )} + +
+ +
+ + +
+ +
+ + {users.map((dep) => ( + + {dep.fullname} + + ))} + + + {ticketPriorities?.map((p: any) => ( + + {p.displayName} + + ))} + + + {ticketStatuses?.map((p: any) => ( + + {p.displayName} + + ))} + +
+ +
+ +
+ + +
+
+
+
+ ); +}