Skip to content

Commit 8f29daa

Browse files
committed
chore(comments): address pr comments
1 parent 5d3b33a commit 8f29daa

File tree

7 files changed

+35
-33
lines changed

7 files changed

+35
-33
lines changed

src/api/posts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './types';
22
export * from './use-add-post';
33
export * from './use-post';
4+
export * from './use-post-comments';
45
export * from './use-posts';

src/api/posts/use-add-post.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import type { AxiosError } from 'axios';
2-
import axios from 'axios';
32
import { createMutation } from 'react-query-kit';
43

5-
import { Env } from '@/core/env';
6-
4+
import { client } from '../common';
75
import type { Post } from './types';
86

97
type Variables = { title: string; body: string; userId: number };
108
type Response = Post;
119

1210
export const useAddPost = createMutation<Response, Variables, AxiosError>({
1311
mutationFn: async (variables) =>
14-
axios({
15-
url: `${Env.API_URL}posts/add`,
12+
client({
13+
url: 'posts/add',
1614
method: 'POST',
1715
data: variables,
1816
headers: {

src/api/posts/use-post-comments.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createQuery } from 'react-query-kit';
2+
3+
import { queryFactory } from '@/api/query-factory';
4+
5+
import { client } from '../common';
6+
import { type Comment } from './types';
7+
8+
type Variables = {
9+
id: number;
10+
};
11+
12+
type Response = {
13+
comments: Comment[];
14+
};
15+
16+
const getPostComments = async (id: number) => {
17+
const { data } = await client.get(`posts/${id}/comments`);
18+
return data;
19+
};
20+
21+
export const usePostComments = createQuery<Response, Variables>({
22+
...queryFactory.posts.detail(1)._ctx.comments,
23+
fetcher: (variables) => getPostComments(variables.id),
24+
});

src/api/posts/use-post.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useQuery } from '@tanstack/react-query';
21
import type { AxiosError } from 'axios';
32
import { createQuery } from 'react-query-kit';
43

@@ -16,12 +15,6 @@ const getPosts = async (variables: Variables) => {
1615
};
1716

1817
export const usePost = createQuery<Response, Variables, AxiosError>({
19-
...queryFactory.posts.detail(1), // this translates to ['posts', 1]
18+
...queryFactory.posts.detail(1),
2019
fetcher: getPosts,
2120
});
22-
23-
export const usePostComments = (postId: number) =>
24-
useQuery({
25-
...queryFactory.posts.detail(postId)._ctx.comments,
26-
enabled: !!postId,
27-
});

src/api/query-factory.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
import {
22
createQueryKeys,
3-
type inferQueryKeyStore,
43
mergeQueryKeys,
54
} from '@lukemorales/query-key-factory';
65

7-
// separate query keys for each service
86
import { cartKeys } from './carts/query-keys';
9-
import { client } from './common';
10-
11-
const getPostComments = async (id: string) => {
12-
const { data } = await client.get(`posts/${id}/comments`);
13-
return data;
14-
};
157

168
export const postKeys = createQueryKeys('posts', {
17-
list: (filters) => ['posts', filters],
18-
// list: null, // if we don't want to pass any filters
19-
// detail: (id) => [id], // option one
9+
list: (filters) => [filters],
2010
detail: (id) => ({
21-
queryKey: [id], // [post, detail, id]
11+
queryKey: [id],
2212
contextQueries: {
23-
// comments become part of the contextQueries query key [post, detail, id, comments]
2413
comments: {
2514
queryKey: null,
26-
queryFn: () => getPostComments(id),
2715
},
2816
},
29-
}), // option two
30-
comments: (id) => [id], // [posts, id, comments]
17+
}),
3118
});
3219

33-
export type QueryKeys = inferQueryKeyStore<typeof queryFactory>;
3420
export const queryFactory = mergeQueryKeys(postKeys, cartKeys);

src/app/feed/[id].tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default function Post() {
1414
const {
1515
data: { comments } = { comments: [] },
1616
isLoading: isLoadingComments,
17-
} = usePostComments(data?.id!);
17+
} = usePostComments({
18+
variables: { id: data?.id! },
19+
});
1820

1921
if (isPending) {
2022
return (

src/app/feed/add-post.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ export default function AddPost() {
3535
message: 'Post added successfully',
3636
type: 'success',
3737
});
38-
// react-query-kit has the getKey method this will require to import the query function everywhere needed
39-
// queryClient.invalidateQueries(usePosts.getKey());
4038
queryClient.invalidateQueries({
4139
queryKey: queryFactory.posts.list({}).queryKey,
4240
});

0 commit comments

Comments
 (0)