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

91 lines
2.9 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/club/protocol/protocolAgenda.models";
2024-10-13 15:47:52 +02:00
import { useProtocolStore } from "./protocol";
2024-10-15 16:24:29 +02:00
import cloneDeep from "lodash.clonedeep";
2024-11-27 17:06:39 +01:00
import isEqual from "lodash.isequal";
2024-10-15 16:24:29 +02:00
import differenceWith from "lodash.differencewith";
2024-10-13 15:47:52 +02:00
export const useProtocolAgendaStore = defineStore("protocolAgenda", {
state: () => {
return {
initialized: false as boolean,
2024-10-13 15:47:52 +02:00
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) =>
2025-03-24 09:19:32 +01:00
!isEqual(
state.origin.sort((a, b) => a.id - b.id),
state.agenda.sort((a, b) => a.id - b.id)
) && state.syncingProtocolAgenda != "syncing",
2024-10-15 16:24:29 +02:00
},
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);
this.initialized = true;
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({
2024-10-29 15:23:35 +01:00
id: Number(res.data),
2024-10-15 16:24:29 +02:00
topic: "",
context: "",
2025-03-21 10:30:55 +01:00
sort: this.agenda.length,
2024-10-18 14:20:58 +02:00
protocolId: Number(protocolId),
2024-10-15 16:24:29 +02:00
});
})
.catch((err) => {});
},
async synchronizeActiveProtocolAgenda() {
if (!this.initialized) return;
2024-10-15 16:24:29 +02:00
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;
2024-10-29 15:23:35 +01:00
if (this.detectedChangeProtocolAgenda) this.syncingProtocolAgenda = "detectedChanges";
2024-10-15 16:24:29 +02:00
})
.catch((err) => {});
2024-10-13 15:47:52 +02:00
},
},
});