update store with data formating and query actions

This commit is contained in:
axolotle
2021-06-04 10:38:33 +02:00
parent 56ad1e3e3d
commit df7f47f718
4 changed files with 344 additions and 3 deletions
+125
View File
@@ -0,0 +1,125 @@
import Vue from 'vue'
import api from '@/api'
import {
AllTags
} from '@/api/queries'
import {
getUniqueNodesIds,
getRelatedNodesIds,
buildTree
} from '@/store/utils.js'
export default {
state: {
tags: undefined,
strangeness: [0, 1, 2, 3, 4],
nodeDepartId: undefined,
trees: {}
},
mutations: {
'SET_TAGS' (state, tags) {
state.tags = tags
},
'SET_NODE_DEPART_ID' (state, id) {
state.nodeDepartId = id
},
'ADD_NODE_TREE' (state, [id, tree]) {
Vue.set(state.trees, id, tree)
}
},
actions: {
async 'INIT_LIBRARY' ({ state, commit, dispatch, rootState }) {
const departIds = await dispatch('GET_ALL_NODES_IDS', 'depart')
await dispatch('GET_NODES', { ids: departIds, dataLevel: 'initial' })
return departIds
},
async 'INIT_LIBRARY_TREE' ({ state, commit, dispatch, rootState }) {
const ids = await dispatch('INIT_LIBRARY')
return dispatch('SET_NODE_DEPART_ID', ids[ids.length - 1])
},
async 'INIT_LIBRARY_MAP' ({ state, commit, dispatch }) {
const departIds = await dispatch('INIT_LIBRARY')
const departNodes = await dispatch('GET_NODES', { ids: departIds.slice(0, 3), dataLevel: 'partial' })
const relatedIds = getRelatedNodesIds(departNodes)
const relatedNodes = await dispatch('GET_NODES', { ids: relatedIds, dataLevel: 'partial' })
return [...departNodes, ...relatedNodes]
},
async 'INIT_LIBRARY_LIST' ({ state, commit, dispatch }) {
const departIds = await dispatch('INIT_LIBRARY')
return dispatch('GET_NODES', { ids: departIds, dataLevel: 'partial' })
},
'GET_ALL_TAGS' ({ state, commit }) {
if (state.tags !== undefined) return state.tags
return api.query(AllTags).then(data => {
commit('SET_TAGS', data.tags)
return state.tags
})
},
async 'SET_NODE_DEPART_ID' ({ state, rootState, commit, dispatch, getters }, id) {
if (state.nodeDepartId === id) return
commit('SET_NODE_DEPART_ID', id)
if (!(id in state.trees)) {
const treeData = await api.queryRecursiveNodes([id])
const nodes = await dispatch('GET_NODES', { ids: getUniqueNodesIds(treeData), dataLevel: 'initial' })
commit('ADD_NODE_TREE', [id, buildTree(treeData, nodes)])
}
return getters.nodeTree
}
},
getters: {
tagsOptions: state => {
if (state.tags === undefined) return
return state.tags.map(tag => ({ value: tag.id, text: tag.name }))
},
nodesDepartsOptions: (state, getters, rootState) => {
const departIds = rootState.ids.depart
if (departIds === undefined || rootState.nodes[departIds[0]] === undefined) return
return departIds.map(id => {
const text = rootState.nodes[id]
const firstAuthor = text.authors !== null ? text.authors[0].name : 'Pad de noms'
return {
text: `${firstAuthor}, ${text.title} (${id})`,
value: id
}
})
},
orderedTextsDepart: (state, getters, rootState) => {
const departIds = rootState.ids.depart
if (departIds === undefined || rootState.nodes[departIds[0]] === undefined) return
return rootState.ids.depart.map(id => rootState.nodes[id]).sort((a, b) => {
if (!b.authors) return -1
if (!a.authors) return +1
if (a.authors[0].last_name < b.authors[0].last_name) return -1
if (a.authors[0].last_name > b.authors[0].last_name) return 1
return 0
}).reduce((dict, text) => {
const firstChar = text.authors ? text.authors[0].last_name[0] : 'Pas de noms'
if (!(firstChar in dict)) dict[firstChar] = []
dict[firstChar].push(text)
return dict
}, {})
},
nodeTree: (state, rootState) => {
if (state.nodeDepartId === undefined) return
return state.trees[state.nodeDepartId]
}
}
}