2024-10-03 13:43:13 +02:00
|
|
|
import { defineStore } from "pinia";
|
2024-10-13 15:47:52 +02:00
|
|
|
import type { CreateProtocolViewModel, SyncProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
2024-10-03 13:43:13 +02:00
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { AxiosResponse } from "axios";
|
|
|
|
import type { ProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
2024-10-14 17:03:48 +02:00
|
|
|
import cloneDeep from "lodash.clonedeep";
|
|
|
|
import isEqual from "lodash.isEqual";
|
|
|
|
import difference from "lodash.difference";
|
2024-10-03 13:43:13 +02:00
|
|
|
|
|
|
|
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,
|
2024-10-14 17:03:48 +02:00
|
|
|
origin: null as ProtocolViewModel | null,
|
2024-10-03 13:43:13 +02:00
|
|
|
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
2024-10-14 17:03:48 +02:00
|
|
|
syncingProtocol: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
|
2024-10-03 13:43:13 +02:00
|
|
|
};
|
|
|
|
},
|
2024-10-14 17:03:48 +02:00
|
|
|
getters: {
|
2024-10-15 16:24:29 +02:00
|
|
|
detectedChangeProtocol: (state) =>
|
|
|
|
!isEqual(state.origin, state.activeProtocolObj) && state.syncingProtocol != "syncing",
|
2024-10-14 17:03:48 +02:00
|
|
|
},
|
2024-10-03 13:43:13 +02:00
|
|
|
actions: {
|
2024-10-14 17:03:48 +02:00
|
|
|
setProtocolSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
|
|
|
|
this.syncingProtocol = state;
|
|
|
|
},
|
2024-10-03 13:43:13 +02:00
|
|
|
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) => {
|
2024-10-14 17:03:48 +02:00
|
|
|
this.origin = res.data;
|
|
|
|
this.activeProtocolObj = cloneDeep(this.origin);
|
2024-10-03 13:43:13 +02:00
|
|
|
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;
|
|
|
|
},
|
2024-10-14 17:03:48 +02:00
|
|
|
async synchronizeActiveProtocol(): Promise<void> {
|
|
|
|
if (this.origin == null || this.activeProtocolObj == null) return;
|
|
|
|
|
|
|
|
this.syncingProtocol = "syncing";
|
|
|
|
await http
|
2024-10-15 16:24:29 +02:00
|
|
|
.patch(`/admin/protocol/${this.origin.id}/synchronize`, {
|
2024-10-14 17:03:48 +02:00
|
|
|
title: this.activeProtocolObj.title,
|
|
|
|
date: this.activeProtocolObj.date,
|
|
|
|
starttime: this.activeProtocolObj.starttime,
|
|
|
|
endtime: this.activeProtocolObj.endtime,
|
|
|
|
summary: this.activeProtocolObj.summary,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
this.syncingProtocol = "synced";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.syncingProtocol = "failed";
|
|
|
|
});
|
|
|
|
this.fetchProtocolById(this.origin.id)
|
|
|
|
.then((res) => {
|
|
|
|
this.origin = res.data;
|
|
|
|
})
|
|
|
|
.catch((err) => {});
|
2024-10-03 13:43:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|