From 9365e5a78a0e5b35892897a101b00a616f0813ac Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 24 Sep 2022 19:45:30 +0200 Subject: Users store --- store/users.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 store/users.js diff --git a/store/users.js b/store/users.js new file mode 100644 index 0000000..cb9b4b4 --- /dev/null +++ b/store/users.js @@ -0,0 +1,64 @@ +export const state = () => ({ + users: [], +}); + +export const getters = { + users: (state) => { + return state.users; + }, +}; + +export const mutations = { + saveUsers: (state, value) => { + state.users = value; + }, +}; + +export const actions = { + // Search user by `id` + async findById({ commit }, id) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + await fetch(`${api}/v1/users/${id}`, { + headers: { + "Content-Type": "application/json", + }, + }) + .then(async (response) => { + res.status = response.status; + res.data = await response.json(); + }) + .catch((e) => { + res.status = e.status; + }); + + commit("loadingStatus", false, { root: true }); + + return res; + }, + // Search user models + async findModels({ commit }, id) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + await fetch(`${api}/v1/users/${id}/models`, { + headers: { + "Content-Type": "application/json", + }, + }) + .then(async (response) => { + res.status = response.status; + res.data = await response.json(); + }) + .catch((e) => { + res.status = e.status; + }); + + commit("loadingStatus", false, { root: true }); + + return res; + }, +}; -- cgit v1.2.3-18-g5258