Skip to content

Instantly share code, notes, and snippets.

@oomusou
Created September 25, 2018 05:53
Show Gist options
  • Save oomusou/642b4d613e44e881872ad71684ebbd63 to your computer and use it in GitHub Desktop.
Save oomusou/642b4d613e44e881872ad71684ebbd63 to your computer and use it in GitHub Desktop.
import Vue from 'vue';
import Vuex from 'vuex';
import todoApi from '../../api/todosApi';
Vue.use(Vuex);
const setTodos = (state, payload) => state.todos = payload;
const addItem = (state, payload) =>
state.todos.push({ title: payload, completed: false });
const finishItem = (state, payload) =>
state.todos[payload].completed = !state.todos[payload].completed;
const error = e => console.log(e);
const response = commit =>
res => commit('setTodos', res.data.slice(0, 5));
const getTodos = ({ commit }) =>
todoApi
.fetchTodos()
.then(response(commit))
.catch(error);
const state = {
todos: [],
};
const mutations = {
setTodos,
addItem,
finishItem,
};
const actions = {
getTodos,
};
export default {
namespaced: true,
state,
mutations,
actions,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment