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/models.js | |
parent | 814dee483b642bcbeee28a4b0c24014d26d794f2 (diff) |
Add store for models
Diffstat (limited to 'store/models.js')
-rw-r--r-- | store/models.js | 53 |
1 files changed, 53 insertions, 0 deletions
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; + }, +}; |