-
Notifications
You must be signed in to change notification settings - Fork 147
/
notes-materialui.js
199 lines (177 loc) · 4.56 KB
/
notes-materialui.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import ReactDOM from 'react-dom/client'
import { useState } from 'react'
import {
Container,
Table,
TableBody,
TableCell,
TableContainer,
TableRow,
Paper,
TextField,
Button,
AppBar,
Toolbar,
Alert
} from '@mui/material'
import {
BrowserRouter as Router,
Routes,
Route,
Link,
Navigate,
useNavigate,
useMatch
} from "react-router-dom"
const Home = () => (
<div>
<h2>TKTL notes app</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
)
const Note = ({ note }) => {
return (
<div>
<h2>{note.content}</h2>
<div>{note.user}</div>
<div><strong>{note.important ? 'important' : ''}</strong></div>
</div>
)
}
const Notes = ({ notes }) => (
<div>
<h2>Notes</h2>
<TableContainer component={Paper}>
<Table>
<TableBody>
{notes.map(note => (
<TableRow key={note.id}>
<TableCell>
<Link to={`/notes/${note.id}`}>{note.content}</Link>
</TableCell>
<TableCell>
{note.name}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div>
)
const Users = () => (
<div>
<h2>TKTL notes app</h2>
<ul>
<li>Matti Luukkainen</li>
<li>Juha Tauriainen</li>
<li>Arto Hellas</li>
</ul>
</div>
)
const Login = (props) => {
const navigate = useNavigate()
const onSubmit = (event) => {
event.preventDefault()
props.onLogin('mluukkai')
navigate('/')
}
return (
<div>
<h2>login</h2>
<form onSubmit={onSubmit}>
<div>
<TextField label="username" />
</div>
<div>
<TextField label="password" type='password' />
</div>
<div>
<Button variant="contained" color="primary" type="submit">
login
</Button>
</div>
</form>
</div>
)
}
const App = () => {
const [notes, setNotes] = useState([
{
id: 1,
content: 'HTML is easy',
important: true,
user: 'Matti Luukkainen'
},
{
id: 2,
content: 'Browser can execute only JavaScript',
important: false,
user: 'Matti Luukkainen'
},
{
id: 3,
content: 'Most important methods of HTTP-protocol are GET and POST',
important: true,
user: 'Arto Hellas'
}
])
const [user, setUser] = useState(null)
const [message, setMessage] = useState(null)
const match = useMatch('/notes/:id')
const note = match
? notes.find(note => note.id === Number(match.params.id))
: null
const login = (user) => {
setUser(user)
setMessage(`welcome ${user}`)
setTimeout(() => {
setMessage(null)
}, 10000)
}
const padding = {
padding: 5
}
return (
<Container>
<div>
{(message &&
<Alert severity="success">
{message}
</Alert>
)}
</div>
<AppBar position="static">
<Toolbar>
<Button color="inherit" component={Link} to="/">
home
</Button>
<Button color="inherit" component={Link} to="/notes">
notes
</Button>
<Button color="inherit" component={Link} to="/users">
users
</Button>
{user
? <em>{user} logged in</em>
: <Button color="inherit" component={Link} to="/login">
login
</Button>
}
</Toolbar>
</AppBar>
<Routes>
<Route path="/notes/:id" element={<Note note={note} />} />
<Route path="/notes" element={<Notes notes={notes} />} />
<Route path="/users" element={user ? <Users /> : <Navigate replace to="/login" />} />
<Route path="/login" element={<Login onLogin={login} />} />
<Route path="/" element={<Home />} />
</Routes>
<div>
<br />
<em>Note app, Department of Computer Science 2022</em>
</div>
</Container>
)
}
ReactDOM.createRoot(document.getElementById('root')).render(<Router><App /></Router>)