import { defineStore } from "pinia"; import type { EquipmentViewModel, CreateEquipmentViewModel, UpdateEquipmentViewModel, } from "@/viewmodels/admin/unit/equipment/equipment.models"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; import { equipmentDemoData } from "../../../../demodata/equipment"; export const useEquipmentStore = defineStore("equipment", { state: () => { return { equipments: [] as Array, totalCount: 0 as number, loading: "loading" as "loading" | "fetched" | "failed", activeEquipment: null as string | null, activeEquipmentObj: null as EquipmentViewModel | null, loadingActive: "loading" as "loading" | "fetched" | "failed", }; }, actions: { fetchEquipments(offset = 0, count = 25, search = "", clear = false) { this.equipments = equipmentDemoData.map((e, i) => ({ ...e, tab_pos: i })); this.totalCount = this.equipments.length; this.loading = "fetched"; return; if (clear) this.equipments = []; this.loading = "loading"; http .get(`/admin/equipment?offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`) .then((result) => { this.totalCount = result.data.total; result.data.equipments .filter((elem: EquipmentViewModel) => this.equipments.findIndex((m) => m.id == elem.id) == -1) .map((elem: EquipmentViewModel, index: number): EquipmentViewModel & { tab_pos: number } => { return { ...elem, tab_pos: index + offset, }; }) .forEach((elem: EquipmentViewModel & { tab_pos: number }) => { this.equipments.push(elem); }); this.loading = "fetched"; }) .catch((err) => { this.loading = "failed"; }); }, async getAllEquipments(): Promise> { return await http.get(`/admin/equipment?noLimit=true`).then((res) => { return { ...res, data: res.data.equipments }; }); }, async getEquipmentsByIds(ids: Array): Promise> { return await http .post(`/admin/equipment/ids`, { ids, }) .then((res) => { return { ...res, data: res.data.equipments }; }); }, async searchEquipments(search: string): Promise> { return await http.get(`/admin/equipment?search=${search}&noLimit=true`).then((res) => { return { ...res, data: res.data.equipments }; }); }, fetchEquipmentByActiveId() { this.activeEquipmentObj = equipmentDemoData.find((e) => e.id == this.activeEquipment) as EquipmentViewModel; this.loading = "fetched"; return; this.loadingActive = "loading"; http .get(`/admin/equipment/${this.activeEquipment}`) .then((res) => { this.activeEquipmentObj = res.data; this.loadingActive = "fetched"; }) .catch((err) => { this.loadingActive = "failed"; }); }, fetchEquipmentById(id: string) { return http.get(`/admin/equipment/${id}`); }, async createEquipment(equipment: CreateEquipmentViewModel): Promise> { const result = await http.post(`/admin/equipment`, { // TODO: data }); this.fetchEquipments(); return result; }, async updateActiveEquipment(equipment: UpdateEquipmentViewModel): Promise> { const result = await http.patch(`/admin/equipment/${equipment.id}`, { // TODO: data }); this.fetchEquipments(); return result; }, async deleteEquipment(equipment: number): Promise> { const result = await http.delete(`/admin/equipment/${equipment}`); this.fetchEquipments(); return result; }, }, });