|
| 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