2020-07-31 16:17:12 +08:00
|
|
|
/**
|
|
|
|
* @file 评论数据状态 / ES module
|
|
|
|
* @module store/comment
|
2020-08-01 00:41:54 +08:00
|
|
|
* @author Ronger <https://github.com/ronger-x>
|
2020-07-31 16:17:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
export const COMMENT_API_PATH = '/api/article/'
|
|
|
|
export const LIKE_COMMENT_API_PATH = '/like/comment'
|
|
|
|
|
|
|
|
const getDefaultListData = () => {
|
|
|
|
return {
|
2023-03-24 15:20:25 +08:00
|
|
|
comments: [],
|
2020-07-31 16:17:12 +08:00
|
|
|
pagination: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const state = () => {
|
|
|
|
return {
|
2023-03-24 15:20:25 +08:00
|
|
|
list: {
|
|
|
|
fetching: false,
|
|
|
|
data: getDefaultListData()
|
|
|
|
}
|
2020-07-31 16:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
// 请求列表
|
|
|
|
updateListFetching(state, action) {
|
2023-03-24 15:20:25 +08:00
|
|
|
state.list.fetching = action
|
2020-07-31 16:17:12 +08:00
|
|
|
},
|
|
|
|
updateListData(state, action) {
|
2023-03-24 15:20:25 +08:00
|
|
|
state.list.data = action
|
2020-07-31 16:17:12 +08:00
|
|
|
},
|
|
|
|
clearListData(state) {
|
2023-03-24 15:20:25 +08:00
|
|
|
state.list.data = getDefaultListData()
|
2020-07-31 16:17:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// 发布评论
|
|
|
|
updatePostFetching(state, action) {
|
|
|
|
state.posting = action
|
|
|
|
},
|
|
|
|
updateListNewItemData(state, action) {
|
|
|
|
state.data.pagination.total += 1
|
|
|
|
state.data.data.push(action.result)
|
|
|
|
},
|
|
|
|
|
|
|
|
// 喜欢某条评论
|
|
|
|
updateLikesIncrement(state, action) {
|
|
|
|
state.data.data.find(comment => {
|
|
|
|
const isMatched = comment.id === action.id
|
|
|
|
isMatched && comment.likes++
|
|
|
|
return isMatched
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2023-03-24 15:20:25 +08:00
|
|
|
fetchList({ commit, state }, params = {}) {
|
|
|
|
// 清空已有数据
|
2020-07-31 16:17:12 +08:00
|
|
|
commit('updateListFetching', true)
|
2023-03-24 15:20:25 +08:00
|
|
|
let currentData = JSON.parse(JSON.stringify(state)).list.data
|
|
|
|
if (Number(params.page) === currentData.pageNum) {
|
|
|
|
commit('updateListFetching', false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let data = {
|
|
|
|
page: params.page || 1
|
|
|
|
}
|
|
|
|
commit('updateListData', getDefaultListData())
|
2020-07-31 16:17:12 +08:00
|
|
|
|
|
|
|
return this.$axios
|
2023-03-24 15:20:25 +08:00
|
|
|
.$get(COMMENT_API_PATH + `${params.post_id}/comments`, {
|
|
|
|
params: data
|
|
|
|
})
|
2020-07-31 16:17:12 +08:00
|
|
|
.then(response => {
|
|
|
|
// isDescSort && response.result.data.reverse()
|
|
|
|
commit('updateListData', response)
|
|
|
|
commit('updateListFetching', false)
|
|
|
|
})
|
|
|
|
.catch(error => commit('updateListFetching', false))
|
|
|
|
},
|
|
|
|
|
|
|
|
// 发布评论
|
|
|
|
fetchPostComment({ commit }, comment) {
|
|
|
|
commit('updatePostFetching', true)
|
|
|
|
return this.$axios
|
|
|
|
.$post(COMMENT_API_PATH, comment)
|
|
|
|
.then(response => {
|
|
|
|
commit('updateListNewItemData', response)
|
|
|
|
commit('updatePostFetching', false)
|
|
|
|
return Promise.resolve(response)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
commit('updatePostFetching', false)
|
|
|
|
return Promise.reject(error)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// 喜欢评论
|
|
|
|
fetchLikeComment({ commit }, comment) {
|
|
|
|
return this.$axios
|
|
|
|
.$patch(LIKE_COMMENT_API_PATH, { comment_id: comment.id })
|
|
|
|
.then(response => {
|
|
|
|
commit('updateLikesIncrement', comment)
|
|
|
|
return Promise.resolve(response)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|