diff options
| author | Zephyrrus <[email protected]> | 2020-07-05 04:17:09 +0300 |
|---|---|---|
| committer | Zephyrrus <[email protected]> | 2020-07-05 04:17:09 +0300 |
| commit | 04fdd63cee5327f49e5e11d5837a9031027c34ef (patch) | |
| tree | 4e965af31eb08d230740dba7104a19124dce3a1e /src/site/store/album.js | |
| parent | chore: change to vue recommended eslint rules + airbnb-base for js (diff) | |
| download | host.fuwn.me-04fdd63cee5327f49e5e11d5837a9031027c34ef.tar.xz host.fuwn.me-04fdd63cee5327f49e5e11d5837a9031027c34ef.zip | |
feat: refactor single album page to use vuex
Diffstat (limited to 'src/site/store/album.js')
| -rw-r--r-- | src/site/store/album.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/site/store/album.js b/src/site/store/album.js index e69de29..f7c88c9 100644 --- a/src/site/store/album.js +++ b/src/site/store/album.js @@ -0,0 +1,54 @@ +/* eslint-disable no-shadow */ +export const state = () => ({ + files: [], + name: null, + isLoading: false, + pagination: { + page: 1, + limit: 30, + totalFiles: 0, + }, + downloadEnabled: false, +}); + +export const getters = { + getTotalFiles: ({ pagination }) => pagination.totalFiles, + getFetchedCount: ({ files }) => files.length, + shouldPaginate: ({ pagination }) => pagination.totalFiles > pagination.limit, + getLimit: ({ pagination }) => pagination.limit, + getName: ({ name }) => name, +}; + +export const actions = { + async fetchById({ commit, dispatch, state }, { id, page }) { + commit('setIsLoading'); + + page = page || 1; + + try { + const response = await this.$axios.$get(`album/${id}/full`, { + params: { limit: state.pagination.limit, page }, + }); + + commit('setFiles', response); + commit('updatePaginationMeta', { totalFiles: response.count, page }); + } catch (e) { + dispatch('alert/set', { text: e.message, error: true }, { root: true }); + } + }, +}; + +export const mutations = { + setIsLoading(state) { + state.isLoading = true; + }, + setFiles(state, { files, name }) { + state.files = files || []; + state.name = name; + state.isLoading = false; + }, + updatePaginationMeta(state, { page, totalFiles }) { + state.pagination.page = page || 1; + state.pagination.totalFiles = totalFiles || 0; + }, +}; |