Skip to content

Commit

Permalink
Merge pull request #138 from IT-Cotato/feat/COT-10_implement_get_curr…
Browse files Browse the repository at this point in the history
…ent_generation

feat: add get current generation
  • Loading branch information
Ea-st-ring authored Oct 7, 2024
2 parents 22d4be1 + 82640ac commit 91eb222
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/hooks/useGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@ interface UseGenerationReturn {
export function useGeneration({ generationId }: UseGenerationParams = {}): UseGenerationReturn {
const _return = useRef<UseGenerationReturn>({} as UseGenerationReturn);

const { data, isLoading, error } = useSWR<CotatoGenerationInfoResponse[]>(
'/v1/api/generation',
fetcher,
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
},
);

const currentGeneration =
data && data.sort((a, b) => (b.generationId as number) - (a.generationId as number))[0];
const {
data: generationData,
isLoading: isGenerationLoading,
error: isGenerationError,
} = useSWR<CotatoGenerationInfoResponse[]>('/v1/api/generation', fetcher, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
});

const {
data: currentGenerationData,
isLoading: isCurrentGenerationLoading,
error: currentGenerationError,
} = useSWR<CotatoGenerationInfoResponse>('/v1/api/generation/current', fetcher, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
});

const currentGeneration = currentGenerationData;

const targetGeneration = generationId
? data?.find((generation) => generation.generationId === Number(generationId))
? generationData?.find((generation) => generation.generationId === Number(generationId))
: undefined;

//
Expand All @@ -49,9 +58,9 @@ export function useGeneration({ generationId }: UseGenerationParams = {}): UseGe

_return.current.currentGeneration = currentGeneration;
_return.current.targetGeneration = targetGeneration;
_return.current.generations = data;
_return.current.isGenerationLoading = isLoading;
_return.current.isGenerationError = error;
_return.current.generations = generationData;
_return.current.isGenerationLoading = isGenerationLoading || isCurrentGenerationLoading;
_return.current.isGenerationError = isGenerationError || currentGenerationError;

return _return.current;
}

0 comments on commit 91eb222

Please sign in to comment.