import Benevole from "@/models/Benevole"; import Competence from "@/models/Competence"; import Creneau from "@/models/Creneau"; import Evenement from "@/models/Evenement"; import EvenementVersion from "@/models/EvenementVersion"; import { Ressource } from "jc-timeline"; import { MutationTree } from "vuex"; import { Client } from "stompjs"; import { State } from "./State"; export enum MutationTypes { resetState = "resetState", editEvenement = "editEvenement", addCreneau = "addCreneau", removeCreneau = "removeCreneau", editCreneau = "editCreneau", addBenevole2Creneau = "addBenevole2Creneau", removeBenevole2Creneau = "removeBenevole2Creneau", clearBenevole2Creneau = "clearBenevole2Creneau", addCreneauGroup = "addCreneauGroup", addCreneauGroupAt = "addCreneauGroupAt", removeCreneauGroup = "removeCreneauGroup", editCreneauGroup = "editCreneauGroup", reorderCreneauGroup = "reorderCreneauGroup", addBenevole = "addBenevole", removeBenevole = "removeBenevole", editBenevole = "editBenevole", addConstraint = "addConstraint", removeConstraint = "removeConstraint", editConstraint = "editConstraint", newVersion = "newVersion", refreshVersion = "refreshVersion", pushSavedPlanning = "pushSavedPlanning", refreshSavedPlanning = "refreshSavedPlanning", setUsername = "setUsername", setStompClient = "setStompClient", } interface CreneauPairing { creneauId: string; benevoleId: number; } export type Mutations = { [MutationTypes.resetState](state: S): void; [MutationTypes.editEvenement]( state: S, payload: { field: K; value: Evenement[K] } ): void; [MutationTypes.addCreneau](state: S, payload: Creneau): void; [MutationTypes.removeCreneau](state: S, payload: Creneau): void; [MutationTypes.editCreneau]( state: S, payload: { id: string; field: K; value: Creneau[K] } ): boolean; [MutationTypes.addCreneauGroup](state: S, payload: Ressource): void; [MutationTypes.addCreneauGroupAt](state: S, payload: { r: Ressource; pos: number }): void; [MutationTypes.removeCreneauGroup](state: S, payload: Ressource): void; [MutationTypes.editCreneauGroup]( state: S, payload: { id: string; field: K; value: Ressource[K] } ): boolean; [MutationTypes.reorderCreneauGroup](state: S, payload: Array): boolean; [MutationTypes.addBenevole2Creneau](state: S, payload: CreneauPairing): void; [MutationTypes.removeBenevole2Creneau](state: S, payload: CreneauPairing): void; [MutationTypes.clearBenevole2Creneau](state: S): void; [MutationTypes.addBenevole](state: S, payload: Benevole): void; [MutationTypes.removeBenevole](state: S, payload: Benevole): void; [MutationTypes.editBenevole]( state: S, payload: { id: number; field: K; value: Benevole[K] } ): boolean; [MutationTypes.addConstraint](state: S, payload: Competence): void; [MutationTypes.removeConstraint](state: S, payload: Competence): void; [MutationTypes.editConstraint]( state: S, payload: { id: number; field: K; value: Competence[K] } ): boolean; [MutationTypes.newVersion](state: S, payload: EvenementVersion): void; [MutationTypes.refreshVersion](state: S, payload: Array): void; [MutationTypes.pushSavedPlanning](state: S, payload: EvenementVersion): void; [MutationTypes.refreshSavedPlanning](state: S, payload: Array): void; [MutationTypes.setUsername](state: S, payload: string): void; [MutationTypes.setStompClient](state: S, payload: Client): void; }; export const mutations: MutationTree & Mutations = { [MutationTypes.resetState](state): void { state.evenement = new Evenement(); state.creneauList = []; state.creneauGroupList = []; state.competenceList = []; state.benevoleList = []; }, [MutationTypes.editEvenement](state, payload): void { state.evenement[payload.field] = payload.value; }, // Creneau Management [MutationTypes.addCreneau](state, creneau) { state.creneauList = [creneau, ...state.creneauList]; }, [MutationTypes.removeCreneau](state, creneau) { state.creneauList = state.creneauList.filter((c) => c.id !== creneau.id); }, [MutationTypes.editCreneau](state, payload) { const el = state.creneauList.find((o) => o.id == payload.id); if (el) { el[payload.field] = payload.value; return true; } return false; }, [MutationTypes.addBenevole2Creneau](state, pair: CreneauPairing) { const benevole = state.benevoleList.find((o) => o.id == pair.benevoleId); const creneau = state.creneauList.find((o) => o.id == pair.creneauId); if (creneau && benevole) { benevole.creneauIdList = [...benevole.creneauIdList, pair.creneauId]; creneau.benevoleIdList = [...creneau.benevoleIdList, pair.benevoleId]; } }, [MutationTypes.removeBenevole2Creneau](state, pair: CreneauPairing) { const benevole = state.benevoleList.find((o) => o.id == pair.benevoleId); const creneau = state.creneauList.find((o) => o.id == pair.creneauId); if (benevole) benevole.creneauIdList = benevole.creneauIdList.filter((id) => id !== pair.creneauId); if (creneau) creneau.benevoleIdList = creneau.benevoleIdList.filter((id) => id !== pair.benevoleId); }, [MutationTypes.clearBenevole2Creneau](state) { state.benevoleList.forEach((b) => (b.creneauIdList = [])); state.creneauList.forEach((b) => (b.benevoleIdList = [])); }, // Creneau Group Management [MutationTypes.reorderCreneauGroup](state, payload) { state.creneauGroupList = payload; return true; }, [MutationTypes.addCreneauGroup](state, payload) { state.creneauGroupList = [...state.creneauGroupList, payload]; }, [MutationTypes.addCreneauGroupAt](state, payload) { if (payload.pos < 1) { state.creneauGroupList = [payload.r, ...state.creneauGroupList]; } else if (payload.pos > state.creneauGroupList.length) { state.creneauGroupList = [...state.creneauGroupList, payload.r]; } else { state.creneauGroupList.splice(payload.pos, 0, payload.r); } }, [MutationTypes.removeCreneauGroup](state, payload) { state.creneauGroupList = state.creneauGroupList.filter((c) => c.id !== payload.id); }, [MutationTypes.editCreneauGroup](state, payload) { const el = state.creneauGroupList.find((o) => o.id == payload.id); if (el) { el[payload.field] = payload.value; return true; } return false; }, // Benevole Management [MutationTypes.addBenevole](state, payload) { state.benevoleList = [payload, ...state.benevoleList]; }, [MutationTypes.removeBenevole](state, payload) { state.creneauList.forEach( (creneau) => (creneau.benevoleIdList = creneau.benevoleIdList.filter((id) => id !== payload.id)) ); state.benevoleList = state.benevoleList.filter((c) => c.id !== payload.id); }, [MutationTypes.editBenevole](state, payload) { const el = state.benevoleList.find((o) => o.id == payload.id); if (el) { el[payload.field] = payload.value; return true; } return false; }, // Constraint Management [MutationTypes.addConstraint](state, payload) { state.competenceList = [payload, ...state.competenceList]; }, [MutationTypes.removeConstraint](state, payload) { const filterFunction = (id: number) => id !== payload.id; state.benevoleList.forEach( (benevole) => (benevole.competenceIdList = benevole.competenceIdList.filter(filterFunction)) ); state.creneauList.forEach( (creneau) => (creneau.competencesIdList = creneau.competencesIdList.filter(filterFunction)) ); state.competenceList = state.competenceList.filter((c) => c.id !== payload.id); }, [MutationTypes.editConstraint](state, payload) { const el = state.competenceList.find((o) => o.id == payload.id); if (el) { el[payload.field] = payload.value; return true; } return false; }, [MutationTypes.newVersion](state, payload) { // if the version is not yet registered in the current history. if (!state.history.find((v) => v.id == payload.id)) { // register the new version in the history state.history = [payload, ...state.history.filter((h) => h.uuid == payload.uuid)]; // add the version in the planning list if it doesn't exist neither. if (state.savedPlanning.filter((o) => o.uuid == payload.uuid).length == 0) { state.savedPlanning = [payload, ...state.savedPlanning]; } } }, [MutationTypes.refreshVersion](state, payload) { state.history = payload; }, [MutationTypes.pushSavedPlanning](state, payload) { state.savedPlanning = [payload, ...state.savedPlanning]; }, [MutationTypes.refreshSavedPlanning](state, payload) { state.savedPlanning = payload; }, [MutationTypes.setUsername](state, payload) { state.username = payload; }, [MutationTypes.setStompClient](state, payload) { state.stompClient = payload; }, };