Skip to content

Commit

Permalink
feat: return data using the hooks in usePostOrPosts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 26, 2023
1 parent 7be0e1d commit 67757c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/react/hooks/useFetchPostOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
PostOrPostsParams,
PostOrPostsFetchStrategyResult,
} from '../../data/strategies/PostOrPostsFetchStrategy';
import { useFetchPost } from './useFetchPost';
import { useFetchPosts } from './useFetchPosts';

export interface usePostOrPostResponse<T extends PostEntity = PostEntity> extends HookResponse {
data?: { post?: T; posts?: T[] };
Expand All @@ -31,7 +33,7 @@ export function useFetchPostOrPosts<
T extends PostEntity = PostEntity,
P extends PostOrPostsParams = PostOrPostsParams,
>(
params: P | {} = {},
params: Partial<P> = {},
options: FetchHookOptions<FetchResponse<PostOrPostsFetchStrategyResult<T>>> = {},
path = '',
): usePostOrPostResponse<T> {
Expand All @@ -42,6 +44,10 @@ export function useFetchPostOrPosts<
path,
);

// TODO: should fetch
const { data: postData } = useFetchPost<T>(params.single, undefined, path);
const { data: postsData } = useFetchPosts<T>(params.archive, undefined, path);

if (error || !data) {
const fakeData = {
post: makeErrorCatchProxy<T>('post'),
Expand All @@ -58,9 +64,9 @@ export function useFetchPostOrPosts<
};
}

if (data.result.isSingle && !Array.isArray(data.result.data)) {
if (data.result.isSingle) {
return {
data: { post: data.result.data },
data: postData,
loading: false,
isMainQuery,
isSingle: data.result.isSingle,
Expand All @@ -69,7 +75,7 @@ export function useFetchPostOrPosts<
}

return {
data: { posts: data.result.data as T[] },
data: postsData,
loading: false,
isMainQuery,
isSingle: data.result.isSingle,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/data/hooks/usePostOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function usePostOrPosts<
T extends PostEntity = PostEntity,
P extends PostOrPostsParams = PostOrPostsParams,
>(
params: P | {} = {},
params: Partial<P> = {},
options: FetchHookOptions<FetchResponse<PostOrPostsFetchStrategyResult<T>>> = {},
) {
const useFetchArguments = usePrepareFetch(params, options);
Expand Down

0 comments on commit 67757c1

Please sign in to comment.