texts.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. textsDepart: state => state.textsDepart,
  49. textsDepartOptions: state => {
  50. if (!state.textsDepart) return undefined
  51. return state.textsDepart.map(({ id, title }) => ({
  52. value: id,
  53. text: `(${id}) ${title}`
  54. }))
  55. },
  56. orderedParents: state => {
  57. // FIXME duplicates references if multiple authors ?
  58. if (!state.textsDepart) return undefined
  59. return state.textsDepart.sort((a, b) => {
  60. if (!b.authors) return -1
  61. if (!a.authors) return +1
  62. if (a.authors[0].name < b.authors[0].name) return -1
  63. if (a.authors[0].name > b.authors[0].name) return 1
  64. return 0
  65. }).reduce((dict, text) => {
  66. const firstChar = text.authors ? text.authors[0].name[0] : '&'
  67. if (!(firstChar in dict)) dict[firstChar] = []
  68. dict[firstChar].push(text)
  69. return dict
  70. }, {})
  71. }
  72. }
  73. }