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'
import { lazy } from '~/common/utils'
import { CancelToken } from 'axios'
export default {
// =================================================
// State
// =================================================
state: () => {
const state = {
// Learn more on https://nuxtjs.org/guide/vuex-store
// =================================================
// State
// =================================================
export const state = () => {
const s = {
items: {
/* [id: number]: Item */
},
......@@ -22,15 +23,39 @@ export default {
}
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)
},
// =================================================
// Actions
// =================================================
actions: {
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 */
}
}
// =================================================
// Actions
// =================================================
export const actions = {
FETCH_FEED({ commit, state }, { feed, page, prefetch }) {
// Don't priorotize already fetched feeds
if (state.feeds[feed][page] && state.feeds[feed][page].length) {
......@@ -74,28 +99,4 @@ export default {
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