55 lines
2 KiB
TypeScript
55 lines
2 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import type { MissionShortViewModel } from "@/viewmodels/admin/operation/mission.models";
|
|
import { http } from "@/serverCom";
|
|
import type { AxiosResponse } from "axios";
|
|
import { useConnectionStore } from "./connection";
|
|
import { useNotificationStore } from "../../notification";
|
|
|
|
export const useMissionStore = defineStore("mission", {
|
|
state: () => {
|
|
return {
|
|
missions: [] as Array<MissionShortViewModel>,
|
|
totalCount: 0 as number,
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
|
};
|
|
},
|
|
actions: {
|
|
initialize() {
|
|
const connectionStore = useConnectionStore();
|
|
connectionStore.connection?.on("created-new-mission", (data) => {
|
|
const notificationStore = useNotificationStore();
|
|
notificationStore.push("Neuer Einsatz", `Es wurde ein neuer Einsatz angelegt`, "info");
|
|
this.fetchMissions(0, 25, true);
|
|
});
|
|
},
|
|
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";
|
|
});
|
|
},
|
|
fetchMissionById(id: string): Promise<AxiosResponse<any, any>> {
|
|
return http.get(`/admin/mission/${id}`);
|
|
},
|
|
prependMission(mission: MissionShortViewModel) {
|
|
this.missions.unshift(mission);
|
|
},
|
|
async createMission(): Promise<AxiosResponse<any, any>> {
|
|
const result = await http.post(`/admin/mission`);
|
|
this.fetchMissions(0, 25, true);
|
|
return result;
|
|
},
|
|
},
|
|
});
|