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

66 lines
2.2 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 {
ProtocolPresenceViewModel,
SyncProtocolPresenceViewModel,
} from "../../viewmodels/admin/protocolPresence.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-13 15:47:52 +02:00
export const useProtocolPresenceStore = defineStore("protocolPresence", {
state: () => {
return {
2024-10-15 16:24:29 +02:00
presence: [] as Array<number>,
origin: [] as Array<number>,
2024-10-13 15:47:52 +02:00
loading: "loading" as "loading" | "fetched" | "failed",
2024-10-15 16:24:29 +02:00
syncingProtocolPresence: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
2024-10-13 15:47:52 +02:00
};
},
2024-10-15 16:24:29 +02:00
getters: {
detectedChangeProtocolPresence: (state) =>
!isEqual(state.origin, state.presence) && state.syncingProtocolPresence != "syncing",
},
2024-10-13 15:47:52 +02:00
actions: {
2024-10-15 16:24:29 +02:00
setProtocolPresenceSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingProtocolPresence = state;
},
2024-10-13 15:47:52 +02:00
fetchProtocolPresence() {
this.loading = "loading";
2024-10-15 16:24:29 +02:00
this.fetchProtocolPresencePromise()
2024-10-13 15:47:52 +02:00
.then((result) => {
2024-10-15 16:24:29 +02:00
this.origin = result.data.map((d: ProtocolPresenceViewModel) => d.memberId);
this.presence = 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
fetchProtocolPresencePromise() {
2024-10-13 15:47:52 +02:00
const protocolId = useProtocolStore().activeProtocol;
2024-10-15 16:24:29 +02:00
return http.get(`/admin/protocol/${protocolId}/presence`);
},
async synchronizeActiveProtocolPresence() {
this.syncingProtocolPresence = "syncing";
const protocolId = useProtocolStore().activeProtocol;
await http
.put(`/admin/protocol/${protocolId}/synchronize/presence`, {
presence: this.presence,
})
.then((res) => {
this.syncingProtocolPresence = "synced";
})
.catch((err) => {
this.syncingProtocolPresence = "failed";
});
this.fetchProtocolPresencePromise()
.then((result) => {
this.origin = result.data.map((d: ProtocolPresenceViewModel) => d.memberId);
})
.catch((err) => {});
2024-10-13 15:47:52 +02:00
},
},
});