import { defineStore } from "pinia"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; import type { CreateInspectionViewModel, InspectionViewModel, UpdateInspectionViewModel, } from "@/viewmodels/admin/unit/inspection/inspection.models"; export const useInspectionStore = defineStore("inspection", { state: () => { return { activeInspection: null as string | null, activeInspectionObj: null as InspectionViewModel | null, loadingActive: "loading" as "loading" | "fetched" | "failed", }; }, actions: { fetchInspectionByActiveId() { this.loadingActive = "loading"; http .get(`/admin/inspection/${this.activeInspection}`) .then((res) => { this.activeInspectionObj = res.data; this.loadingActive = "fetched"; }) .catch((err) => { this.loadingActive = "failed"; }); }, fetchInspectionById(id: string) { return http.get(`/admin/inspection/${id}`); }, async createInspection(inspection: CreateInspectionViewModel): Promise> { const result = await http.post(`/admin/inspection`, { assigned: inspection.assigned, relatedId: inspection.relatedId, inspectionPlanId: inspection.inspectionPlanId, nextInspection: inspection.nextInspection, context: inspection.context, }); return result; }, async updateActiveInspection(inspection: UpdateInspectionViewModel): Promise> { const result = await http.patch(`/admin/inspection/${inspection.id}`, { nextInspection: inspection.nextInspection, context: inspection.context, }); this.fetchInspectionByActiveId(); return result; }, async deleteInspection(inspection: number): Promise> { const result = await http.delete(`/admin/inspection/${inspection}`); this.fetchInspectionByActiveId(); return result; }, }, });