ImportJsonState.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { StateJSON } from "@/store/State";
  7. import { IRessource, Ressource, RessourceJSON } 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: StateJSON, 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]: RessourceJSON } = {};
  34. obj.creneauGroups.forEach((element) => {
  35. dict[element.id] = element;
  36. });
  37. // map parent to children
  38. const creneauGroups = obj.creneauGroups.map((ressource) => {
  39. const iRessource: IRessource = { ...ressource };
  40. if (iRessource.parentId) {
  41. if (iRessource.parentId in dict) {
  42. iRessource.parent = dict[iRessource.parentId];
  43. } else {
  44. throw new Error("Missing parent of creneau group : " + ressource.id);
  45. }
  46. }
  47. return iRessource;
  48. });
  49. // Push the items to this application
  50. creneauGroups.forEach((r) => {
  51. this.$store.commit(MutationTypes.addCreneauGroup, new Ressource(r));
  52. });
  53. // Import Creneau
  54. for (const c of obj.creneaux) {
  55. const creneau = Creneau.fromJSON(c);
  56. this.$store.commit(MutationTypes.addCreneau, creneau);
  57. // add the creneau to the corresponding benevole
  58. for (const id of creneau.benevoleIdList) {
  59. const b = this.$store.getters.getBenevoleById(id);
  60. if (b) {
  61. this.$store.commit(MutationTypes.editBenevole, {
  62. id: id,
  63. field: "creneauIdList",
  64. value: Array.from(new Set([...b.creneauIdList, creneau.id])),
  65. });
  66. } else {
  67. throw new Error(`The benevole ${id} was not find `);
  68. }
  69. }
  70. }
  71. },
  72. },
  73. });