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

82 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-13 15:47:52 +02:00
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type {
ProtocolAgendaViewModel,
SyncProtocolAgendaViewModel,
} from "../../viewmodels/admin/protocolAgenda.models";
import { useProtocolStore } from "./protocol";
2024-10-15 16:24:29 +02:00
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isEqual";
import differenceWith from "lodash.differencewith";
2024-10-13 15:47:52 +02:00
export const useProtocolAgendaStore = defineStore("protocolAgenda", {
state: () => {
return {
agenda: [] as Array<ProtocolAgendaViewModel>,
2024-10-15 16:24:29 +02:00
origin: [] as Array<ProtocolAgendaViewModel>,
2024-10-13 15:47:52 +02:00
loading: "loading" as "loading" | "fetched" | "failed",
2024-10-15 16:24:29 +02:00
syncingProtocolAgenda: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
2024-10-13 15:47:52 +02:00
};
},
2024-10-15 16:24:29 +02:00
getters: {
detectedChangeProtocolAgenda: (state) =>
!isEqual(state.origin, state.agenda) && state.syncingProtocolAgenda != "syncing",
},
2024-10-13 15:47:52 +02:00
actions: {
2024-10-15 16:24:29 +02:00
setProtocolAgendaSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingProtocolAgenda = state;
},
2024-10-13 15:47:52 +02:00
fetchProtocolAgenda() {
this.loading = "loading";
2024-10-15 16:24:29 +02:00
this.fetchProtocolAgendaPromise()
2024-10-13 15:47:52 +02:00
.then((result) => {
2024-10-15 16:24:29 +02:00
this.origin = result.data;
this.agenda = cloneDeep(this.origin);
2024-10-13 15:47:52 +02:00
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
2024-10-15 16:24:29 +02:00
fetchProtocolAgendaPromise() {
const protocolId = useProtocolStore().activeProtocol;
return http.get(`/admin/protocol/${protocolId}/agenda`);
},
createProtocolAgenda() {
const protocolId = useProtocolStore().activeProtocol;
if (protocolId == null) return;
return http
.post(`/admin/protocol/${protocolId}/agenda`)
.then((res) => {
this.agenda.push({
id: res.data,
topic: "",
context: "",
2024-10-18 14:20:58 +02:00
protocolId: Number(protocolId),
2024-10-15 16:24:29 +02:00
});
})
.catch((err) => {});
},
async synchronizeActiveProtocolAgenda() {
this.syncingProtocolAgenda = "syncing";
2024-10-13 15:47:52 +02:00
const protocolId = useProtocolStore().activeProtocol;
2024-10-15 16:24:29 +02:00
await http
.patch(`/admin/protocol/${protocolId}/synchronize/agenda`, {
agenda: differenceWith(this.agenda, this.origin, isEqual),
})
.then((res) => {
this.syncingProtocolAgenda = "synced";
})
.catch((err) => {
this.syncingProtocolAgenda = "failed";
});
this.fetchProtocolAgendaPromise()
.then((res) => {
this.origin = res.data;
})
.catch((err) => {});
2024-10-13 15:47:52 +02:00
},
},
});