@@ -70,53 +70,53 @@ export default function PostPage({
7070 const queryClient = useQueryClient ( ) ;
7171 const { id : postId } = useParams ( ) ;
7272
73- //load post - with initial data from SSR
73+ //load post - with initial data from server
7474 const {
7575 data : post ,
7676 isLoading : isLoadingPost ,
7777 isError : isErrorLoadingPosts ,
7878 } = useQuery ( {
79- queryKey : [ "post" , postId ] ,
79+ initialData : initialPost ,
80+ queryKey : [ `/posts/${ postId } ` ] ,
8081 queryFn : async ( ) => {
8182 const response = await fetch ( `http://localhost:3333/posts/${ postId } ` ) ;
8283 return response . json ( ) as Promise < IPost > ;
8384 } ,
84- initialData : initialPost , //SSR, with refresh
8585 } ) ;
8686
87- //load comments - with initial data from SSR
87+ //load user - depends on user id from post
8888 const {
89- data : comments ,
90- isLoading : isLoadingComments ,
91- isFetching : isFetchingComments ,
92- isError : isErrorLoadingComments ,
93- refetch : refetchComments ,
89+ data : user ,
90+ isLoading : isLoadingUser ,
91+ isError : isErrorLoadingUser ,
9492 } = useQuery ( {
95- queryKey : [ "comments" , postId ] ,
93+ enabled : ! ! post ?. userId ,
94+ queryKey : [ `/users/${ post ?. userId } ` ] ,
9695 queryFn : async ( ) => {
9796 const response = await fetch (
98- `http://localhost:3333/posts /${ postId } /comments` ,
97+ `http://localhost:3333/users /${ post ?. userId } `
9998 ) ;
100- return response . json ( ) as Promise < IComment [ ] > ;
99+ return response . json ( ) as Promise < IUser > ;
101100 } ,
102- initialData : initialComments , //SSR, with refresh
103- refetchInterval : 10000 , // 10 seconds
104101 } ) ;
105102
106- //load user - depends on user id from post so client-side only
103+ //load comments - with initial data from server
107104 const {
108- data : user ,
109- isLoading : isLoadingUser ,
110- isError : isErrorLoadingUser ,
105+ data : comments ,
106+ isLoading : isLoadingComments ,
107+ isFetching : isFetchingComments ,
108+ isError : isErrorLoadingComments ,
109+ refetch : refetchComments ,
111110 } = useQuery ( {
112- enabled : ! ! post ?. userId ,
113- queryKey : [ "user" , post ?. userId ] ,
111+ initialData : initialComments ,
112+ queryKey : [ `/posts/ ${ postId } /comments` ] ,
114113 queryFn : async ( ) => {
115114 const response = await fetch (
116- `http://localhost:3333/users /${ post ?. userId } ` ,
115+ `http://localhost:3333/posts /${ postId } /comments`
117116 ) ;
118- return response . json ( ) as Promise < IUser > ;
117+ return response . json ( ) as Promise < IComment [ ] > ;
119118 } ,
119+ refetchInterval : 10000 , // 10 seconds
120120 } ) ;
121121
122122 //delete comment, refresh comments after delete
@@ -130,7 +130,7 @@ export default function PostPage({
130130 `http://localhost:3333/comments/${ commentId } ` ,
131131 {
132132 method : "DELETE" ,
133- } ,
133+ }
134134 ) ;
135135 return response . json ( ) as Promise < IComment > ;
136136 } ,
@@ -139,7 +139,7 @@ export default function PostPage({
139139 onError : ( err , commentId ) => {
140140 console . error (
141141 `Error deleting comment ${ commentId } . Rolling UI back` ,
142- err ,
142+ err
143143 ) ;
144144 alert ( "Error deleting comment" ) ;
145145 } ,
@@ -152,7 +152,7 @@ export default function PostPage({
152152 ( commentId : number ) => {
153153 deleteComment ( commentId ) ;
154154 } ,
155- [ deleteComment ] ,
155+ [ deleteComment ]
156156 ) ;
157157
158158 // Post new comment - with optimistic updates!
@@ -184,7 +184,7 @@ export default function PostPage({
184184 // Optimistically update to the new value
185185 queryClient . setQueryData (
186186 [ "comments" , newComment . postId . toString ( ) ] ,
187- ( oldComments : any ) => [ ...oldComments , newComment ] ,
187+ ( oldComments : any ) => [ ...oldComments , newComment ]
188188 ) ;
189189
190190 // Return a context object with the snapshot value
@@ -195,7 +195,7 @@ export default function PostPage({
195195 onError : ( err , newComment , context ) => {
196196 queryClient . setQueryData (
197197 [ "comments" , newComment . postId . toString ( ) ] ,
198- context ?. previousComments ,
198+ context ?. previousComments
199199 ) ;
200200 console . error ( "Error posting comment. Rolling UI back" , err ) ;
201201 } ,
0 commit comments