From d9902702db33e071133853afe12c68ba7f9bdb5a Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 18 Oct 2020 14:52:38 +0200 Subject: [PATCH] Fix useAsync --- src/api/edition.ts | 10 +++------- src/hooks/useAsync.ts | 4 +++- src/pages/edition/pick.tsx | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/api/edition.ts b/src/api/edition.ts index ec7975f..5f7951e 100644 --- a/src/api/edition.ts +++ b/src/api/edition.ts @@ -30,14 +30,10 @@ export async function join(key: string): Promise { } export async function get(key: string): Promise { - try { - const response = await axios.get(prepare(EDITION_INFO_ENDPOINT, { key })); - const dto = response.data; + const response = await axios.get(prepare(EDITION_INFO_ENDPOINT, { key })); + const dto = response.data; - return editionDtoTransformer.transform(dto); - } catch (error) { - return null; - } + return editionDtoTransformer.transform(dto); } export async function current(): Promise { diff --git a/src/hooks/useAsync.ts b/src/hooks/useAsync.ts index a8293d4..8b83d9b 100644 --- a/src/hooks/useAsync.ts +++ b/src/hooks/useAsync.ts @@ -40,8 +40,10 @@ export function useAsync(supplier: Promise | (() => Promise< useEffect(() => { if (typeof supplier === "function") { setPromise(supplier()); + } else { + setPromise(supplier); } - }, []) + }, [ supplier ]) return { isLoading, diff --git a/src/pages/edition/pick.tsx b/src/pages/edition/pick.tsx index a1261c0..ea04030 100644 --- a/src/pages/edition/pick.tsx +++ b/src/pages/edition/pick.tsx @@ -1,5 +1,5 @@ import { Page } from "@/pages/base"; -import React from "react"; +import React, { useCallback } from "react"; import { useTranslation } from "react-i18next"; import { Box, Button, CircularProgress, Container, Typography } from "@material-ui/core"; import { Actions } from "@/components"; @@ -15,7 +15,7 @@ import { EditionActions, useDispatch, UserActions } from "@/state/actions"; export const PickEditionPage = () => { const { t } = useTranslation(); - const { value: editions, isLoading } = useAsync(() => api.edition.available()); + const { value: editions, isLoading } = useAsync(useCallback(() => api.edition.available(), [])); const dispatch = useDispatch(); const history = useHistory();