ImportJsonState.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Benevole from "@/models/Benevole";
  2. import Competence from "@/models/Competence";
  3. import Creneau from "@/models/Creneau";
  4. import Evenement from "@/models/Evenement";
  5. import { MutationTypes } from "@/store/Mutations";
  6. import { EvenementStateJSON } from "@/store/State";
  7. import { Ressource } from "jc-timeline";
  8. import { defineComponent } from "vue";
  9. const keyofEvent: Array<keyof Evenement> = ["name", "uuid", "start", "end"];
  10. export default defineComponent({
  11. methods: {
  12. importJsonState(obj: EvenementStateJSON, preserve = false) {
  13. // Remove previous content and load the main event title
  14. if (preserve == false) {
  15. this.$store.commit(MutationTypes.resetState, undefined);
  16. const e = Evenement.fromJSON(obj.evenement);
  17. for (const k of keyofEvent) {
  18. this.$store.commit(MutationTypes.editEvenement, {
  19. field: k,
  20. value: e[k],
  21. });
  22. }
  23. }
  24. // Import constraint
  25. obj.competences.forEach((c) => {
  26. this.$store.commit(MutationTypes.addConstraint, Competence.fromJSON(c));
  27. });
  28. // Import Benevoles
  29. obj.benevoles.forEach((b) => {
  30. this.$store.commit(MutationTypes.addBenevole, Benevole.fromJSON(b));
  31. });
  32. // Import creneau group
  33. const dict: { [k: string]: Ressource } = {};
  34. const creneauGroups = obj.creneauGroups.map((element) => {
  35. const ressource = new Ressource(element);
  36. dict[element.id] = ressource;
  37. return ressource;
  38. });
  39. // map parent to children
  40. obj.creneauGroups.forEach((iRessource) => {
  41. if (iRessource.parentId) {
  42. if (iRessource.parentId in dict) {
  43. dict[iRessource.id].parent = dict[iRessource.parentId];
  44. } else {
  45. throw new Error("Missing parent of creneau group : " + iRessource.id);
  46. }
  47. }
  48. return iRessource;
  49. });
  50. // Push the items to the store
  51. creneauGroups.forEach((r, pos) => {
  52. this.$store.commit(MutationTypes.addCreneauGroupAt, { r, pos });
  53. });
  54. // Import Creneau
  55. for (const c of obj.creneaux) {
  56. const creneau = Creneau.fromJSON(c);
  57. this.$store.commit(MutationTypes.addCreneau, creneau);
  58. // add the creneau to the corresponding benevole
  59. for (const id of creneau.benevoleIdList) {
  60. const b = this.$store.getters.getBenevoleById(id);
  61. if (b) {
  62. this.$store.commit(MutationTypes.editBenevole, {
  63. id: id,
  64. field: "creneauIdList",
  65. value: Array.from(new Set([...b.creneauIdList, creneau.id])),
  66. });
  67. } else {
  68. throw new Error(`The benevole ${id} was not find `);
  69. }
  70. }
  71. }
  72. },
  73. },
  74. });