Commit 328ef229 authored by Sébastien Chopin's avatar Sébastien Chopin

chore: Refactor store for better lisibility

parent 3f5ae21d
...@@ -4,12 +4,13 @@ import { validFeeds } from '~/common/api' ...@@ -4,12 +4,13 @@ import { validFeeds } from '~/common/api'
import { lazy } from '~/common/utils' import { lazy } from '~/common/utils'
import { CancelToken } from 'axios' import { CancelToken } from 'axios'
export default { // Learn more on https://nuxtjs.org/guide/vuex-store
// =================================================
// State // =================================================
// ================================================= // State
state: () => { // =================================================
const state = { export const state = () => {
const s = {
items: { items: {
/* [id: number]: Item */ /* [id: number]: Item */
}, },
...@@ -22,15 +23,39 @@ export default { ...@@ -22,15 +23,39 @@ export default {
} }
validFeeds.forEach((feed) => { validFeeds.forEach((feed) => {
state.feeds[feed] = {} s.feeds[feed] = {}
}) })
return state return s
}
// =================================================
// Mutations
// =================================================
export const mutations = {
SET_FEED: (state, { feed, ids, page }) => {
Vue.set(state.feeds[feed], page, ids)
}, },
// ================================================= SET_ITEM: (state, { item }) => {
// Actions if (item) {
// ================================================= Vue.set(state.items, item.id, item)
actions: { }
},
SET_ITEMS: (state, { items }) => {
items.forEach((item) => {
if (item) {
Vue.set(state.items, item.id, item)
}
})
},
SET_USER: (state, { id, user }) => {
Vue.set(state.users, id, user || false) /* false means user not found */
}
}
// =================================================
// Actions
// =================================================
export const actions = {
FETCH_FEED({ commit, state }, { feed, page, prefetch }) { FETCH_FEED({ commit, state }, { feed, page, prefetch }) {
// Don't priorotize already fetched feeds // Don't priorotize already fetched feeds
if (state.feeds[feed][page] && state.feeds[feed][page].length) { if (state.feeds[feed][page] && state.feeds[feed][page].length) {
...@@ -74,28 +99,4 @@ export default { ...@@ -74,28 +99,4 @@ export default {
Object.assign({ id, loading: true }, state.users[id]) Object.assign({ id, loading: true }, state.users[id])
) )
} }
},
// =================================================
// Mutations
// =================================================
mutations: {
SET_FEED: (state, { feed, ids, page }) => {
Vue.set(state.feeds[feed], page, ids)
},
SET_ITEM: (state, { item }) => {
if (item) {
Vue.set(state.items, item.id, item)
}
},
SET_ITEMS: (state, { items }) => {
items.forEach((item) => {
if (item) {
Vue.set(state.items, item.id, item)
}
})
},
SET_USER: (state, { id, user }) => {
Vue.set(state.users, id, user || false) /* false means user not found */
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment