forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
183 lines (162 loc) · 4.17 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* global Vue, Vuex, axios, FileReader, window */
/* eslint-disable no-console */
(function () {
Vue.use(Vuex)
if (window['bootstrap-vue']) {
Vue.use(window['bootstrap-vue'])
}
function randomId () {
return Math.random().toString().substr(2, 10)
}
const store = new Vuex.Store({
state: {
loading: true,
todos: [],
newTodo: '',
},
getters: {
newTodo: (state) => state.newTodo,
todos: (state) => state.todos,
},
mutations: {
SET_LOADING (state, flag) {
state.loading = flag
},
SET_TODOS (state, todos) {
state.todos = todos
},
SET_NEW_TODO (state, todo) {
state.newTodo = todo
},
ADD_TODO (state, todoObject) {
console.log('add todo', todoObject)
state.todos.push(todoObject)
},
REMOVE_TODO (state, todo) {
let todos = state.todos
todos.splice(todos.indexOf(todo), 1)
},
CLEAR_NEW_TODO (state) {
state.newTodo = ''
console.log('clearing new todo')
},
},
actions: {
loadTodos ({ commit }) {
commit('SET_LOADING', true)
console.log('asking for todos')
axios.get('/todos').then((r) => r.data).then((todos) => {
console.log('got %d todos', todos.length)
commit('SET_TODOS', todos)
commit('SET_LOADING', false)
})
},
/**
* Sets text for the future todo
*
* @param {any} { commit }
* @param {string} todo Message
*/
setNewTodo ({ commit }, todo) {
commit('SET_NEW_TODO', todo)
},
addTodo ({ commit, state }) {
if (!state.newTodo) {
// do not add empty todos
return
}
const todo = {
title: state.newTodo,
completed: false,
id: randomId(),
}
axios.post('/todos', todo).then(() => {
commit('ADD_TODO', todo)
})
},
removeTodo ({ commit }, todo) {
axios.delete(`/todos/${todo.id}`).then(() => {
console.log('removed todo', todo.id, 'from the server')
commit('REMOVE_TODO', todo)
})
},
clearNewTodo ({ commit }) {
commit('CLEAR_NEW_TODO')
},
// example promise-returning action
addTodoAfterDelay ({ commit }, { milliseconds, title }) {
return new Promise((resolve) => {
setTimeout(() => {
const todo = {
title,
completed: false,
id: randomId(),
}
commit('ADD_TODO', todo)
resolve()
}, milliseconds)
})
},
},
})
// app Vue instance
const app = new Vue({
store,
data: {
file: null,
},
el: '.todoapp',
created () {
console.log('created Vue app')
this.$store.dispatch('loadTodos')
},
mounted () {
console.log('Vue component mounted')
},
beforeUpdate () {
console.log('Vue component beforeUpdate')
},
updated () {
console.log('Vue component updated')
},
// computed properties
// https://vuejs.org/guide/computed.html
computed: {
newTodo () {
return this.$store.getters.newTodo
},
todos () {
return this.$store.getters.todos
},
},
// methods that implement data logic.
// note there's no DOM manipulation here at all.
methods: {
setNewTodo (e) {
this.$store.dispatch('setNewTodo', e.target.value)
},
addTodo (e) {
e.target.value = ''
this.$store.dispatch('addTodo')
this.$store.dispatch('clearNewTodo')
},
removeTodo (todo) {
this.$store.dispatch('removeTodo', todo)
},
uploadTodos (e) {
// either set component data.file to test file
// or read it off the native event
const f = this.file || e.target.files[0]
const reader = new FileReader()
reader.onload = (e) => {
const list = JSON.parse(e.target.result)
list.forEach((todo) => {
this.$store.commit('ADD_TODO', todo)
})
}
reader.readAsText(f)
},
},
})
window.app = app
})()