corpus.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. },
  10. // getters
  11. getters: {},
  12. // mutations
  13. mutations: {
  14. setAuthors (state, authors) {
  15. state.authors = authors
  16. },
  17. setEditionslist (state, editionslist) {
  18. state.editionslist = editionslist
  19. }
  20. },
  21. // actions
  22. actions: {
  23. getCorpuses ({ dispatch, commit, state }) {
  24. return new Promise((resolve, reject) => {
  25. // get the list of corpuses (aka authors)
  26. dispatch('getAuthors')
  27. .then(({ data }) => {
  28. console.log('getCorpuses authors data', data)
  29. commit('setAuthors', data.content)
  30. // get the texts list for each corpus (aka author)
  31. // let authorsUuids = []
  32. // for (let author of data.content) {
  33. // authorsUuids.push(author.uuid)
  34. // }
  35. dispatch('getEditionsList', data.content)
  36. .then((editionslist) => {
  37. console.log('all texts returned: editionslist', editionslist)
  38. commit('setEditionslist', editionslist)
  39. })
  40. })
  41. })
  42. },
  43. // get authors
  44. getAuthors ({ dispatch, commit, state }) {
  45. return REST.get(`/corpus`, {})
  46. // .then(({ data }) => {
  47. // console.log('corpus getAuthors REST: data', data)
  48. // commit('setAuthors', data.content)
  49. // })
  50. .catch((error) => {
  51. console.warn('Issue with getAuthors', error)
  52. Promise.reject(error)
  53. })
  54. },
  55. // get editionslist
  56. getEditionsList ({ dispatch, commit, state }, authors) {
  57. return Promise.all(authors.map(function (author) {
  58. return REST.get(`/corpus/` + author.uuid, {})
  59. .then(({ data }) => {
  60. console.log('corpus getEditionsList REST: author, data', author, data)
  61. // work arround
  62. if (!Array.isArray(data.content)) {
  63. data.content = [data.content]
  64. }
  65. return {
  66. author: author,
  67. editions: data
  68. }
  69. })
  70. .catch((error) => {
  71. console.warn('Issue with getEditionsList', error)
  72. Promise.reject(error)
  73. })
  74. }))
  75. }
  76. }
  77. }