11/* eslint-disable no-nested-ternary */
22import type { Component } from 'solid-js' ;
3- import { Switch , Match } from 'solid-js' ;
3+ import { createEffect , ErrorBoundary , Suspense } from 'solid-js' ;
44
55import { useParams } from '@solidjs/router' ;
6- import { createQuery } from '@tanstack/solid-query' ;
76import toast from 'solid-toast' ;
87
98import Head from '@/components/modules/Head/Head' ;
10- import type { Post } from '@/types/api/post.interface' ;
11-
12- const apiEndpointUrl = import . meta. env . VITE_PUBLIC_API_URL ;
13-
14- const fetchData = async ( id : string ) : Promise < Post > =>
15- ( await fetch ( `${ apiEndpointUrl } /posts/${ id } ` ) ) . json ( ) ;
9+ import useFetchPost from '@/hooks/useFetchPost' ;
1610
1711const PostContent : Component = ( ) => {
1812 const params = useParams ( ) ;
1913 const { id } = params ;
2014
2115 // w/ createQuery() on '@tanstack/solid-query'
22- const query = createQuery ( ( ) => ( {
23- // Array Keys with variables
24- // @ https://tanstack.com/query/v4/docs/react/guides/query-keys?from=reactQueryV3&original=https%3A%2F%2Freact-query-v3.tanstack.com%2Fguides%2Fquery-keys#array-keys-with-variables
25- queryKey : [ `/posts/` , id ] ,
26- // Pass parameter w/ inline anonymous functions. @ https://stackoverflow.com/a/68111112/4542456
27- queryFn : ( ) => fetchData ( id ) ,
28- } ) ) ;
16+ const query = useFetchPost ( id ) ;
2917
30- if ( query . isError ) {
31- toast . error ( 'Failed to fetch data.' ) ;
32- }
18+ createEffect ( ( ) => {
19+ if ( query . isError ) {
20+ toast . error ( 'Failed to fetch data.' ) ;
21+ }
22+ } ) ;
3323
3424 // w/ createResource()
3525 // const [post] = createResource<Post, string>(id, fetchData);
@@ -46,20 +36,14 @@ const PostContent: Component = () => {
4636
4737 < section >
4838 < div class = "inner" >
49- < Switch >
50- < Match when = { query . isLoading } >
51- < div > Loading...</ div >
52- </ Match >
53- < Match when = { query . isError } >
54- < div > Error: { ( query . error as Error ) . message } </ div >
55- </ Match >
56- < Match when = { query . isSuccess } >
57- < >
58- < h1 class = "mb-5 text-4xl leading-none" > { query . data ?. title } </ h1 >
59- < p class = "whitespace-pre-wrap" > { query . data ?. body } </ p >
60- </ >
61- </ Match >
62- </ Switch >
39+ < ErrorBoundary
40+ fallback = { < div > Error: { ( query . error as Error ) . message } </ div > }
41+ >
42+ < Suspense fallback = { < div > Loading...</ div > } >
43+ < h1 class = "mb-5 text-4xl leading-none" > { query . data ?. title } </ h1 >
44+ < p class = "whitespace-pre-wrap" > { query . data ?. body } </ p >
45+ </ Suspense >
46+ </ ErrorBoundary >
6347 </ div >
6448 </ section >
6549
0 commit comments