This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
frontend-article.js
110 lines (105 loc) · 2.74 KB
/
frontend-article.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function state() {
return {
lists: {
data: [],
hasNext: 0,
page: 1,
path: '',
},
item: {
data: {},
path: '',
isLoad: false,
},
trending: [],
}
}
const actions = {
async getArticleList({ commit, state, rootState: { $api } }, config) {
if (state.lists.data.length > 0 && config.path === state.lists.path && config.page === 1)
return
const { code, data } = await $api.get('frontend/article/list', { ...config, cache: true })
if (data && code === 200) {
commit('receiveArticleList', {
...config,
...data,
})
}
},
async getArticleItem({ commit, state, rootState: { $api } }, config) {
if (config.path === state.item.path)
return
const { code, data } = await $api.get('frontend/article/item', { ...config, markdown: 1, cache: true })
if (data && code === 200) {
commit('receiveArticleItem', {
data,
...config,
})
}
},
async getTrending({ commit, state, rootState: { $api } }) {
if (state.trending.length)
return
const { code, data } = await $api.get('frontend/trending', { cache: true })
if (data && code === 200)
commit('receiveTrending', data)
},
}
const mutations = {
receiveArticleList(state, { list, hasNext, hasPrev, page, path }) {
if (page === 1)
list = [].concat(list)
else
list = state.lists.data.concat(list)
state.lists = {
data: list,
hasNext,
hasPrev,
page,
path,
}
},
receiveArticleItem(state, { data, path }) {
state.item = {
data,
path,
isLoad: true,
}
},
receiveTrending(state, data) {
state.trending = data.list
},
modifyLikeStatus(state, { id, status }) {
if (state.item.data._id === id) {
if (status)
state.item.data.like++
else state.item.data.like--
state.item.data.like_status = status
}
const obj = state.lists.data.find(item => item._id === id)
if (obj) {
if (status)
obj.like++
else obj.like--
obj.like_status = status
}
},
}
const getters = {
getArticleList(state) {
return state.lists
},
getArticleItem(state) {
return state.item
},
getTrending(state) {
return state.trending
},
}
export default {
namespaced: true,
state,
actions,
mutations,
getters,
}