-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: #111: make stats request in client components * feat: #111: implement serialize, deserialize and innerFetch helpers * refactor: use `apiFetch` and serializers for summary and summaries * fix: use apifetch * feat: #111: implement pagination of the trading pairs
- Loading branch information
Showing
10 changed files
with
161 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
'use client'; | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
import { SummaryDataResponse } from '@/shared/api/server/summary/types'; | ||
import { SummariesResponse } from '@/shared/api/server/summary/all'; | ||
import { InfiniteData, QueryKey, useInfiniteQuery } from '@tanstack/react-query'; | ||
import { SummaryData } from '@/shared/api/server/summary/types'; | ||
import { DurationWindow } from '@/shared/utils/duration'; | ||
import { apiFetch } from '@/shared/utils/api-fetch'; | ||
|
||
const BASE_LIMIT = 15; | ||
const BASE_OFFSET = 0; | ||
const BASE_PAGE = 0; | ||
const BASE_WINDOW: DurationWindow = '1d'; | ||
|
||
export const useSummaries = () => { | ||
return useQuery({ | ||
queryKey: ['summaries', BASE_LIMIT, BASE_OFFSET], | ||
queryFn: async () => { | ||
const paramsObj = { | ||
export const useSummaries = (search: string) => { | ||
return useInfiniteQuery<SummaryData[], Error, InfiniteData<SummaryData[]>, QueryKey, number>({ | ||
queryKey: ['summaries', search], | ||
staleTime: 1000 * 60 * 5, | ||
initialPageParam: BASE_PAGE, | ||
getNextPageParam: (lastPage, _, lastPageParam) => { | ||
return lastPage.length ? lastPageParam + 1 : undefined; | ||
}, | ||
queryFn: async ({ pageParam }) => { | ||
return apiFetch<SummaryData[]>('/api/summaries', { | ||
search, | ||
limit: BASE_LIMIT.toString(), | ||
offset: BASE_OFFSET.toString(), | ||
offset: (pageParam * BASE_LIMIT).toString(), | ||
durationWindow: BASE_WINDOW, | ||
}; | ||
|
||
const baseUrl = '/api/summaries'; | ||
const urlParams = new URLSearchParams(paramsObj).toString(); | ||
const fetchRes = await fetch(`${baseUrl}?${urlParams}`); | ||
const jsonRes = (await fetchRes.json()) as SummariesResponse; | ||
if ('error' in jsonRes) { | ||
throw new Error(jsonRes.error); | ||
} | ||
|
||
return jsonRes.map(res => SummaryDataResponse.fromJson(res)); | ||
}); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.