41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { http } from "@/serverCom";
|
|
import type { InspectionViewModel } from "@/viewmodels/admin/unit/inspection/inspection.models";
|
|
import { useEquipmentStore } from "./equipment";
|
|
|
|
export const useEquipmentInspectionStore = defineStore("equipmentInspection", {
|
|
state: () => {
|
|
return {
|
|
inspections: [] as Array<InspectionViewModel & { tab_pos: number }>,
|
|
totalCount: 0 as number,
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
|
};
|
|
},
|
|
actions: {
|
|
fetchInspectionForEquipment(offset = 0, count = 25, clear = false) {
|
|
const equipmentId = useEquipmentStore().activeEquipment;
|
|
if (clear) this.inspections = [];
|
|
this.loading = "loading";
|
|
http
|
|
.get(`/admin/inspection/equipment/${equipmentId}?offset=${offset}&count=${count}`)
|
|
.then((result) => {
|
|
this.totalCount = result.data.total;
|
|
result.data.inspections
|
|
.filter((elem: InspectionViewModel) => this.inspections.findIndex((m) => m.id == elem.id) == -1)
|
|
.map((elem: InspectionViewModel, index: number): InspectionViewModel & { tab_pos: number } => {
|
|
return {
|
|
...elem,
|
|
tab_pos: index + offset,
|
|
};
|
|
})
|
|
.forEach((elem: InspectionViewModel & { tab_pos: number }) => {
|
|
this.inspections.push(elem);
|
|
});
|
|
this.loading = "fetched";
|
|
})
|
|
.catch((err) => {
|
|
this.loading = "failed";
|
|
});
|
|
},
|
|
},
|
|
});
|