ff-admin/src/stores/admin/protocolDecision.ts

83 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-10-13 15:47:52 +02:00
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type {
ProtocolDecisionViewModel,
SyncProtocolDecisionViewModel,
} from "../../viewmodels/admin/protocolDecision.models";
import { useProtocolStore } from "./protocol";
2024-10-15 16:24:29 +02:00
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isEqual";
2024-10-18 14:20:58 +02:00
import differenceWith from "lodash.differencewith";
2024-10-13 15:47:52 +02:00
export const useProtocolDecisionStore = defineStore("protocolDecision", {
state: () => {
return {
decision: [] as Array<ProtocolDecisionViewModel>,
2024-10-18 14:20:58 +02:00
origin: [] as Array<ProtocolDecisionViewModel>,
2024-10-13 15:47:52 +02:00
loading: "loading" as "loading" | "fetched" | "failed",
2024-10-18 14:20:58 +02:00
syncingProtocolDecision: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
2024-10-13 15:47:52 +02:00
};
},
2024-10-18 14:20:58 +02:00
getters: {
detectedChangeProtocolDecision: (state) =>
!isEqual(state.origin, state.decision) && state.syncingProtocolDecision != "syncing",
},
2024-10-13 15:47:52 +02:00
actions: {
2024-10-18 14:20:58 +02:00
setProtocolDecisionSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingProtocolDecision = state;
},
2024-10-13 15:47:52 +02:00
fetchProtocolDecision() {
this.loading = "loading";
2024-10-18 14:20:58 +02:00
this.fetchProtocolDecisionPromise()
2024-10-13 15:47:52 +02:00
.then((result) => {
2024-10-18 14:20:58 +02:00
this.origin = result.data;
this.decision = cloneDeep(this.origin);
2024-10-13 15:47:52 +02:00
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
2024-10-18 14:20:58 +02:00
fetchProtocolDecisionPromise() {
const protocolId = useProtocolStore().activeProtocol;
return http.get(`/admin/protocol/${protocolId}/decisions`);
},
createProtocolDecision() {
const protocolId = useProtocolStore().activeProtocol;
if (protocolId == null) return;
return http
.post(`/admin/protocol/${protocolId}/decision`)
.then((res) => {
this.decision.push({
id: res.data,
topic: "",
context: "",
protocolId: Number(protocolId),
});
})
.catch((err) => {});
},
async synchronizeActiveProtocolDecision() {
this.syncingProtocolDecision = "syncing";
2024-10-13 15:47:52 +02:00
const protocolId = useProtocolStore().activeProtocol;
2024-10-18 14:20:58 +02:00
await http
.patch(`/admin/protocol/${protocolId}/synchronize/decisions`, {
decisions: differenceWith(this.decision, this.origin, isEqual),
})
.then((res) => {
this.syncingProtocolDecision = "synced";
})
.catch((err) => {
this.syncingProtocolDecision = "failed";
});
this.fetchProtocolDecisionPromise()
.then((res) => {
this.origin = res.data;
})
.catch((err) => {});
2024-10-13 15:47:52 +02:00
},
},
});