import { defineStore } from "pinia"; import type { CreateCalendarViewModel, UpdateCalendarViewModel, CalendarViewModel, } from "@/viewmodels/admin/calendar.models"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; export const useCalendarStore = defineStore("calendar", { state: () => { return { calendars: [] as Array, loading: "loading" as "loading" | "fetched" | "failed", loadingFields: "loading" as "loading" | "fetched" | "failed", }; }, getters: { formattedItems: (state) => { return state.calendars.map((c) => ({ id: c.id, title: c.title, start: c.starttime, end: c.endtime, backgroundColor: c.type.color, })); }, }, actions: { fetchCalendars() { this.loading = "loading"; http .get("/admin/calendar/items") .then((result) => { this.calendars = result.data; this.loading = "fetched"; }) .catch((err) => { this.loading = "failed"; }); }, fetchCalendarById(id: string): Promise> { return http.get(`/admin/calendar/item/${id}`); }, async createCalendar(calendar: CreateCalendarViewModel): Promise> { const result = await http.post(`/admin/calendar/item`, { starttime: calendar.starttime, endtime: calendar.endtime, title: calendar.title, content: calendar.content, allDay: calendar.allDay, location: calendar.location, typeId: calendar.typeId, }); this.fetchCalendars(); return result; }, async updateCalendar(calendar: UpdateCalendarViewModel): Promise> { const result = await http.patch(`/admin/calendar/item/${calendar.id}`, { starttime: calendar.starttime, endtime: calendar.endtime, title: calendar.title, content: calendar.content, allDay: calendar.allDay, location: calendar.location, typeId: calendar.typeId, }); this.fetchCalendars(); return result; }, async deleteCalendar(calendar: number): Promise> { const result = await http.delete(`/admin/calendar/item/${calendar}`); this.fetchCalendars(); return result; }, }, });