texts.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import api from '@/api'
  2. import { print } from 'graphql/language/printer'
  3. import {
  4. TextsDepart, TextCard, TextdepartRecursive
  5. } from '@/api/queries'
  6. import TextdepartRecursiveWithDepth from '@/api/queries/TextdepartRecursiveWithDepth.gql'
  7. export default {
  8. state: {
  9. textsDepart: undefined
  10. },
  11. mutations: {
  12. 'SET_TEXTS_DEPART' (state, texts) {
  13. state.textsDepart = texts
  14. }
  15. },
  16. actions: {
  17. 'GET_TEXTS_DEPART' ({ state, commit }) {
  18. return api.post('', { query: print(TextsDepart) }).then(({ data }) => {
  19. commit('SET_TEXTS_DEPART', data.data.textsdepart)
  20. return state.textsDepart
  21. })
  22. },
  23. 'GET_TEXT' (store, { id }) {
  24. return api.post('', { query: print(TextCard), variables: { id } })
  25. .then(data => (data.data.data.text))
  26. },
  27. 'GET_TREE' (store, id) {
  28. return api.post('', { query: print(TextdepartRecursive), variables: { id } })
  29. .then(({ data }) => (data.data.textref))
  30. },
  31. 'GET_TREE_WITH_DEPTH' (store, { id, depth }) {
  32. const baseQuery = print(TextdepartRecursiveWithDepth)
  33. function formatQuery (str, depth) {
  34. if (depth > 0) {
  35. return formatQuery(
  36. str.replace('INPUT', '...TextrefTreeFields\nsiblings: text_en_rebond {\nINPUT\n}'),
  37. --depth
  38. )
  39. } else {
  40. return str.replace('INPUT', '...TextrefTreeFields')
  41. }
  42. }
  43. return api.post('', { query: formatQuery(baseQuery, depth), variables: { id } })
  44. .then(({ data }) => (data.data.textref))
  45. }
  46. },
  47. getters: {
  48. textsDepartOptions: state => {
  49. if (!state.textsDepart) return undefined
  50. return state.textsDepart.map(({ id, title }) => ({
  51. value: id,
  52. text: `(${id}) ${title}`
  53. }))
  54. },
  55. orderedParents: state => {
  56. // FIXME duplicates references if multiple authors ?
  57. if (!state.textsDepart) return undefined
  58. return state.textsDepart.sort((a, b) => {
  59. if (!b.authors) return -1
  60. if (!a.authors) return +1
  61. if (a.authors[0].name < b.authors[0].name) return -1
  62. if (a.authors[0].name > b.authors[0].name) return 1
  63. return 0
  64. }).reduce((dict, text) => {
  65. const firstChar = text.authors ? text.authors[0].name[0] : '&'
  66. if (!(firstChar in dict)) dict[firstChar] = []
  67. dict[firstChar].push(text)
  68. return dict
  69. }, {})
  70. }
  71. }
  72. }