forked from guiseek/nx-clean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.tsx
186 lines (165 loc) · 4.84 KB
/
app.tsx
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
import React, { useEffect } from 'react';
import {
Grid,
Stack,
Button,
TextField,
Checkbox,
FormGroup,
Typography,
ToggleButton,
FormControlLabel,
ToggleButtonGroup,
} from '@mui/material';
import { NavigateBefore, Clear } from '@material-ui/icons';
import { ThemeProvider } from '@mui/material/styles';
import { TodoVM, TodoPresenter } from '@nx-clean/todo-presentation';
import { injector } from './app.provider';
import { theme } from './app.theme';
import styles from './app.module.scss';
const presenter = injector.get<TodoPresenter>(TodoPresenter);
export function App() {
const [value, setValue] = React.useState('');
const [state, setState] = React.useState<TodoVM[]>([]);
const [buttonGroup, setButtonGroup] = React.useState('showAll');
useEffect(() => {
presenter.getAllTodos().subscribe(setState).unsubscribe();
const subscription = presenter.todos$.subscribe(setState);
return () => subscription.unsubscribe();
}, []);
function handleAddTodo(event: React.KeyboardEvent) {
if (event.key === 'Enter') {
presenter.addTodo(value);
setValue('');
}
}
function onToggleItem(input: HTMLInputElement, id: string) {
if (input.checked) {
presenter.markTodoAsCompleted(id);
} else {
presenter.markTodoAsActive(id);
}
}
function onShowAll() {
presenter.getAllTodos();
}
function onShowActive() {
presenter.getActiveTodos();
}
function onShowCompleted() {
presenter.getCompletedTodos();
}
function onClearCompleted() {
presenter.removeCompletedTodos();
}
function onRemoveItem(todo: TodoVM) {
presenter.removeTodo(todo.id);
}
function handleButtonGroup(
event: React.MouseEvent<HTMLElement>,
newAlignment: string
) {
setButtonGroup(newAlignment);
}
const renderTodoItem = (todo: TodoVM) => {
const label = { inputProps: { 'aria-label': todo.name } };
return (
<Grid key={todo.id} container alignItems="center">
<FormControlLabel
value={todo.name}
control={
<Checkbox
{...label}
checked={todo.completed}
onChange={(event) => onToggleItem(event.target, todo.id)}
/>
}
label={todo.name}
/>
<Clear onClick={() => onRemoveItem(todo)} sx={{ cursor: 'pointer' }} />
</Grid>
);
};
return (
<ThemeProvider theme={theme}>
<div className={styles.app}>
<Grid
p="20px"
container
justifyContent="space-between"
sx={{ backgroundColor: 'primary.dark' }}
alignItems="center"
>
<Button variant="text" href="/" color="secondary">
<NavigateBefore />
<span>Voltar</span>
</Button>
<Grid item>
<Typography variant="h5" color="white">
React - <span className="opacity">Clean Archtecture .design</span>
</Typography>
</Grid>
</Grid>
<main>
<TextField
type="text"
autoFocus
fullWidth
value={value}
onChange={(event) => setValue(event.target.value)}
onKeyPress={(event) => handleAddTodo(event)}
id="outline-required"
label="O que falta finalizar?"
variant="outlined"
InputLabelProps={{
shrink: true,
}}
/>
<hr />
<Stack spacing={2}>
<FormGroup>{state.map(renderTodoItem)}</FormGroup>
</Stack>
<hr />
</main>
<footer className="footer">
<p className="todo-count">
Faltam {state.filter((value) => value.completed === false).length}{' '}
itens pra finalizar
</p>
<ToggleButtonGroup
color="primary"
exclusive
value={buttonGroup}
onChange={handleButtonGroup}
aria-label="outlined button group"
>
<ToggleButton value="showAll" onClick={onShowAll}>
Todos
</ToggleButton>
<ToggleButton value="showActive" onClick={onShowActive}>
Ativos
</ToggleButton>
<ToggleButton value="showCompleted" onClick={onShowCompleted}>
Finalizados
</ToggleButton>
</ToggleButtonGroup>
<hr />
<Button variant="contained" onClick={onClearCompleted}>
Limpar finalizados
</Button>
</footer>
<div className="repo-link">
<Button
variant="text"
target="_blank"
rel="noopener noreferrer"
href="https://github.com/guiseek/nx-clean/blob/main/apps/todo/react/src/app"
>
Source code
</Button>
</div>
</div>
</ThemeProvider>
);
}
export default App;