From 683cd8be5d6cf42cce58eae4134459ee60658e8c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 18 Sep 2022 14:24:16 +0200 Subject: Add store for models --- store/index.js | 4 ++-- store/models.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 store/models.js (limited to 'store') 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; + }, +}; -- cgit v1.2.3-18-g5258