Skip to content

Commit 02ffc1a

Browse files
committed
add router for examples
1 parent ab430b8 commit 02ffc1a

File tree

23 files changed

+848
-273
lines changed

23 files changed

+848
-273
lines changed

examples/vite/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
},
1414
"dependencies": {
1515
"@tanstack/react-query-devtools": "^5.69.0",
16+
"@tanstack/react-router": "^1.114.29",
1617
"react": "^19.0.0",
1718
"react-dom": "^19.0.0",
1819
"tanstack-query-builder": "^0.9.3",
1920
"tanstack-query-builder-example-mocks": "^0.9.3"
2021
},
2122
"devDependencies": {
23+
"@tanstack/router-plugin": "^1.114.30",
2224
"@types/react": "^19.0.12",
2325
"@types/react-dom": "^19.0.4",
2426
"@vitejs/plugin-react": "^4.3.4",

examples/vite/src/App.tsx

Lines changed: 0 additions & 247 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { useNavigate, useParams } from '@tanstack/react-router';
2+
import { useRef, useState } from 'react';
3+
import { commentsQuery, editArticleMutation, useArticle } from './example';
4+
5+
export function ArticlePage() {
6+
const id = Number.parseInt(useParams({ from: '/main-example/$id' }).id);
7+
const nav = useNavigate();
8+
const article = useArticle({ id });
9+
const comments = commentsQuery.useQuery({ search: { articleId: id } });
10+
const [showEdit, setShowEdit] = useState(false);
11+
const editArticle = editArticleMutation.useMutation();
12+
13+
const titleRef = useRef<HTMLInputElement>(null);
14+
const bodyRef = useRef<HTMLTextAreaElement>(null);
15+
16+
return (
17+
<>
18+
{!showEdit ? (
19+
<>
20+
{article.isLoading ? (
21+
<div className="text-center">Loading...</div>
22+
) : article.isError ? (
23+
<div className="text-red-500">{article.error.message}</div>
24+
) : (
25+
<div className="p-4">
26+
<h2 className="text-2xl font-bold">{article.data?.titleUppercase}</h2>
27+
<p className="mt-2 whitespace-pre-wrap">{article.data?.body}</p>
28+
<button onClick={() => nav({ to: '..' })} className="btn btn-secondary mt-4 mr-2">
29+
Back
30+
</button>
31+
<button onClick={() => setShowEdit(true)} disabled={editArticle.isPending} className="btn btn-primary mt-4">
32+
Edit article
33+
</button>
34+
</div>
35+
)}
36+
</>
37+
) : (
38+
<div className="p-4">
39+
<h2 className="text-2xl font-bold">Edit article</h2>
40+
41+
<input ref={titleRef} defaultValue={article.data?.title} className="block w-full mt-2 p-2 border rounded" />
42+
43+
<textarea ref={bodyRef} defaultValue={article.data?.body} className="block w-full mt-2 p-2 border rounded" />
44+
45+
<button
46+
onClick={() => {
47+
editArticle.mutateAsync({
48+
params: { id },
49+
body: {
50+
id,
51+
title: titleRef.current!.value,
52+
body: bodyRef.current!.value,
53+
},
54+
});
55+
56+
setShowEdit(false);
57+
}}
58+
disabled={editArticle.isPending}
59+
className="btn btn-primary mt-4"
60+
>
61+
Save
62+
</button>
63+
</div>
64+
)}
65+
66+
<h3 className="text-xl font-bold m-4">Comments</h3>
67+
68+
{comments.isLoading ? (
69+
<div className="text-center">Loading comments...</div>
70+
) : comments.isError ? (
71+
<div className="text-red-500">{comments.error.message}</div>
72+
) : (
73+
comments.data?.map((comment) => (
74+
<div key={comment.id} className="p-4 border-b">
75+
<h5 className="text-lg font-bold">{comment.name}</h5>
76+
<p className="mt-2">{comment.body}</p>
77+
</div>
78+
))
79+
)}
80+
</>
81+
);
82+
}

0 commit comments

Comments
 (0)