ticket list + create ticket
This commit is contained in:
@@ -1,14 +1,37 @@
|
||||
import * as api from "../service/TicketService";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { type AddMessageTicketType, type CreateTicketType } from "../types/TicketTypes";
|
||||
import { type AddMessageTicketType, type CreateTicketPayload, type CreateTicketType } from "../types/TicketTypes";
|
||||
|
||||
export const useGetTickets = (status: string) => {
|
||||
export const useGetTickets = (status?: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["tickets", status],
|
||||
queryKey: ["public-tickets", status ?? ""],
|
||||
queryFn: () => api.getTickets(status),
|
||||
});
|
||||
};
|
||||
|
||||
/** جزئیات یک تیکت از GET /public/tickets/{id} */
|
||||
export const useGetTicketById = (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["public-ticket", id],
|
||||
queryFn: () => api.getTicketById(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
};
|
||||
|
||||
/** ایجاد تیکت جدید (بدون parentId) یا ارسال پیام (با parentId) */
|
||||
export const useCreateOrReplyTicket = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (variables: CreateTicketPayload) => api.createOrReplyTicket(variables),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["public-tickets"] });
|
||||
if (variables.parentId) {
|
||||
queryClient.invalidateQueries({ queryKey: ["public-ticket", variables.parentId] });
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateTicket = () => {
|
||||
return useMutation({
|
||||
mutationFn: (variables: CreateTicketType) => api.createTicket(variables),
|
||||
|
||||
Reference in New Issue
Block a user