adminFront/src/stores/tickets.ts

28 lines
732 B
TypeScript
Raw Normal View History

2023-03-20 15:25:28 +00:00
import { Ticket } from "@root/model/ticket";
import { create } from "zustand";
import { devtools } from "zustand/middleware";
interface TicketStore {
tickets: Ticket[];
}
export const useTicketStore = create<TicketStore>()(
devtools(
(set, get) => ({
tickets: [],
}),
{
name: "Tickets store"
}
)
);
2023-03-22 11:43:19 +00:00
export const updateTickets = (receivedTickets: Ticket[]) => {
2023-03-20 15:25:28 +00:00
const state = useTicketStore.getState();
2023-03-22 11:43:19 +00:00
const ticketIdToTicketMap: { [ticketId: string]: Ticket; } = {};
2023-03-20 15:25:28 +00:00
2023-03-22 11:43:19 +00:00
[...state.tickets, ...receivedTickets].forEach(ticket => ticketIdToTicketMap[ticket.id] = ticket);
2023-03-20 15:25:28 +00:00
2023-03-22 11:43:19 +00:00
useTicketStore.setState({ tickets: Object.values(ticketIdToTicketMap) });
2023-03-20 15:25:28 +00:00
};