Browse Source

implement indempotent deletion and insertion

tripeur 4 years ago
parent
commit
a7dc5d8edc

+ 3 - 1
src/mixins/ImportJsonState.ts

@@ -50,7 +50,9 @@ export default defineComponent({
         return iRessource;
       });
       // Push the items to the store
-      this.$store.commit(MutationTypes.reorderCreneauGroup, creneauGroups);
+      creneauGroups.forEach((r, pos) => {
+        this.$store.commit(MutationTypes.addCreneauGroupAt, { r, pos });
+      });
 
       // Import Creneau
       for (const c of obj.creneaux) {

+ 31 - 34
src/store/Mutations.ts

@@ -56,38 +56,38 @@ export type Mutations<S = State> = {
   ): void;
 
   [MutationTypes.addCreneau](state: S, payload: Creneau): void;
-  [MutationTypes.removeCreneau](state: S, payload: Creneau): void;
+  [MutationTypes.removeCreneau](state: S, uuid: string): void;
   [MutationTypes.editCreneau]<K extends keyof Creneau>(
     state: S,
     payload: { id: string; field: K; value: Creneau[K] }
-  ): boolean;
+  ): void;
 
   [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.removeCreneauGroup](state: S, uuid: string): void;
   [MutationTypes.editCreneauGroup]<K extends keyof Ressource>(
     state: S,
     payload: { id: string; field: K; value: Ressource[K] }
-  ): boolean;
-  [MutationTypes.reorderCreneauGroup](state: S, payload: Array<Ressource>): boolean;
+  ): void;
+  [MutationTypes.reorderCreneauGroup](state: S, payload: Array<string>): void;
 
   [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.removeBenevole](state: S, uuid: number): void;
   [MutationTypes.editBenevole]<K extends keyof Benevole>(
     state: S,
     payload: { id: number; field: K; value: Benevole[K] }
-  ): boolean;
+  ): void;
 
   [MutationTypes.addConstraint](state: S, payload: Competence): void;
-  [MutationTypes.removeConstraint](state: S, payload: Competence): void;
+  [MutationTypes.removeConstraint](state: S, uuid: number): void;
   [MutationTypes.editConstraint]<K extends keyof Competence>(
     state: S,
     payload: { id: number; field: K; value: Competence[K] }
-  ): boolean;
+  ): void;
 
   [MutationTypes.newVersion](state: S, payload: EvenementVersion): void;
   [MutationTypes.refreshVersion](state: S, payload: Array<EvenementVersion>): void;
