blog-client/store/index.js
2019-12-13 11:46:15 +08:00

64 lines
1.5 KiB
JavaScript

export const state = () => ({
counter: 0,
newComments: [],
hotArticle: [],
categorys: [],
tags: []
})
export const mutations = {
increment (state) {
state.counter++
},
setLeftDatas (state, data) {
const NewComments = data[0]
const HotArticle = data[1]
const terms = data[2]
for (const n in NewComments) {
NewComments[n].comment_date = this.$moment(NewComments[n].comment_date).format('YYYY-MM-DD')
}
state.newComments = NewComments
for (const n in HotArticle) {
HotArticle[n].post_modified = this.$moment(HotArticle[n].post_modified).format('MMM Do YY')
}
state.hotArticle = HotArticle
const categorys = []
const tags = []
for (const n in terms) {
if (terms[n].term_taxonomy.taxonomy === 'category') {
categorys.push(terms[n])
} else {
tags.push(terms[n])
}
}
state.categorys = categorys
state.tags = tags
}
}
export const actions = {
async nuxtServerInit ({ state, commit }, { $axios }) {
if (state.tags.length === 0) {
try {
const datas = await Promise.all([
$axios.$get('/public/comments/list'),
$axios.$get('/public/article/hot'),
$axios.$get('/public/terms/all')
])
for (const n in datas) {
datas[n] = datas[n].result
}
commit('setLeftDatas', datas)
} catch (err) {
console.log(err && err.response && err.response.data)
}
}
}
}