concernements.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. concernements: [],
  15. concernementsByID: {},
  16. allEntitesById: {},
  17. opened: false,
  18. ct_concernement: {}
  19. }),
  20. getters: {
  21. },
  22. actions: {
  23. loadConcernements () {
  24. console.log('concernements store loadConcernements');
  25. return new Promise((resolve, reject) => {
  26. const ast = gql`{
  27. allconcernements {
  28. ...ConcernementFields
  29. }
  30. }
  31. ${ConcernementFields}
  32. `
  33. console.log('ast', ast);
  34. GQL.post('', { query: print(ast) })
  35. .then(({ data : { data : { allconcernements } } }) => {
  36. console.log('loadconcernements loaded', allconcernements)
  37. this.concernements = [];
  38. allconcernements.forEach(concernement => {
  39. concernement.entites_byid = {};
  40. concernement.entites.forEach(entite => {
  41. concernement.entites_byid[entite.entite.id] = entite;
  42. // record a flat list of all entités of all concernement for map-popup
  43. this.allEntitesById[entite.entite.id] = entite;
  44. });
  45. this.concernements.push(concernement);
  46. this.concernementsByID[concernement.id] = concernement;
  47. });
  48. })
  49. .catch(error => {
  50. console.warn('Issue with loadConcernements', error)
  51. Promise.reject(error)
  52. })
  53. })
  54. },
  55. loadContentTypeDefinition () {
  56. const body = {
  57. query: `
  58. query EntityDef($type: String!, $bundle: String!){
  59. entitydef(type: $type, bundle: $bundle) {
  60. fields {
  61. type
  62. field_name
  63. label
  64. description
  65. }
  66. }
  67. }`,
  68. variables: { type: 'node', bundle: 'concernement' }
  69. }
  70. GQL.post('', body)
  71. .then(({ data: { data: { entitydef }}}) => {
  72. console.log('loadContentTypeDefinition entitydef', entitydef);
  73. entitydef.fields.forEach(field => {
  74. this.ct_concernement[field.field_name] = field;
  75. });
  76. })
  77. },
  78. openCloseConcernements (ids) {
  79. var state;
  80. this.concernements.forEach((c, i) => {
  81. state = ids.indexOf(c.id) !== -1;
  82. this.concernements[i].opened = this.concernementsByID[c.id].opened = state;
  83. if (state) {
  84. this.opened = c;
  85. this.router.push({name: 'concernement', params: {id: c.id}});
  86. }
  87. });
  88. },
  89. resetConcernementOpened () {
  90. this.opened = null;
  91. },
  92. openEntite (cid, id) {
  93. this.router.push({name: 'concernement', params: {id: cid, eid: id}});
  94. }
  95. }
  96. })