nebula/store/portfolio.js

203 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-08-02 00:25:44 +08:00
import Vue from 'vue';
import { isBrowser } from '~/environment';
2020-08-02 19:23:32 +08:00
export const BASE_API_PATH = '/api/console'
export const PORTFOLIO_API_PATH = '/api/portfolio'
2020-08-02 00:25:44 +08:00
const getDefaultListData = () => {
return {
portfolios: [],
pagination: {}
}
}
export const state = () => {
return {
list: {
fetching: false,
data: getDefaultListData()
},
detail: {
fetching: false,
data: {}
},
articles: {
articles: [],
pagination: {}
2020-08-02 19:23:32 +08:00
},
unbindArticles: {
articles: [],
pagination: {}
2020-08-02 00:25:44 +08:00
}
}
}
export const mutations = {
// 作品集列表
updateListFetching(state, action) {
state.list.fetching = action
},
updateListData(state, action) {
state.list.data = action
},
updateExistingListData(state, action) {
state.list.data.data.push(...action.data)
state.list.data.pagination = action.pagination
},
// 作品集详情
updateDetailFetching(state, action) {
state.detail.fetching = action
},
updateDetailData(state, action) {
state.detail.data = action.portfolio
},
updateArticleList(state, action) {
state.articles.articles = action.articles
state.articles.pagination = action.pagination
},
2020-08-02 19:23:32 +08:00
updateUnbindArticleList(state, action) {
state.unbindArticles.articles = action.articles
state.unbindArticles.pagination = action.pagination
},
2020-08-02 00:25:44 +08:00
// 更新作品集阅读全文状态
updateDetailRenderedState(state, action) {
Vue.set(
state.detail.data,
'isRenderedFullContent',
action == null ? true : action
)
}
}
export const actions = {
// 获取作品集列表
2021-03-06 21:29:35 +08:00
fetchList({commit, state}, params = {}) {
2020-08-02 00:25:44 +08:00
// 清空已有数据
commit('updateListFetching', true)
2021-03-06 21:29:35 +08:00
let currentData = JSON.parse(JSON.stringify(state)).list.data
if (Number(params.page) === currentData.pagination.currentPage) {
commit('updateListFetching', false)
return
}
2020-08-02 00:25:44 +08:00
let data = {
page: params.page,
topicUri: params.topic_uri
}
2021-03-06 21:29:35 +08:00
commit('updateListData', getDefaultListData())
2020-08-02 00:25:44 +08:00
return this.$axios
2020-08-02 19:23:32 +08:00
.$get(`${BASE_API_PATH}/portfolios`, {
2020-08-02 00:25:44 +08:00
params: data
})
.then(response => {
commit('updateListFetching', false)
commit('updateListData', response)
if (isBrowser) {
Vue.nextTick(() => {
window.scrollTo(0,0);
})
}
})
.catch(error => commit('updateListFetching', false))
},
// 获取作品集详情
fetchDetail({ commit }, params = {}) {
// const delay = fetchDelay(
// isBrowser
// )
// if (isBrowser) {
// Vue.nextTick(() => {
// window.scrollTo(0, 300);
// })
// }
commit('updateDetailFetching', true)
// commit('updateDetailData', {})
return this.$axios
2020-08-02 19:23:32 +08:00
.$get(`${BASE_API_PATH}/portfolio/${params.portfolio_id}`)
2020-08-02 00:25:44 +08:00
.then(response => {
return new Promise(resolve => {
commit('updateDetailData', response)
commit('updateDetailFetching', false)
resolve(response)
// delay(() => {
// resolve(response)
// })
})
})
.catch(error => {
commit('updateDetailFetching', false)
return Promise.reject(error)
})
},
fetchArticleList({commit}, params) {
commit('updateDetailFetching', true)
return this.$axios
2020-08-02 19:23:32 +08:00
.$get(`${BASE_API_PATH}/portfolio/${params.portfolio_id}/articles`, {
2020-08-02 00:25:44 +08:00
params: {
2020-08-02 19:23:32 +08:00
page: params.page || 1
2020-08-02 00:25:44 +08:00
}
})
.then(response => {
commit('updateArticleList', response)
commit('updateDetailFetching', false)
})
.catch(error => {
commit('updateDetailFetching', false)
})
2020-08-02 19:23:32 +08:00
},
fetchUnBindArticleList({commit}, params) {
commit('updateDetailFetching', true)
return this.$axios
.$get(`${PORTFOLIO_API_PATH}/${params.portfolio_id}/unbind-articles`, {
params: {
page: params.page || 1,
searchText: params.searchText || ''
}
})
.then(response => {
commit('updateUnbindArticleList', response)
commit('updateDetailFetching', false)
})
.catch(error => {
commit('updateDetailFetching', false)
})
},
fetchPostDetail({ commit }, params = {}) {
// const delay = fetchDelay(
// isBrowser
// )
// if (isBrowser) {
// Vue.nextTick(() => {
// window.scrollTo(0, 300);
// })
// }
if (typeof params.portfolio_id === 'undefined') {
commit('updateDetailData', getDefaultListData())
return;
}
commit('updateDetailFetching', true)
// commit('updateDetailData', {})
return this.$axios
.$get(`${PORTFOLIO_API_PATH}/detail/${params.portfolio_id}`)
.then(response => {
return new Promise(resolve => {
commit('updateDetailData', response)
commit('updateDetailFetching', false)
resolve(response)
// delay(() => {
// resolve(response)
// })
})
})
.catch(error => {
commit('updateDetailFetching', false)
return Promise.reject(error)
})
2020-08-02 00:25:44 +08:00
}
}