import { defineStore } from "pinia"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; import type { ForceViewModel, CreateForceViewModel, UpdateForceViewModel, } from "../../../viewmodels/admin/configuration/force.models"; export const useForceStore = defineStore("force", { state: () => { return { forces: [] as Array, totalCount: 0 as number, loading: "loading" as "loading" | "fetched" | "failed", }; }, actions: { fetchForces(offset = 0, count = 25, search = "", clear = false) { if (clear) this.forces = []; this.loading = "loading"; http .get(`/admin/force?offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`) .then((result) => { this.totalCount = result.data.total; result.data.forces .filter((elem: ForceViewModel) => this.forces.findIndex((m) => m.id == elem.id) == -1) .map((elem: ForceViewModel, index: number): ForceViewModel & { tab_pos: number } => { return { ...elem, tab_pos: index + offset, }; }) .forEach((elem: ForceViewModel & { tab_pos: number }) => { this.forces.push(elem); }); this.loading = "fetched"; }) .catch((err) => { this.loading = "failed"; }); }, async getAllForces(): Promise> { return await http.get(`/admin/force?noLimit=true`).then((res) => { return { ...res, data: res.data.forces }; }); }, async getForcesByIds(ids: Array): Promise> { return await http .post(`/admin/force/ids`, { ids, }) .then((res) => { return { ...res, data: res.data.forces }; }); }, async searchForces(search: string): Promise> { return await http.get(`/admin/force?search=${search}&noLimit=true`).then((res) => { return { ...res, data: res.data.forces }; }); }, fetchForceById(id: string) { return http.get(`/admin/force/${id}`); }, async createForce(force: CreateForceViewModel): Promise> { const result = await http.post(`/admin/force`, { firstname: force.firstname, lastname: force.lastname, nameaffix: force.nameaffix, }); this.fetchForces(); return result; }, async updateActiveForce(force: UpdateForceViewModel): Promise> { const result = await http.patch(`/admin/force/${force.id}`, { firstname: force.firstname, lastname: force.lastname, nameaffix: force.nameaffix, }); this.fetchForces(); return result; }, async deleteForce(force: number): Promise> { const result = await http.delete(`/admin/force/${force}`); this.fetchForces(); return result; }, }, });