corpus.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { REST } from 'api/rest-axios'
  2. export default {
  3. namespaced: true,
  4. // initial state
  5. state: {
  6. // items: [],
  7. authors: [],
  8. editionslist: [],
  9. editionsbyuuid: {}
  10. },
  11. // getters
  12. getters: {},
  13. // mutations
  14. mutations: {
  15. setAuthors (state, authors) {
  16. state.authors = authors
  17. },
  18. setEditionslist (state, editionslist) {
  19. state.editionslist = editionslist
  20. },
  21. setEditionsByUUID (state, editionlist) {
  22. for (var i = 0; i < editionlist.length; i++) {
  23. for (var j = 0; j < editionlist[i].editions.content.length; j++) {
  24. state.editionsbyuuid[editionlist[i].editions.content[j].uuid] = editionlist[i].editions.content[j]
  25. }
  26. }
  27. console.log('corpus editionsbyuuid', state.editionsbyuuid)
  28. }
  29. },
  30. // actions
  31. actions: {
  32. getCorpuses ({ dispatch, commit, state }) {
  33. return new Promise((resolve, reject) => {
  34. // get the list of corpuses (aka authors)
  35. dispatch('getAuthors')
  36. .then(({ data }) => {
  37. console.log('getCorpuses authors data', data)
  38. commit('setAuthors', data.content)
  39. // get the texts list for each corpus (aka author)
  40. // let authorsUuids = []
  41. // for (let author of data.content) {
  42. // authorsUuids.push(author.uuid)
  43. // }
  44. dispatch('getEditionsList', data.content)
  45. .then((editionslist) => {
  46. console.log('all texts returned: editionslist', editionslist)
  47. commit('setEditionslist', editionslist)
  48. commit('setEditionsByUUID', editionslist)
  49. })
  50. })
  51. })
  52. },
  53. // get authors
  54. getAuthors ({ dispatch, commit, state }) {
  55. return REST.get(`${apipath}/corpus`, {})
  56. // .then(({ data }) => {
  57. // console.log('corpus getAuthors REST: data', data)
  58. // commit('setAuthors', data.content)
  59. // })
  60. .catch((error) => {
  61. console.warn('Issue with getAuthors', error)
  62. Promise.reject(error)
  63. })
  64. },
  65. // get editionslist
  66. getEditionsList ({ dispatch, commit, state }, authors) {
  67. return Promise.all(authors.map(function (author) {
  68. return REST.get(`${apipath}/corpus/` + author.uuid, {})
  69. .then(({ data }) => {
  70. console.log('corpus getEditionsList REST: author, data', author, data)
  71. // work arround
  72. if (!Array.isArray(data.content)) {
  73. data.content = [data.content]
  74. }
  75. return {
  76. author: author,
  77. editions: data
  78. }
  79. })
  80. .catch((error) => {
  81. console.warn('Issue with getEditionsList', error)
  82. Promise.reject(error)
  83. })
  84. }))
  85. }
  86. }
  87. }