Skip to content

Instantly share code, notes, and snippets.

@Kaperskyguru
Created July 20, 2020 12:16
Show Gist options
  • Save Kaperskyguru/d36d7734d0458039577e0488ad8b2c72 to your computer and use it in GitHub Desktop.
Save Kaperskyguru/d36d7734d0458039577e0488ad8b2c72 to your computer and use it in GitHub Desktop.
import Vue from "vue";
import Vuex from "vuex";
import { ErrorService } from "./Services/ErrorService";
import axios from "axios";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
todos: [],
errors: [],
users: [],
},
actions: {
async getTodos({ commit }) {
try {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/todos`
);
const { data } = response;
commit("STORE_TODOS", data);
} catch (error) {
// Handling HTTPs Errors
commit("STORE_ERRORS", error);
}
},
async getUsers({ commit }) {
try {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/users`
);
const { data } = response;
commit("STORE_USERS", data);
} catch (error) {
// Handling HTTPs Errors
commit("STORE_ERRORS", error);
}
},
},
mutations: {
STORE_TODOS: (state, data) => {
state.todos = data;
},
STORE_ERRORS: (state, error) => {
// Call Error Service here
ErrorService.onError(error);
ErrorService.initHandler();
// Store error to state(optional)
if (error.response) {
state.errors = error.response;
}
},
STORE_USERS: (state, data) => {
state.users = data;
},
},
getters: {
getTodo: (state) => (id) => {
return state.todos.find((todo) => todo.id == id);
},
getUser: (state) => (id) => {
return state.users.find((user) => user.id == id);
},
},
// strict: true
});
export default store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment