121 lines
4.6 KiB
TypeScript
121 lines
4.6 KiB
TypeScript
|
import { defineStore } from "pinia";
|
||
|
import type {
|
||
|
RespiratoryGearViewModel,
|
||
|
CreateRespiratoryGearViewModel,
|
||
|
UpdateRespiratoryGearViewModel,
|
||
|
} from "@/viewmodels/admin/unit/respiratoryGear/respiratoryGear.models";
|
||
|
import { http } from "@/serverCom";
|
||
|
import type { AxiosResponse } from "axios";
|
||
|
|
||
|
export const useRespiratoryGearStore = defineStore("respiratoryGear", {
|
||
|
state: () => {
|
||
|
return {
|
||
|
respiratoryGears: [] as Array<RespiratoryGearViewModel & { tab_pos: number }>,
|
||
|
totalCount: 0 as number,
|
||
|
loading: "loading" as "loading" | "fetched" | "failed",
|
||
|
activeRespiratoryGear: null as string | null,
|
||
|
activeRespiratoryGearObj: null as RespiratoryGearViewModel | null,
|
||
|
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
||
|
};
|
||
|
},
|
||
|
actions: {
|
||
|
fetchRespiratoryGears(offset = 0, count = 25, search = "", clear = false) {
|
||
|
if (clear) this.respiratoryGears = [];
|
||
|
this.loading = "loading";
|
||
|
http
|
||
|
.get(`/admin/respiratoryGear?offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
|
||
|
.then((result) => {
|
||
|
this.totalCount = result.data.total;
|
||
|
result.data.respiratoryGears
|
||
|
.filter((elem: RespiratoryGearViewModel) => this.respiratoryGears.findIndex((m) => m.id == elem.id) == -1)
|
||
|
.map((elem: RespiratoryGearViewModel, index: number): RespiratoryGearViewModel & { tab_pos: number } => {
|
||
|
return {
|
||
|
...elem,
|
||
|
tab_pos: index + offset,
|
||
|
};
|
||
|
})
|
||
|
.forEach((elem: RespiratoryGearViewModel & { tab_pos: number }) => {
|
||
|
this.respiratoryGears.push(elem);
|
||
|
});
|
||
|
this.loading = "fetched";
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
this.loading = "failed";
|
||
|
});
|
||
|
},
|
||
|
async getAllRespiratoryGears(): Promise<AxiosResponse<any, any>> {
|
||
|
return await http.get(`/admin/respiratoryGear?noLimit=true`).then((res) => {
|
||
|
return { ...res, data: res.data.respiratoryGears };
|
||
|
});
|
||
|
},
|
||
|
async getRespiratoryGearsByIds(ids: Array<string>): Promise<AxiosResponse<any, any>> {
|
||
|
return await http
|
||
|
.post(`/admin/respiratoryGear/ids`, {
|
||
|
ids,
|
||
|
})
|
||
|
.then((res) => {
|
||
|
return { ...res, data: res.data.respiratoryGears };
|
||
|
});
|
||
|
},
|
||
|
async searchRespiratoryGears(search: string): Promise<AxiosResponse<any, any>> {
|
||
|
return await http.get(`/admin/respiratoryGear?search=${search}&noLimit=true`).then((res) => {
|
||
|
return { ...res, data: res.data.respiratoryGears };
|
||
|
});
|
||
|
},
|
||
|
fetchRespiratoryGearByActiveId() {
|
||
|
this.loadingActive = "loading";
|
||
|
http
|
||
|
.get(`/admin/respiratoryGear/${this.activeRespiratoryGear}`)
|
||
|
.then((res) => {
|
||
|
this.activeRespiratoryGearObj = res.data;
|
||
|
this.loadingActive = "fetched";
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
this.loadingActive = "failed";
|
||
|
});
|
||
|
},
|
||
|
fetchRespiratoryGearById(id: string) {
|
||
|
return http.get(`/admin/respiratoryGear/${id}`);
|
||
|
},
|
||
|
async printRespiratoryGearByActiveId() {
|
||
|
return http.get(`/admin/respiratoryGear/${this.activeRespiratoryGear}/print`, {
|
||
|
responseType: "blob",
|
||
|
});
|
||
|
},
|
||
|
fetchRespiratoryGearStatisticsById(id: string) {
|
||
|
return http.get(`/admin/respiratoryGear/${id}/statistics`);
|
||
|
},
|
||
|
async createRespiratoryGear(respiratoryGear: CreateRespiratoryGearViewModel): Promise<AxiosResponse<any, any>> {
|
||
|
const result = await http.post(`/admin/respiratoryGear`, {
|
||
|
salutationId: respiratoryGear.salutationId,
|
||
|
firstname: respiratoryGear.firstname,
|
||
|
lastname: respiratoryGear.lastname,
|
||
|
nameaffix: respiratoryGear.nameaffix,
|
||
|
birthdate: respiratoryGear.birthdate,
|
||
|
internalId: respiratoryGear.internalId,
|
||
|
});
|
||
|
this.fetchRespiratoryGears();
|
||
|
return result;
|
||
|
},
|
||
|
async updateActiveRespiratoryGear(
|
||
|
respiratoryGear: UpdateRespiratoryGearViewModel
|
||
|
): Promise<AxiosResponse<any, any>> {
|
||
|
const result = await http.patch(`/admin/respiratoryGear/${respiratoryGear.id}`, {
|
||
|
salutationId: respiratoryGear.salutationId,
|
||
|
firstname: respiratoryGear.firstname,
|
||
|
lastname: respiratoryGear.lastname,
|
||
|
nameaffix: respiratoryGear.nameaffix,
|
||
|
birthdate: respiratoryGear.birthdate,
|
||
|
internalId: respiratoryGear.internalId,
|
||
|
});
|
||
|
this.fetchRespiratoryGears();
|
||
|
return result;
|
||
|
},
|
||
|
async deleteRespiratoryGear(respiratoryGear: number): Promise<AxiosResponse<any, any>> {
|
||
|
const result = await http.delete(`/admin/respiratoryGear/${respiratoryGear}`);
|
||
|
this.fetchRespiratoryGears();
|
||
|
return result;
|
||
|
},
|
||
|
},
|
||
|
});
|