Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Commit d56479f

Browse files
committed
update store calls
1 parent 9c881e0 commit d56479f

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

components/footer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default {
3333
},
3434
methods: {
3535
removeCompleted () {
36-
this.$store.commit('removeCompleted', this.actives)
36+
this.$store.dispatch('setTodos', this.actives)
3737
}
3838
}
3939
}

components/header.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
addTodo () {
1515
var value = this.todo && this.todo.trim()
1616
if (value) {
17-
this.$store.commit('add', { title: value, completed: this.$route.params.slug === 'completed' })
17+
this.$store.dispatch('addTodo', { title: value, completed: this.$route.params.slug === 'completed' })
1818
this.todo = ''
1919
}
2020
}

pages/_slug.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ export default {
3333
titleTemplate: 'Nuxt TodoMVC : %s todos'
3434
}
3535
},
36-
mounted () {
37-
this.$store.commit('fetch', JSON.parse(localStorage.getItem('nuxt-todos') || '[]'))
38-
},
3936
data () {
4037
return {
4138
editedTodo: null
@@ -60,7 +57,7 @@ export default {
6057
},
6158
methods: {
6259
allDone () {
63-
this.$store.commit('allDone')
60+
this.$store.dispatch('allDone')
6461
},
6562
editTodo (todo) {
6663
this.beforeEditCache = todo.title
@@ -70,19 +67,18 @@ export default {
7067
this.editedTodo = null
7168
todo.title = todo.title.trim()
7269
if (!todo.title) {
73-
this.removeTodo(todo)
70+
this.$store.dispatch('removeTodo', todo)
7471
}
7572
},
7673
cancelEdit (todo) {
7774
this.editedTodo = null
7875
todo.title = this.beforeEditCache
7976
},
8077
removeTodo (todo) {
81-
this.$store.commit('remove', todo)
78+
this.$store.dispatch('removeTodo', todo)
8279
},
8380
save () {
84-
// this.$store.dispatch('saveTodos', this.todos)
85-
localStorage.setItem('nuxt-todos', JSON.stringify(this.todos))
81+
this.$store.dispatch('saveTodos')
8682
}
8783
},
8884
directives: {

store/index.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3+
import axios from 'axios'
34

45
Vue.use(Vuex)
56

@@ -19,25 +20,42 @@ const store = new Vuex.Store({
1920
}
2021
},
2122
mutations: {
22-
fetch (state, todos) {
23+
SET_TODOS (state, todos) {
2324
state.todos = todos
2425
},
25-
add (state, todo) {
26+
ADD_TODO (state, todo) {
2627
state.todos.push(todo)
2728
},
28-
remove (state, todo) {
29+
REMOVE_TODO (state, todo) {
2930
var i = state.todos.indexOf(todo)
3031
state.todos.splice(i, 1)
3132
},
32-
removeCompleted (state, todos) {
33-
state.todos = todos
34-
},
35-
allDone (state) {
36-
var value = state.todos.filter(todo => todo.completed).length === state.todos.length
33+
FILTER_TODOS (state, value) {
3734
state.todos.forEach((todo) => {
3835
todo.completed = !value
3936
})
4037
}
38+
},
39+
actions: {
40+
addTodo ({ commit }, todo) {
41+
commit('ADD_TODO', todo)
42+
},
43+
setTodos ({ commit }, todos) {
44+
commit('SET_TODOS', todos)
45+
},
46+
removeTodo ({ commit }, todo) {
47+
commit('REMOVE_TODO', todo)
48+
},
49+
allDone ({ state, commit }) {
50+
var value = state.todos.filter(todo => todo.completed).length === state.todos.length
51+
commit('FILTER_TODOS', value)
52+
},
53+
saveTodos ({ state }) {
54+
axios.put('/api/todos', { todos: state.todos })
55+
},
56+
nuxtServerInit ({ commit }, { req }) {
57+
commit('SET_TODOS', req.session.todos || [])
58+
}
4159
}
4260
})
4361

0 commit comments

Comments
 (0)