diff --git a/src/stores/mocks/tickets.ts b/src/stores/mocks/tickets.ts new file mode 100644 index 0000000..7af305b --- /dev/null +++ b/src/stores/mocks/tickets.ts @@ -0,0 +1,49 @@ +import { Ticket } from "@root/model/ticket"; + + +export const testTickets: Ticket[] = [ + { + "id": "cg5irh4vc9g7b3n3tcrg", + "user": "6407625ed01874dcffa8b008", + "sess": "6407625ed01874dcffa8b008", + "ans": "", + "state": "open", + "top_message": { + "id": "cg5irh4vc9g7b3n3tcs0", + "ticket_id": "cg5irh4vc9g7b3n3tcrg", + "user_id": "6407625ed01874dcffa8b008", + "session_id": "6407625ed01874dcffa8b008", + "message": "text", + "files": [], + "shown": {}, + "request_screenshot": "", + "created_at": "2023-03-10T13:16:52.73Z" + }, + "title": "textual ticket", + "created_at": "2023-03-10T13:16:52.73Z", + "updated_at": "2023-03-10T13:16:52.73Z", + "rate": -1 + }, + { + "id": "cg55nssvc9g7gddpnsug", + "user": "", + "sess": "", + "ans": "", + "state": "open", + "top_message": { + "id": "cg55nssvc9g7gddpnsv0", + "ticket_id": "cg55nssvc9g7gddpnsug", + "user_id": "", + "session_id": "", + "message": "text", + "files": [], + "shown": {}, + "request_screenshot": "", + "created_at": "2023-03-09T22:21:39.822Z" + }, + "title": "textual ticket", + "created_at": "2023-03-09T22:21:39.822Z", + "updated_at": "2023-03-09T22:21:39.822Z", + "rate": -1 + } +]; \ No newline at end of file diff --git a/src/stores/tickets.ts b/src/stores/tickets.ts new file mode 100644 index 0000000..f003498 --- /dev/null +++ b/src/stores/tickets.ts @@ -0,0 +1,34 @@ +import { Ticket } from "@root/model/ticket"; +import { create } from "zustand"; +import { devtools } from "zustand/middleware"; + + +interface TicketStore { + tickets: Ticket[]; +} + +export const useTicketStore = create()( + devtools( + (set, get) => ({ + tickets: [], + }), + { + name: "Tickets store" + } + ) +); + +export const setTickets = (tickets: Ticket[]) => useTicketStore.setState(({ tickets })); +export const addTickets = (tickets: Ticket[]) => useTicketStore.setState(state => ({ tickets: [...state.tickets, ...tickets] })); + +export const addOrUpdateTicket = (updatedTicket: Ticket) => { + const state = useTicketStore.getState(); + const ticketIndex = state.tickets.findIndex(ticket => ticket.id === updatedTicket.id); + + if (ticketIndex === -1) { + return useTicketStore.setState({ tickets: [...state.tickets, updatedTicket] }); + } + + const newTickets = state.tickets.slice().splice(ticketIndex, 1, updatedTicket); + useTicketStore.setState({ tickets: newTickets }); +}; \ No newline at end of file