2024-10-03 13:43:13 +02:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import type { CreateProtocolViewModel, UpdateProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { AxiosResponse } from "axios";
|
|
|
|
import type { ProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
|
|
|
|
|
|
|
export const useProtocolStore = defineStore("protocol", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
protocols: [] as Array<ProtocolViewModel & { tab_pos: number }>,
|
|
|
|
totalCount: 0 as number,
|
|
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
|
|
|
activeProtocol: null as number | null,
|
|
|
|
activeProtocolObj: null as ProtocolViewModel | null,
|
|
|
|
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
2024-10-04 12:47:04 +02:00
|
|
|
syncing: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
|
2024-10-03 13:43:13 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
fetchProtocols(offset = 0, count = 25, clear = false) {
|
|
|
|
if (clear) this.protocols = [];
|
|
|
|
this.loading = "loading";
|
|
|
|
http
|
|
|
|
.get(`/admin/protocol?offset=${offset}&count=${count}`)
|
|
|
|
.then((result) => {
|
|
|
|
this.totalCount = result.data.total;
|
|
|
|
result.data.protocols
|
|
|
|
.filter((elem: ProtocolViewModel) => this.protocols.findIndex((m) => m.id == elem.id) == -1)
|
|
|
|
.map((elem: ProtocolViewModel, index: number): ProtocolViewModel & { tab_pos: number } => {
|
|
|
|
return {
|
|
|
|
...elem,
|
|
|
|
tab_pos: index + offset,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.forEach((elem: ProtocolViewModel & { tab_pos: number }) => {
|
|
|
|
this.protocols.push(elem);
|
|
|
|
});
|
|
|
|
this.loading = "fetched";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.loading = "failed";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fetchProtocolByActiveId() {
|
|
|
|
this.loadingActive = "loading";
|
|
|
|
http
|
|
|
|
.get(`/admin/protocol/${this.activeProtocol}`)
|
|
|
|
.then((res) => {
|
|
|
|
this.activeProtocolObj = res.data;
|
|
|
|
this.loadingActive = "fetched";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.loadingActive = "failed";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fetchProtocolById(id: number) {
|
|
|
|
return http.get(`/admin/protocol/${id}`);
|
|
|
|
},
|
|
|
|
async createProtocol(protocol: CreateProtocolViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.post(`/admin/protocol`, {
|
|
|
|
title: protocol.title,
|
|
|
|
date: protocol.date,
|
|
|
|
});
|
|
|
|
this.fetchProtocols();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
async updateActiveProtocol(protocol: UpdateProtocolViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.patch(`/admin/protocol/${protocol.id}`, {
|
|
|
|
title: protocol.title,
|
|
|
|
date: protocol.date,
|
|
|
|
});
|
|
|
|
this.fetchProtocols();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
async deleteProtocol(protocol: number): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.delete(`/admin/protocol/${protocol}`);
|
|
|
|
this.fetchProtocols();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|