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

84 lines
2.8 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";
2024-12-26 12:34:36 +01:00
import type { ProtocolVotingViewModel, SyncProtocolVotingViewModel } from "@/viewmodels/admin/protocolVoting.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-18 14:20:58 +02:00
import differenceWith from "lodash.differencewith";
2024-10-13 15:47:52 +02:00
export const useProtocolVotingStore = defineStore("protocolVoting", {
state: () => {
return {
voting: [] as Array<ProtocolVotingViewModel>,
2024-10-18 14:20:58 +02:00
origin: [] as Array<ProtocolVotingViewModel>,
2024-10-13 15:47:52 +02:00
loading: "loading" as "loading" | "fetched" | "failed",
2024-10-18 14:20:58 +02:00
syncingProtocolVoting: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
2024-10-13 15:47:52 +02:00
};
},
2024-10-18 14:20:58 +02:00
getters: {
detectedChangeProtocolVoting: (state) =>
!isEqual(state.origin, state.voting) && state.syncingProtocolVoting != "syncing",
},
2024-10-13 15:47:52 +02:00
actions: {
2024-10-18 14:20:58 +02:00
setProtocolVotingSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingProtocolVoting = state;
},
2024-10-13 15:47:52 +02:00
fetchProtocolVoting() {
this.loading = "loading";
2024-10-18 14:20:58 +02:00
this.fetchProtocolVotingPromise()
2024-10-13 15:47:52 +02:00
.then((result) => {
2024-10-18 14:20:58 +02:00
this.origin = result.data;
this.voting = 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
fetchProtocolVotingPromise() {
const protocolId = useProtocolStore().activeProtocol;
return http.get(`/admin/protocol/${protocolId}/votings`);
},
createProtocolVoting() {
const protocolId = useProtocolStore().activeProtocol;
if (protocolId == null) return;
return http
.post(`/admin/protocol/${protocolId}/voting`)
.then((res) => {
this.voting.push({
2024-10-29 15:23:35 +01:00
id: Number(res.data),
2024-10-18 14:20:58 +02:00
topic: "",
context: "",
favour: 0,
abstain: 0,
against: 0,
protocolId: Number(protocolId),
});
})
.catch((err) => {});
},
async synchronizeActiveProtocolVoting() {
this.syncingProtocolVoting = "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/votings`, {
votings: differenceWith(this.voting, this.origin, isEqual),
})
.then((res) => {
this.syncingProtocolVoting = "synced";
})
.catch((err) => {
this.syncingProtocolVoting = "failed";
});
this.fetchProtocolVotingPromise()
.then((res) => {
this.origin = res.data;
2024-10-29 15:23:35 +01:00
if (this.detectedChangeProtocolVoting) this.syncingProtocolVoting = "detectedChanges";
2024-10-18 14:20:58 +02:00
})
.catch((err) => {});
2024-10-13 15:47:52 +02:00
},
},
});