-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path[id].jsx
73 lines (63 loc) · 2.09 KB
/
[id].jsx
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
// See https://nextjs.org/docs/pages/building-your-application/data-fetching/forms-and-mutations
import logger from '../../../lib/logger.js'
import getDatabase from '../../../lib/database.js'
// See https://react.dev/reference/react/useState
import { useState } from 'react'
export default function Page({ user }) {
const [errorState, setErrorState] = useState(null)
async function onSubmit(event) {
event.preventDefault()
try {
const response = await fetch("/api/users/" + user.id, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
firstName: event.target.firstName.value,
lastName: event.target.lastName.value,
age: event.target.age.value
})
})
if (response.ok !== true) {
throw Error('Failed to update the data: ' + await response.text())
}
} catch (error) {
setErrorState(error.message)
}
}
return (
<form onSubmit={onSubmit}>
<p>
Try updating any of the fields. An update to the "age" field
will result in an error.
</p>
{errorState && <p style={{ color: 'red' }}>{errorState}</p>}
<div>
<label htmlFor={"user.firstName"}>First name: </label>
<input id={"user.firstName"} name={"firstName"} defaultValue={user.firstName}></input>
</div>
<div>
<label htmlFor={"user.lastName"}>Last name: </label>
<input id={"user.lastName"} name={"lastName"} defaultValue={user.lastName}></input>
</div>
<div>
<label htmlFor={"user.age"}>Age: </label>
<input id={"user.age"} name={"age"} defaultValue={user.age}></input>
</div>
<button type="submit">Update</button>
</form>
)
}
export async function getServerSideProps(context) {
logger.info('rendering user page')
const db = await getDatabase()
const user = db.userById(context.params.id)
if (user === undefined) {
logger.error('cannot find user with id: %s', context.params.id)
return { notFound: true }
}
return {
props: { user }
}
}