forked from fullstack-hy2020/misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-app-v2.js
148 lines (129 loc) · 3.48 KB
/
router-app-v2.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
import ReactDOM from 'react-dom/client'
import { useState } from 'react'
import {
BrowserRouter as Router,
Routes,
Route,
Link,
Navigate,
useParams,
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>
<ul>
{notes.map(note =>
<li key={note.id}>
<Link to={`/notes/${note.id}`}>{note.content}</Link>
</li>
)}
</ul>
</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>
username: <input />
</div>
<div>
password: <input type='password' />
</div>
<button type="submit">login</button>
</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 match = useMatch('/notes/:id')
const note = match
? notes.find(note => note.id === Number(match.params.id))
: null
const login = (user) => {
setUser(user)
}
const padding = {
padding: 5
}
return (
<div>
<div>
<Link style={padding} to="/">home</Link>
<Link style={padding} to="/notes">notes</Link>
<Link style={padding} to="/users">users</Link>
{user
? <em>{user} logged in</em>
: <Link style={padding} to="/login">login</Link>
}
</div>
<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>
</div>
)
}
ReactDOM.createRoot(document.getElementById('root')).render(<Router><App /></Router>)