-
Notifications
You must be signed in to change notification settings - Fork 147
/
notes-bootstrap.js
190 lines (172 loc) · 4.72 KB
/
notes-bootstrap.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
import ReactDOM from 'react-dom/client'
import { useState } from 'react'
import { Table, Form, Button, Alert, Navbar, Nav } from 'react-bootstrap'
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>
<Table striped>
<tbody>
{notes.map(note =>
<tr key={note.id}>
<td>
<Link to={`/notes/${note.id}`}>
{note.content}
</Link>
</td>
<td>
{note.user}
</td>
</tr>
)}
</tbody>
</Table>
</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}>
<Form.Group>
<Form.Label>username:</Form.Label>
<Form.Control
type="text"
name="username"
/>
<Form.Label>password:</Form.Label>
<Form.Control
type="password"
/>
<Button variant="primary" type="submit">
login
</Button>
</Form.Group>
</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 (
<div className="container">
{(message &&
<Alert variant="success">
{message}
</Alert>
)}
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="#" as="span">
<Link style={padding} to="/">home</Link>
</Nav.Link>
<Nav.Link href="#" as="span">
<Link style={padding} to="/notes">notes</Link>
</Nav.Link>
<Nav.Link href="#" as="span">
<Link style={padding} to="/users">users</Link>
</Nav.Link>
<Nav.Link href="#" as="span">
{user
? <em>{user} logged in</em>
: <Link to="/login">login</Link>
}
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
<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 2023</em>
</div>
</div>
)
}
ReactDOM.createRoot(document.getElementById('root')).render(
<Router>
<App />
</Router>
)