-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
69 lines (60 loc) · 1.89 KB
/
App.js
File metadata and controls
69 lines (60 loc) · 1.89 KB
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
// -*-RJSX-*-
import React, { useState } from 'react';
import LC from 'leancloud-storage';
import AppStyles from './App.module.css';
LC.init({
appId: 'B09iIN0UKf2qQIjqKz5WiRnv-gzGzoHsz',
appKey: 'Xes23aMR9VUqzpmEvch8YV4A',
serverURL: 'https://ozewwcws.lc-cn-n1-shared.com'
});
function saveTodo(content) {
const Todo = LC.Object.extend('Todo');
const todo = new Todo();
todo.set('content', content);
todo.set('finished', false);
return todo.save();
}
function loadTodos() {
const query = new LC.Query('Todo');
query.equalTo('finished', false);
query.limit(20);
query.descending('createdAt');
return query.find();
}
function App() {
const [inputValue, setInputValue] = useState('');
const [todos, setTodos] = useState(undefined);
const [error, setError] = useState('');
const addTodo = () => {
saveTodo(inputValue).then(todo => {
setInputValue('');
setTodos(prev => [todo].concat(prev));
}).catch(setError);
};
if (todos === undefined) {
loadTodos().then(setTodos).catch(setError);
}
const toggle = item => {
item.set('finished', !item.get('finished'));
item.save().then(() => setTodos(prev => prev.slice(0))).catch(setError);
};
return (
<div className={AppStyles.app}>
<div className={AppStyles.error}>{error.toString()}</div>
<div className={AppStyles.add}>
<input placeholder="What to do next?" value={inputValue}
onChange={e => setInputValue(e.target.value)}
onKeyUp={e => { if (e.keyCode === 13) addTodo(); } } />
<input type="button" value="↩" />
</div>
<ul>
{todos && todos.map(item =>
<li key={item.getObjectId()} onClick={() => toggle(item)}
data-finished={item.get('finished')}>
{item.get('content')}
</li> )}
</ul>
</div>
);
}
export default App;