2025-06-20 02:33:53 +08:00

86 lines
2.5 KiB
TypeScript

import { Ticket } from '@/pages/define/type';
import { create } from 'zustand';
import { query } from '@/modules/query';
import { toast } from 'react-toastify';
type SearchTicketsParams = {
current?: number;
pageSize?: number;
search?: string;
status?: string;
type?: string;
sort?: string;
order?: 'asc' | 'desc';
uid?: string;
startDate?: string;
endDate?: string;
};
type TicketStore = {
tickets: Ticket[];
getTickets: (opts: SearchTicketsParams) => Promise<void>;
updateTicket: (ticket: Ticket) => Promise<void>;
deleteTicket: (id: string) => Promise<void>;
showEdit: boolean;
setShowEdit: (show: boolean) => void;
formData: Ticket | null;
setFormData: (data: Ticket | null) => void;
loading: boolean;
setLoading: (loading: boolean) => void;
searchForm: Partial<SearchTicketsParams>;
setSearchForm: (form: Partial<SearchTicketsParams>) => void;
pagination: {
current?: number;
total?: number;
};
setPagination: (pagination: { current?: number; total?: number }) => void;
};
export const useTicketStore = create<TicketStore>((set, get) => {
return {
tickets: [],
searchForm: { current: 1, pageSize: 10 },
setSearchForm: (form) => set({ searchForm: form }),
pagination: { current: 1, total: 0 },
setPagination: (pagination) => set({ pagination }),
getTickets: async (data) => {
const res = await query.post({ path: 'ticket', key: 'list', data });
if (res.code === 200) {
const pagination = res.data.pagination || {};
set({ tickets: res.data.list, pagination });
}
},
updateTicket: async (ticket: Ticket) => {
const res = await query.post({
path: 'ticket',
key: 'update',
data: ticket,
});
if (res.code === 200) {
toast.success('Ticket updated successfully');
// get().getTickets(get().searchForm);
} else {
toast.error('Failed to update ticket');
}
},
deleteTicket: async (id: string) => {
const res = await query.post({
path: 'ticket',
key: 'delete',
data: { id },
});
if (res.code === 200) {
toast.success('删除成功');
// Refresh the ticket list after deletion
get().getTickets(get().searchForm);
} else {
toast.error('删除失败');
}
},
showEdit: false,
setShowEdit: (show) => set({ showEdit: show }),
formData: null,
setFormData: (data) => set({ formData: data }),
loading: false,
setLoading: (loading) => set({ loading }),
};
});