Skip to content

Commit

Permalink
add primary images query #1081
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdimanteto committed Nov 4, 2024
1 parent 5dc385e commit 5a666bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/api/images.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@ describe('images api functions', () => {

expect(result.current.data?.length).toEqual(20);
});

it('sends request to fetch primary image data and returns successful response', async () => {
const { result } = renderHook(() => useGetImages('1', true), {
wrapper: hooksWrapperWithProviders(),
});

await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(result.current.data?.length).toEqual(1);

Check failure on line 32 in src/api/images.test.tsx

View workflow job for this annotation

GitHub Actions / Lint & Unit Tests

src/api/images.test.tsx > images api functions > useGetImages > sends request to fetch primary image data and returns successful response

AssertionError: expected 20 to deeply equal 1 - Expected + Received - 1 + 20 ❯ src/api/images.test.tsx:32:43
});
});
});
14 changes: 10 additions & 4 deletions src/api/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ export const getImage = async (id: string): Promise<ImageGet> => {
});
};

const getImages = async (entityId: string): Promise<Image[]> => {
const getImages = async (
entityId: string,
primary?: boolean
): Promise<Image[]> => {
const queryParams = new URLSearchParams();
queryParams.append('entity_id', entityId);

if (primary !== undefined) queryParams.append('primary', String(primary));
return storageApi
.get(`/images`, {
params: queryParams,
Expand All @@ -20,11 +25,12 @@ const getImages = async (entityId: string): Promise<Image[]> => {
};

export const useGetImages = (
entityId?: string
entityId?: string,
primary?: boolean
): UseQueryResult<Image[], AxiosError> => {
return useQuery({
queryKey: ['Images', entityId],
queryFn: () => getImages(entityId ?? ''),
queryKey: ['Images', entityId, primary],
queryFn: () => getImages(entityId ?? '', primary),
enabled: !!entityId,
});
};

0 comments on commit 5a666bb

Please sign in to comment.