@@ -113,24 +113,23 @@ export const mutations: MutationTree<State> & Mutations = {
 
   // Creneau Management
   [MutationTypes.addCreneau](state, creneau) {
-    state.creneauList = [creneau, ...state.creneauList];
+    if (state.creneauList.findIndex((c) => c.id == creneau.id) < 0)
+      state.creneauList = [creneau, ...state.creneauList];
   },
-  [MutationTypes.removeCreneau](state, creneau) {
-    state.creneauList = state.creneauList.filter((c) => c.id !== creneau.id);
+  [MutationTypes.removeCreneau](state, uuid) {
+    state.creneauList = state.creneauList.filter((c) => c.id !== uuid);
   },
   [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) {
+    if (creneau && benevole && !benevole.creneauIdList.includes(pair.creneauId)) {
       benevole.creneauIdList = [...benevole.creneauIdList, pair.creneauId];
       creneau.benevoleIdList = [...creneau.benevoleIdList, pair.benevoleId];
     }
@@ -149,11 +148,14 @@ export const mutations: MutationTree<State> & Mutations = {
   },
   // Creneau Group Management
   [MutationTypes.reorderCreneauGroup](state, payload) {
-    state.creneauGroupList = payload;
+    state.creneauGroupList = state.creneauGroupList
+      .slice()
+      .sort((a, b) => payload.indexOf(a.id) - payload.indexOf(b.id));
     return true;
   },
   [MutationTypes.addCreneauGroup](state, payload) {
-    state.creneauGroupList = [...state.creneauGroupList, payload];
+    if (state.creneauGroupList.findIndex((c) => c.id == payload.id) < 0)
+      state.creneauGroupList = [...state.creneauGroupList, payload];
   },
   [MutationTypes.addCreneauGroupAt](state, payload) {
     if (payload.pos < 1) {
@@ -164,57 +166,52 @@ export const mutations: MutationTree<State> & Mutations = {
       state.creneauGroupList.splice(payload.pos, 0, payload.r);
     }
   },
-  [MutationTypes.removeCreneauGroup](state, payload) {
-    state.creneauGroupList = state.creneauGroupList.filter((c) => c.id !== payload.id);
+  [MutationTypes.removeCreneauGroup](state, uuid) {
+    state.creneauGroupList = state.creneauGroupList.filter((c) => c.id !== uuid);
   },
   [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];
+    if (state.benevoleList.findIndex((c) => c.id == payload.id) < 0)
+      state.benevoleList = [payload, ...state.benevoleList];
   },
-  [MutationTypes.removeBenevole](state, payload) {
+  [MutationTypes.removeBenevole](state, uuid) {
     state.creneauList.forEach(
-      (creneau) =>
-        (creneau.benevoleIdList = creneau.benevoleIdList.filter((id) => id !== payload.id))
+      (creneau) => (creneau.benevoleIdList = creneau.benevoleIdList.filter((id) => id !== uuid))
     );
-    state.benevoleList = state.benevoleList.filter((c) => c.id !== payload.id);
+    state.benevoleList = state.benevoleList.filter((c) => c.id !== uuid);
   },
   [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];
+    if (state.competenceList.findIndex((c) => c.id == payload.id) < 0)
+      state.competenceList = [payload, ...state.competenceList];
   },
-  [MutationTypes.removeConstraint](state, payload) {
-    const filterFunction = (id: number) => id !== payload.id;
+  [MutationTypes.removeConstraint](state, uuid) {
+    const filterFunction = (id: number) => id !== uuid;
     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);
+    state.competenceList = state.competenceList.filter((c) => c.id !== uuid);
   },
   [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) {

+ 1 - 1
src/views/BenevoleManager.vue

@@ -208,7 +208,7 @@ export default defineComponent({
       this.currentBenevole = temp;
     },
     deleteBenevole(payload: Benevole) {
-      this.$store.commit(MutationTypes.removeBenevole, payload);
+      this.$store.commit(MutationTypes.removeBenevole, payload.id);
       this.currentBenevole = undefined;
     },
     onRowClick(row: { id: number }) {

+ 1 - 1
src/views/CompetenceManager.vue

@@ -101,7 +101,7 @@ export default defineComponent({
       this.currentCompetence = comp;
     },
     deleteCompetence(payload: Competence) {
-      this.$store.commit(MutationTypes.removeConstraint, payload);
+      this.$store.commit(MutationTypes.removeConstraint, payload.id);
       this.currentCompetence = undefined;
     },
     onRowClick(row: { id: number }) {

+ 1 - 1
src/views/Evenement.vue

@@ -241,7 +241,7 @@ export default defineComponent({
       const toBeRemoved = this.$store.state.creneauList.filter((c) => max < c.start || c.end < min);
 
       for (let elt of toBeRemoved) {
-        this.$store.commit(MutationTypes.removeCreneau, elt);
+        this.$store.commit(MutationTypes.removeCreneau, elt.id);
       }
     },
   },

+ 13 - 5
src/views/Planning.vue

@@ -196,10 +196,12 @@ export default defineComponent({
     },
     deleteRessource(ressource: Ressource): void {
       const contentRemoved = this.timeline.removeRessourceById(ressource.id);
-      contentRemoved.ressources.map((r) => this.$store.commit(MutationTypes.removeCreneauGroup, r));
+      contentRemoved.ressources.map((r) =>
+        this.$store.commit(MutationTypes.removeCreneauGroup, r.id)
+      );
       contentRemoved.items.map((e) => {
         const crenenau = this.$store.getters.getCreneauById(e.id);
-        if (crenenau) this.$store.commit(MutationTypes.removeCreneau, crenenau);
+        if (crenenau) this.$store.commit(MutationTypes.removeCreneau, crenenau.id);
       });
     },
     deleteCurrentRessource(): void {
@@ -207,7 +209,7 @@ export default defineComponent({
     },
     deleteCreneau(creneau: Creneau) {
       this.timeline.removeEventById(creneau.id);
-      this.$store.commit(MutationTypes.removeCreneau, creneau);
+      this.$store.commit(MutationTypes.removeCreneau, creneau.id);
     },
     deleteCurrentCreneau(): void {
       if (this.currentCreneau) this.deleteCreneau(this.currentCreneau);
@@ -255,7 +257,10 @@ export default defineComponent({
       }
     },
     ressourceChangeHandler(ev: CustomEvent<{ ressources: Array<Ressource> }>) {
-      this.$store.commit(MutationTypes.reorderCreneauGroup, ev.detail.ressources);
+      this.$store.commit(
+        MutationTypes.reorderCreneauGroup,
+        ev.detail.ressources.map((o) => o.id)
+      );
     },
     updateCreneauGroup<K extends keyof Ressource>(payload: {
       id: string;
@@ -405,7 +410,10 @@ export default defineComponent({
     this.timeline.setAttribute("start", this.start.toISOString());
     this.timeline.setAttribute("end", this.end.toISOString());
     this.timeline.addRessources(this.creneauGroupList);
-    this.$store.commit(MutationTypes.reorderCreneauGroup, this.timeline.getRessources());
+    this.$store.commit(
+      MutationTypes.reorderCreneauGroup,
+      this.timeline.getRessources().map((o) => o.id)
+    );
     this.timeline.addEvents(this.eventList);
     this.timeline.setLegendUnitFormat("d", "dddd D MMMM");
     this.timeline.customStyle = `.bubble{