diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-09-18 14:24:16 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-18 14:24:16 +0200 |
commit | 683cd8be5d6cf42cce58eae4134459ee60658e8c (patch) | |
tree | 82fc6732a704ec260bb77515249ba68703489464 /store | |
parent | 814dee483b642bcbeee28a4b0c24014d26d794f2 (diff) |
Add store for models
Diffstat (limited to 'store')
-rw-r--r-- | store/index.js | 4 | ||||
-rw-r--r-- | store/models.js | 53 |
2 files changed, 55 insertions, 2 deletions
diff --git a/store/index.js b/store/index.js index eabe2e5..a91e3a7 100644 --- a/store/index.js +++ b/store/index.js @@ -3,8 +3,8 @@ export const state = () => ({ }); export const getters = { - animation: (state) => { - return state.loading ? "animate-none" : "animate-spin"; + isLoading: (state) => { + return state.loading; }, }; export const mutations = { diff --git a/store/models.js b/store/models.js new file mode 100644 index 0000000..b1b0b95 --- /dev/null +++ b/store/models.js @@ -0,0 +1,53 @@ +export const state = () => ({ + models: [], + count: 0, +}); + +export const getters = { + models: (state) => { + return state.models; + }, + count: (state) => { + return state.count; + }, +}; + +export const mutations = { + // Save results and count + saveModels: (state, value) => { + state.models = value.results; + state.count = value.count; + }, +}; + +export const actions = { + // Get models usign `page` + async getModels({ commit, getters }, page) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + page = page ? page : 1; + + await fetch(`${api}/v1/models?page=${page}`, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getters.accessToken}`, + }, + }) + .then(async (response) => { + res.data = await response.json(); + res.status = response.status; + if (res.status == 200) { + commit("saveModels", res.data); + } + }) + .catch((e) => { + res.status = e.status; + }); + + commit("loadingStatus", false, { root: true }); + + return res; + }, +}; |