Skip to content

Commit 6718a49

Browse files
committed
refactor(app): code refactor
1 parent 2631c41 commit 6718a49

2 files changed

Lines changed: 54 additions & 32 deletions

File tree

apps/solidjs-boilerplate/src/components/modules/PostContent/PostContent.tsx

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
/* eslint-disable no-nested-ternary */
22
import type { Component } from 'solid-js';
3-
import { Switch, Match } from 'solid-js';
3+
import { createEffect, ErrorBoundary, Suspense } from 'solid-js';
44

55
import { useParams } from '@solidjs/router';
6-
import { createQuery } from '@tanstack/solid-query';
76
import toast from 'solid-toast';
87

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

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { CreateQueryResult } from '@tanstack/solid-query';
2+
import { createQuery } from '@tanstack/solid-query';
3+
4+
import type { Post } from '@/types/api/post.interface';
5+
6+
// import type { Post } from '@/types/api/post.interface';
7+
8+
const apiEndpointUrl = import.meta.env.VITE_PUBLIC_API_URL;
9+
10+
// const fetchData = async (id: string): Promise<Post> =>
11+
// (await fetch(`${apiEndpointUrl}/posts/${id}`)).json();
12+
13+
const useFetchPost = (id: string) => {
14+
// const query = createQuery(() => ({
15+
// queryKey: ['/posts'],
16+
// queryFn: fetchData,
17+
// }));
18+
19+
const query: CreateQueryResult<Post, Error> = createQuery(() => ({
20+
// Array Keys with variables
21+
// @ 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
22+
queryKey: [`/posts/`, id],
23+
// Pass parameter w/ inline anonymous functions. @ https://stackoverflow.com/a/68111112/4542456
24+
// queryFn: () => fetchData(id),
25+
queryFn: async () => {
26+
const result = await fetch(`${apiEndpointUrl}/posts/${id}`);
27+
if (!result.ok) throw new Error('Failed to fetch data');
28+
return result.json();
29+
},
30+
31+
staleTime: 1000 * 60 * 5, // 5 minutes
32+
throwOnError: true, // Throw an error if the query fails
33+
}));
34+
35+
return query;
36+
};
37+
38+
export default useFetchPost;

0 commit comments

Comments
 (0)