import { defineStore } from "pinia"; import type { MissionShortViewModel } from "@/viewmodels/admin/operation/mission.models"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; export const useMissionStore = defineStore("mission", { state: () => { return { missions: [] as Array, totalCount: 0 as number, loading: "loading" as "loading" | "fetched" | "failed", }; }, actions: { fetchMissions(offset = 0, count = 25, clear = false) { if (clear) this.missions = []; this.loading = "loading"; http .get(`/admin/mission?offset=${offset}&count=${count}`) .then((result) => { this.totalCount = result.data.total; result.data.missions .filter((elem: MissionShortViewModel) => this.missions.findIndex((m) => m.id == elem.id) == -1) .forEach((elem: MissionShortViewModel) => { this.missions.push(elem); }); this.loading = "fetched"; }) .catch((err) => { this.loading = "failed"; }); }, prependMission(mission: MissionShortViewModel) { this.missions.unshift(mission); }, async createMission(): Promise> { const result = await http.post(`/admin/mission`); this.fetchMissions(0, 25, true); return result; }, }, });