89 lines
3 KiB
TypeScript
89 lines
3 KiB
TypeScript
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<ForceViewModel & { tab_pos: number }>,
|
|
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<AxiosResponse<any, any>> {
|
|
return await http.get(`/admin/force?noLimit=true`).then((res) => {
|
|
return { ...res, data: res.data.forces };
|
|
});
|
|
},
|
|
async getForcesByIds(ids: Array<string>): Promise<AxiosResponse<any, any>> {
|
|
return await http
|
|
.post(`/admin/force/ids`, {
|
|
ids,
|
|
})
|
|
.then((res) => {
|
|
return { ...res, data: res.data.forces };
|
|
});
|
|
},
|
|
async searchForces(search: string): Promise<AxiosResponse<any, any>> {
|
|
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<AxiosResponse<any, any>> {
|
|
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<AxiosResponse<any, any>> {
|
|
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<AxiosResponse<any, any>> {
|
|
const result = await http.delete(`/admin/force/${force}`);
|
|
this.fetchForces();
|
|
return result;
|
|
},
|
|
},
|
|
});
|