-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusers.$userId.tsx
More file actions
46 lines (41 loc) · 1.2 KB
/
users.$userId.tsx
File metadata and controls
46 lines (41 loc) · 1.2 KB
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
import { createFileRoute } from '@tanstack/react-router'
import { NotFound } from '../components/NotFound'
import { UserErrorComponent } from '../components/UserError'
import { DEPLOY_URL } from '../utils/users'
import type { User } from '../utils/users'
export const Route = createFileRoute('/users/$userId')({
loader: async ({ params: { userId } }) => {
try {
const res = await fetch(DEPLOY_URL + '/api/users/' + userId)
if (!res.ok) {
throw new Error('Unexpected status code')
}
const data = await res.json()
return data as User
} catch {
throw new Error('Failed to fetch user')
}
},
errorComponent: UserErrorComponent,
component: UserComponent,
notFoundComponent: () => {
return <NotFound>User not found</NotFound>
},
})
function UserComponent() {
const user = Route.useLoaderData()
return (
<div className="space-y-2">
<h4 className="text-xl font-bold underline">{user.name}</h4>
<div className="text-sm">{user.email}</div>
<div>
<a
href={`/api/users/${user.id}`}
className="text-blue-800 hover:text-blue-600 underline"
>
View as JSON
</a>
</div>
</div>
)
}