Skip to content

Commit 3013499

Browse files
committed
Use label element with forms and improve code style consistency
1 parent 95d884f commit 3013499

File tree

2 files changed

+104
-155
lines changed

2 files changed

+104
-155
lines changed

src/content/5/en/part5a.md

Lines changed: 56 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ const App = () => {
5151
return (
5252
<div>
5353
<h1>Notes</h1>
54-
5554
<Notification message={errorMessage} />
56-
55+
5756
// highlight-start
57+
<h2>Login</h2>
5858
<form onSubmit={handleLogin}>
5959
<div>
60-
username
61-
<input
60+
<label htmlFor="username">username</label>
61+
<input
6262
type="text"
63+
id="username"
6364
value={username}
64-
name="Username"
6565
onChange={({ target }) => setUsername(target.value)}
6666
/>
6767
</div>
6868
<div>
69-
password
70-
<input
69+
<label htmlFor="password">password</label>
70+
<input
7171
type="password"
72+
id="password"
7273
value={password}
73-
name="Password"
7474
onChange={({ target }) => setPassword(target.value)}
7575
/>
7676
</div>
@@ -129,27 +129,26 @@ const App = () => {
129129
// highlight-start
130130
const [user, setUser] = useState(null)
131131
// highlight-end
132-
133-
// highlight-start
134-
const handleLogin = async (event) => {
132+
133+
// ...
134+
135+
const handleLogin = async event => {
135136
event.preventDefault()
136137

138+
// highlight-start
137139
try {
138-
const user = await loginService.login({
139-
username, password,
140-
})
141-
140+
const user = await loginService.login({ username, password })
142141
setUser(user)
143142
setUsername('')
144143
setPassword('')
145-
} catch (exception) {
146-
setErrorMessage('Wrong credentials')
144+
} catch {
145+
setErrorMessage('wrong credentials')
147146
setTimeout(() => {
148147
setErrorMessage(null)
149148
}, 5000)
150149
}
151-
152-
} // highlight-end
150+
// highlight-end
151+
}
153152

154153
// ...
155154
}
@@ -170,35 +169,32 @@ const App = () => {
170169
const loginForm = () => (
171170
<form onSubmit={handleLogin}>
172171
<div>
173-
username
174-
<input
172+
<label htmlFor="username">username</label>
173+
<input
175174
type="text"
175+
id="username"
176176
value={username}
177-
name="Username"
178177
onChange={({ target }) => setUsername(target.value)}
179178
/>
180179
</div>
181180
<div>
182-
password
183-
<input
181+
<label htmlFor="password">password</label>
182+
<input
184183
type="password"
184+
id="password"
185185
value={password}
186-
name="Password"
187186
onChange={({ target }) => setPassword(target.value)}
188187
/>
189188
</div>
190189
<button type="submit">login</button>
191-
</form>
190+
</form>
192191
)
193192

194193
const noteForm = () => (
195194
<form onSubmit={addNote}>
196-
<input
197-
value={newNote}
198-
onChange={handleNoteChange}
199-
/>
195+
<input value={newNote} onChange={handleNoteChange} />
200196
<button type="submit">save</button>
201-
</form>
197+
</form>
202198
)
203199

204200
return (
@@ -224,25 +220,24 @@ const App = () => {
224220
return (
225221
<div>
226222
<h1>Notes</h1>
227-
228223
<Notification message={errorMessage} />
229224

230-
{user === null && loginForm()} // highlight-line
231-
{user !== null && noteForm()} // highlight-line
225+
{!user && loginForm()} // highlight-line
226+
{user && noteForm()} // highlight-line
232227

233228
<div>
234229
<button onClick={() => setShowAll(!showAll)}>
235230
show {showAll ? 'important' : 'all'}
236231
</button>
237232
</div>
238233
<ul>
239-
{notesToShow.map((note, i) =>
234+
{notesToShow.map(note => (
240235
<Note
241-
key={i}
242-
note={note}
236+
key={note.id}
237+
note={note}
243238
toggleImportance={() => toggleImportanceOf(note.id)}
244239
/>
245-
)}
240+
))}
246241
</ul>
247242

248243
<Footer />
@@ -254,60 +249,32 @@ const App = () => {
254249
A slightly odd looking, but commonly used [React trick](https://react.dev/learn/conditional-rendering#logical-and-operator-) is used to render the forms conditionally:
255250

256251
```js
257-
{
258-
user === null && loginForm()
259-
}
252+
{!user && loginForm()}
260253
```
261254

262255
If the first statement evaluates to false or is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), the second statement (generating the form) is not executed at all.
263256

264-
We can make this even more straightforward by using the [conditional operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator):
265-
266-
```js
267-
return (
268-
<div>
269-
<h1>Notes</h1>
270-
271-
<Notification message={errorMessage}/>
272-
273-
{user === null ?
274-
loginForm() :
275-
noteForm()
276-
}
277-
278-
<h2>Notes</h2>
279-
280-
// ...
281-
282-
</div>
283-
)
284-
```
285-
286-
If _user === null_ is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), _loginForm()_ is executed. If not, _noteForm()_ is.
287-
288257
Let's do one more modification. If the user is logged in, their name is shown on the screen:
289258

290259
```js
291260
return (
292261
<div>
293262
<h1>Notes</h1>
294-
295263
<Notification message={errorMessage} />
296264

297-
{user === null ?
298-
loginForm() :
265+
{!user && loginForm()}
266+
// highlight-start
267+
{user && (
299268
<div>
300-
<p>{user.name} logged-in</p>
269+
<p>{user.name} logged in</p>
301270
{noteForm()}
302271
</div>
303-
}
304-
305-
<h2>Notes</h2>
272+
)}
273+
// highlight-end
306274

275+
<div>
276+
<button onClick={() => setShowAll(!showAll)}>
307277
// ...
308-
309-
</div>
310-
)
311278
```
312279
313280
The solution isn't perfect, but we'll leave it like this for now.
@@ -358,10 +325,10 @@ const getAll = () => {
358325
return request.then(response => response.data)
359326
}
360327

361-
// highlight-start
362328
const create = async newObject => {
329+
// highlight-start
363330
const config = {
364-
headers: { Authorization: token },
331+
headers: { Authorization: token }
365332
}
366333
// highlight-end
367334

@@ -384,16 +351,14 @@ The event handler responsible for login must be changed to call the method <code
384351
```js
385352
const handleLogin = async (event) => {
386353
event.preventDefault()
387-
try {
388-
const user = await loginService.login({
389-
username, password,
390-
})
391354

355+
try {
356+
const user = await loginService.login({ username, password })
392357
noteService.setToken(user.token) // highlight-line
393358
setUser(user)
394359
setUsername('')
395360
setPassword('')
396-
} catch (exception) {
361+
} catch {
397362
// ...
398363
}
399364
}
@@ -435,9 +400,7 @@ Changes to the login method are as follows:
435400
const handleLogin = async (event) => {
436401
event.preventDefault()
437402
try {
438-
const user = await loginService.login({
439-
username, password,
440-
})
403+
const user = await loginService.login({ username, password })
441404

442405
// highlight-start
443406
window.localStorage.setItem(
@@ -468,21 +431,20 @@ We can have multiple effect hooks, so let's create a second one to handle the fi
468431
469432
```js
470433
const App = () => {
471-
const [notes, setNotes] = useState([])
434+
const [notes, setNotes] = useState([])
472435
const [newNote, setNewNote] = useState('')
473436
const [showAll, setShowAll] = useState(true)
474437
const [errorMessage, setErrorMessage] = useState(null)
475-
const [username, setUsername] = useState('')
476-
const [password, setPassword] = useState('')
477-
const [user, setUser] = useState(null)
438+
const [username, setUsername] = useState('')
439+
const [password, setPassword] = useState('')
440+
const [user, setUser] = useState(null)
478441

479442
useEffect(() => {
480-
noteService
481-
.getAll().then(initialNotes => {
482-
setNotes(initialNotes)
483-
})
443+
noteService.getAll().then(initialNotes => {
444+
setNotes(initialNotes)
445+
})
484446
}, [])
485-
447+
486448
// highlight-start
487449
useEffect(() => {
488450
const loggedUserJSON = window.localStorage.getItem('loggedNoteappUser')

0 commit comments

Comments
 (0)