/
index.js
74 lines (62 loc) · 2.09 KB
/
index.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
import React, {useState} from 'react';
import {ScrollView} from 'react-native';
import {useSelector, useDispatch} from 'react-redux';
import {useNavigation} from 'react-navigation-hooks';
import {View, Appbar, TextInput, Button} from 'view/components';
import Todo from './Todo';
import {t} from 'i18n';
import style from 'view/style';
import {selectTodos} from 'store/selectors/todos';
import {addTodo, toggleTodo, deleteTodo} from 'store/reducers/todos';
const Todos = () => {
const {appStyle, todosStyle} = style;
const dispatch = useDispatch();
const {goBack} = useNavigation();
const [todoText, setTodoText] = useState('');
const todos = useSelector(selectTodos);
const _addTodo = () => {
if (todoText !== '') {
dispatch(addTodo(todoText));
setTodoText('');
}
};
const _goBack = () => goBack();
const _toggleTodo = id => dispatch(toggleTodo(id));
const _deleteTodo = id => dispatch(deleteTodo(id));
return (
<View style={appStyle.container}>
<Appbar.Header style={appStyle.header}>
<Appbar.Action onPress={() => _goBack()} icon="arrow-back" />
<Appbar.Content title={t('todos.title')} />
</Appbar.Header>
<View style={appStyle.content}>
<View style={todosStyle.addTodo}>
<TextInput
onChangeText={text => setTodoText(text)}
value={todoText}
mode="outlined"
selectionColor="#13a77f"
style={todosStyle.addTodoText}
/>
<Button mode="outlined" color="#13a77f" onPress={() => _addTodo()}>
{t('todos.addTodo')}
</Button>
</View>
<View style={todosStyle.todos}>
<ScrollView scrollEnabled={false}>
{todos.map(todo => (
<Todo
key={todo.id}
text={todo.text}
completed={todo.completed}
onClick={() => _toggleTodo({id: todo.id})}
onDelete={() => _deleteTodo({id: todo.id})}
/>
))}
</ScrollView>
</View>
</View>
</View>
);
};
export default Todos;