107 lines
3.7 KiB
TypeScript
107 lines
3.7 KiB
TypeScript
|
import { defineStore } from "pinia";
|
||
|
import type { RepairViewModel, UpdateRepairViewModel } from "@/viewmodels/admin/unit/repair.models";
|
||
|
import { http } from "@/serverCom";
|
||
|
import type { AxiosResponse } from "axios";
|
||
|
|
||
|
export const useRepairStore = defineStore("repair", {
|
||
|
state: () => {
|
||
|
return {
|
||
|
repairs: [] as Array<RepairViewModel & { tab_pos: number }>,
|
||
|
totalCount: 0 as number,
|
||
|
loading: "loading" as "loading" | "fetched" | "failed",
|
||
|
activeRepair: null as string | null,
|
||
|
activeRepairObj: null as RepairViewModel | null,
|
||
|
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
||
|
};
|
||
|
},
|
||
|
actions: {
|
||
|
formatQueryReturnToPagination(result: AxiosResponse<any, any>, offset: number) {
|
||
|
this.totalCount = result.data.total;
|
||
|
result.data.repairs
|
||
|
.filter((elem: RepairViewModel) => this.repairs.findIndex((m) => m.id == elem.id) == -1)
|
||
|
.map((elem: RepairViewModel, index: number): RepairViewModel & { tab_pos: number } => {
|
||
|
return {
|
||
|
...elem,
|
||
|
tab_pos: index + offset,
|
||
|
};
|
||
|
})
|
||
|
.forEach((elem: RepairViewModel & { tab_pos: number }) => {
|
||
|
this.repairs.push(elem);
|
||
|
});
|
||
|
},
|
||
|
fetchOpenRepairs(offset = 0, count = 25, search = "", clear = false) {
|
||
|
if (clear) this.repairs = [];
|
||
|
this.loading = "loading";
|
||
|
http
|
||
|
.get(`/admin/repair?done=false&offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
|
||
|
.then((result) => {
|
||
|
this.formatQueryReturnToPagination(result, offset);
|
||
|
this.loading = "fetched";
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
this.loading = "failed";
|
||
|
});
|
||
|
},
|
||
|
fetchDoneRepairs(offset = 0, count = 25, search = "", clear = false) {
|
||
|
if (clear) this.repairs = [];
|
||
|
this.loading = "loading";
|
||
|
http
|
||
|
.get(`/admin/repair?done=true&offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
|
||
|
.then((result) => {
|
||
|
this.formatQueryReturnToPagination(result, offset);
|
||
|
this.loading = "fetched";
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
this.loading = "failed";
|
||
|
});
|
||
|
},
|
||
|
async getAllRepairs(): Promise<AxiosResponse<any, any>> {
|
||
|
return await http.get(`/admin/repair?noLimit=true`).then((res) => {
|
||
|
return { ...res, data: res.data.repairs };
|
||
|
});
|
||
|
},
|
||
|
async getRepairsByIds(ids: Array<string>): Promise<AxiosResponse<any, any>> {
|
||
|
return await http
|
||
|
.post(`/admin/repair/ids`, {
|
||
|
ids,
|
||
|
})
|
||
|
.then((res) => {
|
||
|
return { ...res, data: res.data.repairs };
|
||
|
});
|
||
|
},
|
||
|
async searchRepairs(search: string): Promise<AxiosResponse<any, any>> {
|
||
|
return await http.get(`/admin/repair?search=${search}&noLimit=true`).then((res) => {
|
||
|
return { ...res, data: res.data.repairs };
|
||
|
});
|
||
|
},
|
||
|
fetchRepairByActiveId() {
|
||
|
this.loadingActive = "loading";
|
||
|
http
|
||
|
.get(`/admin/repair/${this.activeRepair}`)
|
||
|
.then((res) => {
|
||
|
this.activeRepairObj = res.data;
|
||
|
this.loadingActive = "fetched";
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
this.loadingActive = "failed";
|
||
|
});
|
||
|
},
|
||
|
fetchRepairById(id: string) {
|
||
|
return http.get(`/admin/repair/${id}`);
|
||
|
},
|
||
|
loadRepairImage(url: string) {
|
||
|
return http.get(`/admin/repair/${this.activeRepairObj?.id}/${url}`, {
|
||
|
responseType: "blob",
|
||
|
});
|
||
|
},
|
||
|
async updateRepair(repair: UpdateRepairViewModel): Promise<AxiosResponse<any, any>> {
|
||
|
const result = await http.patch(`/admin/repair/${repair.id}`, {
|
||
|
status: repair.status,
|
||
|
noteByWorker: repair.noteByWorker,
|
||
|
done: repair.done,
|
||
|
});
|
||
|
return result;
|
||
|
},
|
||
|
},
|
||
|
});
|