concernements.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { defineStore } from 'pinia'
  2. // import REST from '@api/rest-axios'
  3. // import JSONAPI from '@api/json-axios'
  4. import { print } from 'graphql/language/printer'
  5. import gql from 'graphql-tag'
  6. // import REST from '@api/rest-axios'
  7. import GQL from '@api/graphql-axios'
  8. // import JSONAPI from '@api/json-axios'
  9. import ConcernementFields from '@api/gql/concernement.fragment.gql'
  10. // import EntityFields from '@api/gql/entitydef.fragment.gql'
  11. export const ConcernementsStore = defineStore({
  12. id: 'concernements',
  13. state: () => ({
  14. map_mode: null,
  15. concernements: [],
  16. concernementsByID: {},
  17. allEntitesById: {},
  18. allBesoinsById: {},
  19. allSuperpositions: [],
  20. allProximites: [],
  21. opened_concernement: false,
  22. opened_entite_id: null,
  23. opened_recit: false,
  24. recit_plyr_player: null,
  25. ct_concernement: {},
  26. ct_entite: {},
  27. ct_cercle_politique: {},
  28. p_groupes_interets: {},
  29. p_reception_et_traitement: {},
  30. p_mise_en_oeuvre_decision: {},
  31. p_reception_application_decision: {},
  32. concernements_loaded: false,
  33. }),
  34. getters: {
  35. },
  36. actions: {
  37. setMapMode(mode) {
  38. console.log(`set map_mode ${mode}`);
  39. // mode can be : terraindevie, proximite, superposition, puissancedagir, action, doleancer
  40. this.map_mode = mode;
  41. },
  42. loadConcernements () {
  43. console.log('concernements store loadConcernements');
  44. return new Promise((resolve, reject) => {
  45. const ast = gql`{
  46. allconcernements {
  47. ...ConcernementFields
  48. }
  49. }
  50. ${ConcernementFields}
  51. `
  52. // console.log('ast', ast);
  53. GQL.post('', { query: print(ast) })
  54. .then(({ data : { data : { allconcernements } } }) => {
  55. console.log('loadconcernements all loaded', allconcernements)
  56. this.concernements = [];
  57. // parse concernements
  58. allconcernements.forEach(concernement => {
  59. concernement.visible = true;
  60. concernement.entites_byid = {};
  61. concernement.entitesagissantes_byid = {};
  62. concernement.has_proximites = false;
  63. concernement.has_superpositions = false;
  64. concernement.has_agissantes = false;
  65. // var entites_temp = concernement.entites; // record a temp entites liste
  66. // concernement.entites = []; // erase the concernement.entite array as we want to keep only visible entites
  67. concernement.entites.forEach(entite => {
  68. if (entite.entite) { // entite.entite may be null because of workflow confidentiality
  69. concernement.entites_byid[entite.entite.id] = entite;
  70. // record entite agissante
  71. if (entite.entite.agissante) {
  72. concernement.entitesagissantes_byid[entite.entite.id] = entite;
  73. concernement.has_agissantes = true;
  74. }
  75. // record a flat list of all entités of all concernement for map-popup
  76. this.allEntitesById[entite.entite.id] = entite;
  77. // concernement.entites.push(entite); // fill the entites array with visible entite only
  78. // PROXIMITES
  79. if (entite.entite.proximite.length) {
  80. console.log("proximite", entite.entite.proximite);
  81. concernement.has_proximites = true;
  82. }
  83. // SUPERPOSITIONS
  84. if (entite.entite.superposition.length) {
  85. concernement.has_superpositions = true;
  86. entite.entite.superposition.forEach(entite_superpose => {
  87. // console.log(`superposition eid:${entite.entite.id}, teid:${entite_superpose.id}`);
  88. let already_recorded = false;
  89. // loop through all already recorded superposition to complete the array instead of create duplicates
  90. for(let superposition of this.allSuperpositions) {
  91. // if (superposition.indexOf(entite_superpose.id) >= 0) {
  92. // already_recorded = true;
  93. // if (superposition.indexOf(entite.entite.id) < 0) {
  94. // superposition.push(entite.entite.id)
  95. // }
  96. // break;
  97. // }
  98. // if (superposition.indexOf(entite.entite.id) >= 0) {
  99. // already_recorded = true;
  100. // if (superposition.indexOf(entite_superpose.id) < 0) {
  101. // superposition.push(entite_superpose.id)
  102. // }
  103. // break;
  104. // }
  105. for(let superposition_item of superposition) {
  106. if (superposition_item.eid === entite.entite.id && !superposition_item.cid) {
  107. console.log(`already_recorded, eid:${entite.entite.id}, teid:${entite_superpose.id}`, entite.entite.title);
  108. already_recorded = true;
  109. superposition_item.cid = concernement.id;
  110. break;
  111. }
  112. }
  113. if (already_recorded) {
  114. break;
  115. }
  116. }
  117. // if not already recorded, add it to the array
  118. if (!already_recorded) {
  119. console.log(`NOT already_recorded, eid:${entite.entite.id}, teid:${entite_superpose.id}`, entite.entite.title);
  120. this.allSuperpositions.push([
  121. {
  122. cid: concernement.id,
  123. eid: entite.entite.id
  124. },
  125. {
  126. cid: null,
  127. eid: entite_superpose.id
  128. }
  129. ])
  130. }
  131. })
  132. }
  133. }
  134. });
  135. // revisions
  136. concernement.active_revision = concernement.revision_id;
  137. concernement.revisions_byid = {};
  138. concernement.revisions.forEach(rev => {
  139. concernement.revisions_byid[rev.revision_id] = rev;
  140. });
  141. // puissance d'agir
  142. concernement.has_puissancedagir = concernement.besoins.length ? true : false;
  143. concernement.besoins.forEach(besoin => {
  144. this.allBesoinsById[besoin.id] = besoin;
  145. });
  146. // doleance
  147. concernement.has_doleance = concernement.doleances.length ? true : false;
  148. if (concernement.has_doleance) {
  149. concernement.opened_doleance = {
  150. id: concernement.doleances[0].id
  151. };
  152. }
  153. // recit
  154. concernement.has_recit = concernement.recit !== null;
  155. // common
  156. this.concernements.push(concernement);
  157. this.concernementsByID[concernement.id] = concernement;
  158. });
  159. console.log('this.allSuperpositions', this.allSuperpositions);
  160. this.concernements_loaded = true;
  161. })
  162. .catch(error => {
  163. console.warn('Issue with loadConcernements', error)
  164. Promise.reject(error)
  165. })
  166. })
  167. },
  168. reloadConcernements () {
  169. console.log('reloadConcernements');
  170. // reset the arrays
  171. this.concernements = [];
  172. this.concernementsByID = {};
  173. this.concernements_loaded = false;
  174. this.loadConcernements();
  175. },
  176. loadContentTypeDefinition () {
  177. const body = {
  178. query: `
  179. query EntityDef($type: String!, $bundle: String!){
  180. entitydef(type: $type, bundle: $bundle) {
  181. fields {
  182. type
  183. field_name
  184. label
  185. description
  186. }
  187. }
  188. }`,
  189. variables: {}
  190. }
  191. // concernement
  192. body.variables = { type: 'node', bundle: 'concernement' }
  193. GQL.post('', body)
  194. .then(({ data: { data: { entitydef }}}) => {
  195. // console.log('loadContentTypeDefinition entitydef', entitydef);
  196. entitydef.fields.forEach(field => {
  197. this.ct_concernement[field.field_name] = field;
  198. });
  199. console.log('loadContentTypeDefinition entitydef concernement', this.ct_concernement);
  200. })
  201. // entite
  202. body.variables = { type: 'node', bundle: 'entite' }
  203. GQL.post('', body)
  204. .then(({ data: { data: { entitydef }}}) => {
  205. // console.log('loadContentTypeDefinition entitydef', entitydef);
  206. entitydef.fields.forEach(field => {
  207. this.ct_entite[field.field_name] = field;
  208. });
  209. console.log('loadContentTypeDefinition entitydef entite', this.ct_entite);
  210. })
  211. // cercle_politique
  212. body.variables = { type: 'node', bundle: 'cercle_politique' }
  213. GQL.post('', body)
  214. .then(({ data: { data: { entitydef }}}) => {
  215. // console.log('loadContentTypeDefinition entitydef', entitydef);
  216. entitydef.fields.forEach(field => {
  217. this.ct_cercle_politique[field.field_name] = field;
  218. });
  219. console.log('loadContentTypeDefinition entitydef cercle_politique', this.ct_cercle_politique);
  220. })
  221. // paragraphe groupes_interets
  222. body.variables = { type: 'paragraph', bundle: 'groupes_interets' }
  223. GQL.post('', body)
  224. .then(({ data: { data: { entitydef }}}) => {
  225. // console.log('loadContentTypeDefinition entitydef', entitydef);
  226. entitydef.fields.forEach(field => {
  227. this.p_groupes_interets[field.field_name] = field;
  228. });
  229. console.log('loadContentTypeDefinition entitydef p_groupes_interets', this.p_groupes_interets);
  230. })
  231. // paragraphe reception_et_traitement
  232. body.variables = { type: 'paragraph', bundle: 'reception_et_traitement' }
  233. GQL.post('', body)
  234. .then(({ data: { data: { entitydef }}}) => {
  235. // console.log('loadContentTypeDefinition entitydef', entitydef);
  236. entitydef.fields.forEach(field => {
  237. this.p_reception_et_traitement[field.field_name] = field;
  238. });
  239. console.log('loadContentTypeDefinition entitydef p_reception_et_traitement', this.p_reception_et_traitement);
  240. })
  241. // paragraphe mise_en_oeuvre_decision
  242. body.variables = { type: 'paragraph', bundle: 'mise_en_oeuvre_decision' }
  243. GQL.post('', body)
  244. .then(({ data: { data: { entitydef }}}) => {
  245. // console.log('loadContentTypeDefinition entitydef', entitydef);
  246. entitydef.fields.forEach(field => {
  247. this.p_mise_en_oeuvre_decision[field.field_name] = field;
  248. });
  249. console.log('loadContentTypeDefinition entitydef p_mise_en_oeuvre_decision', this.p_mise_en_oeuvre_decision);
  250. })
  251. // paragraphe reception_application_decision
  252. body.variables = { type: 'paragraph', bundle: 'reception_application_decision' }
  253. GQL.post('', body)
  254. .then(({ data: { data: { entitydef }}}) => {
  255. // console.log('loadContentTypeDefinition entitydef', entitydef);
  256. entitydef.fields.forEach(field => {
  257. this.p_reception_application_decision[field.field_name] = field;
  258. });
  259. console.log('loadContentTypeDefinition entitydef p_reception_application_decision', this.p_reception_application_decision);
  260. })
  261. },
  262. hideShowConcernement (id, state) {
  263. // console.log(`disableConcernement id: ${id}`);
  264. this.concernements.forEach((c, i) => {
  265. if (c.id === id) {
  266. this.concernements[i].visible = this.concernementsByID[id].visible = state;
  267. }
  268. })
  269. },
  270. openCloseConcernements (id) {
  271. console.log(`openCloseConcernements id: ${id}`);
  272. var state;
  273. this.opened_concernement = null;
  274. this.opened_recit = null;
  275. this.concernements.forEach((c, i) => {
  276. state = id === c.id;
  277. // console.log(`openCloseConcernements foreach id: ${id}, c.id: ${c.id}, state: ${state}`, id, c.id);
  278. c.opened = this.concernements[i].opened = this.concernementsByID[c.id].opened = state;
  279. if (state) {
  280. this.opened_concernement = c;
  281. if (c.has_recit) {
  282. this.opened_recit = c.recit;
  283. }
  284. // console.log('openCloseConcernements', this.opened_concernement.opened);
  285. }
  286. });
  287. },
  288. resetConcernementOpened () {
  289. this.opened_concernement = null;
  290. this.openCloseConcernements();
  291. },
  292. setOpenedEntityId(id){
  293. this.opened_entite_id = id;
  294. },
  295. setBesoinPaperId(paper_id, cid, bid, rid){
  296. // this.concernements.forEach((c, i) => {
  297. // if(c.id === cid){
  298. this.concernementsByID[cid].besoins.forEach((b,j) => {
  299. if(b.id === bid) {
  300. if (!rid) {
  301. // this.concernements[i].besoins[j].paper_id = this.concernementsByID[cid].besoins[j].paper_id = paper_id;
  302. this.concernementsByID[cid].besoins[j].paper_id = paper_id;
  303. } else {
  304. this.concernementsByID[cid].besoins[j].reponses.forEach((r,k) => {
  305. if (r.id === rid) {
  306. // this.concernements[i].besoins[j].reponses[k].paper_id = this.concernementsByID[cid].besoins[j].reponses[k].paper_id = paper_id;
  307. this.concernementsByID[cid].besoins[j].reponses[k].paper_id = paper_id;
  308. }
  309. })
  310. }
  311. }
  312. })
  313. // }
  314. // })
  315. },
  316. setOpenedDoleance(cid, did){
  317. console.log(`setOpenedDoleance(${cid}, ${did})`, cid, did);
  318. this.concernementsByID[cid].opened_doleance = {id: did};
  319. // console.log('this.opened_concernement.opened_doleance', this.opened_concernement.opened_doleance);
  320. },
  321. setOpenedDoleanceField(cid, did, field, index){
  322. console.log(`setOpenedDoleanceField cid, did, field, index`, cid, did, field, index);
  323. this.concernementsByID[cid].opened_doleance = {
  324. cid: cid,
  325. id: did,
  326. field: field,
  327. field_index: index
  328. };
  329. },
  330. setOpenedRecit (recit) {
  331. this.opened_recit = recit;
  332. },
  333. setRecitPlayer (player) {
  334. console.log('concernement store setRecitPlayer', player);
  335. this.recit_plyr_player = player;
  336. },
  337. setActiveRevision (cid, rid) {
  338. this.concernementsByID[cid].active_revision = rid;
  339. }
  340. }
  341. })