-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
313 lines (277 loc) · 7.49 KB
/
index.html
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="https://tylermcginnis.com/goals-todos-api/index.js"></script>
<script src="https://unpkg.com/[email protected]/dist/redux-thunk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-redux.min.js"></script>
<title>To-Do</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript">
function generateId() {
return Math.random().toString(36).substring(2) + new Date().getTime().toString(36);
}
// Library Code: removed add redux file
// App Code
const ADD_TODO = "ADD_TODO";
const REMOVE_TODO = "REMOVE_TODO";
const TOGGLE_TODO = "TOGGLE_TODO";
const ADD_GOAL = "ADD_GOAL";
const REMOVE_GOAL = "REMOVE_GOAL";
const RECEIVE_DATA = "RECEIVE_DATA";
function addTodoAction(todo) {
return {
type: ADD_TODO,
todo,
};
}
function removeTodoAction(id) {
return {
type: REMOVE_TODO,
id,
};
}
function toggleTodoAction(id) {
return {
type: TOGGLE_TODO,
id,
};
}
function addGoalAction(goal) {
return {
type: ADD_GOAL,
goal,
};
}
function removeGoalAction(id) {
return {
type: REMOVE_GOAL,
id,
};
}
function receiveDataAction(todos, goals) {
return {
type: RECEIVE_DATA,
todos,
goals,
};
}
function handleAddTodo(name, cb) {
return (dispatch) => {
return API.saveTodo(name)
.then((todo) => {
dispatch(addTodoAction(todo));
cb();
})
.catch(() => {
alert("There was an error. Try again.");
});
};
}
function handleDeleteTodo(todo) {
return (dispatch) => {
dispatch(removeTodoAction(todo.id));
return API.deleteTodo(todo.id).catch(() => {
dispatch(addTodoAction(todo));
alert("an error occurred. Try again");
});
};
}
function handleToggle(id) {
return (dispatch) => {
dispatch(toggleTodoAction(id));
return API.saveTodoToggle(id).catch(() => {
dispatch(toggleTodoAction(id));
alert("An error occurred. Try again.");
});
};
}
function handleAddGoal(name, cb) {
return (dispatch) => {
return API.saveGoal(name)
.then((goal) => {
dispatch(addGoalAction(goal));
cb();
})
.catch(() => alert("There was an error. Try again."));
};
}
function handleDeleteGoal(goal) {
return (dispatch) => {
dispatch(removeGoalAction(goal.id));
return API.deleteGoal(goal.id).catch(() => {
dispatch(addGoalAction(goal));
alert("An error occurred. Try again.");
});
};
}
function handleInitialData() {
return (dispatch) => {
return Promise.all([API.fetchTodos(), API.fetchGoals()]).then(([todos, goals]) => {
dispatch(receiveDataAction(todos, goals));
});
};
}
function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return state.concat([action.todo]);
case REMOVE_TODO:
return state.filter((todo) => todo.id !== action.id);
case TOGGLE_TODO:
return state.map((todo) =>
todo.id !== action.id ? todo : Object.assign({}, todo, { complete: !todo.complete })
);
case RECEIVE_DATA:
return action.todos;
default:
return state;
}
}
function goals(state = [], action) {
switch (action.type) {
case ADD_GOAL:
return state.concat([action.goal]);
case REMOVE_GOAL:
return state.filter((goal) => goal.id !== action.id);
case RECEIVE_DATA:
return action.goals;
default:
return state;
}
}
function loading(state = true, action) {
switch (action.type) {
case RECEIVE_DATA:
return false;
default:
return state;
}
}
const checker = (store) => (next) => (action) => {
if (action.type === ADD_TODO && action.todo.name.toLowerCase().includes("bitcoin")) {
return alert("Nope, that's a bad ideas.");
}
if (action.type === ADD_GOAL && action.goal.name.toLowerCase().includes("bitcoin")) {
return alert("Nope, that's a bad ideas.");
}
return next(action);
};
const logger = (store) => (next) => (action) => {
console.group(action.type);
console.log("The action: ", action);
const result = next(action);
console.log("The new state:", store.getState());
console.groupEnd();
return result;
};
const store = Redux.createStore(
Redux.combineReducers({
todos,
goals,
loading,
}),
Redux.applyMiddleware(ReduxThunk.default, checker, logger)
);
</script>
<script type="text/babel">
function List(props) {
return (
<ul>
{props.items.map((item) => (
<li key={item.id}>
<span
onClick={() => props.toggle && props.toggle(item.id)}
style={{ textDecoration: item.complete ? "line-through" : "none" }}
>
{item.name}
</span>
<button onClick={() => props.remove(item)}>X</button>
</li>
))}
</ul>
);
}
class Todos extends React.Component {
addItem = (e) => {
e.preventDefault();
this.props.dispatch(handleAddTodo(this.input.value, () => (this.input.value = "")));
};
removeItem = (todo) => {
this.props.dispatch(handleDeleteTodo(todo));
};
toggleItem = (id) => {
this.props.dispatch(handleToggle(id));
};
render() {
return (
<div>
<h1>Todo List</h1>
<input type="text" placeholder="add todo" ref={(input) => (this.input = input)} />
<button onClick={this.addItem}>Add ToDo</button>
<List toggle={this.toggleItem} remove={this.removeItem} items={this.props.todos} />
</div>
);
}
}
const ConnectedTodos = ReactRedux.connect((state) => ({
todos: state.todos,
}))(Todos);
class Goals extends React.Component {
addItem = (e) => {
e.preventDefault();
this.props.dispatch(handleAddGoal(this.input.value, () => (this.input.value = "")));
};
removeItem = (goal) => {
this.props.dispatch(handleDeleteGoal(goal));
};
render() {
return (
<div>
<h1>Goals</h1>
<input type="text" placeholder="add goals" ref={(input) => (this.input = input)} />
<button onClick={this.addItem}>Add Goals</button>
<List remove={this.removeItem} items={this.props.goals} />
</div>
);
}
}
const ConnectedGoals = ReactRedux.connect((state) => ({
goals: state.goals,
}))(Goals);
class App extends React.Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch(handleInitialData());
}
render() {
if (this.props.loading === true) {
return <h3>Loading...</h3>;
}
return (
<div>
<ConnectedTodos />
<ConnectedGoals />
</div>
);
}
}
const ConnectedApp = ReactRedux.connect((state) => ({
loading: state.loading,
}))(App);
ReactDOM.render(
<ReactRedux.Provider store={store}>
<ConnectedApp />
</ReactRedux.Provider>,
document.getElementById("app")
);
</script>
</body>
</html>