diff --git a/app/hooks/useFetchAtlas.ts b/app/hooks/useFetchAtlas.ts index 452f2b17..2210059e 100644 --- a/app/hooks/useFetchAtlas.ts +++ b/app/hooks/useFetchAtlas.ts @@ -1,46 +1,17 @@ -import { useAsync } from "@databiosphere/findable-ui/lib/hooks/useAsync"; -import { useAuthentication } from "@databiosphere/findable-ui/lib/hooks/useAuthentication/useAuthentication"; -import { useCallback, useEffect } from "react"; import { API } from "../apis/catalog/hca-atlas-tracker/common/api"; import { HCAAtlasTrackerAtlas } from "../apis/catalog/hca-atlas-tracker/common/entities"; import { METHOD, PathParameter } from "../common/entities"; -import { - getFetchOptions, - getRequestURL, - isFetchStatusOk, -} from "../common/utils"; +import { getRequestURL } from "../common/utils"; +import { useFetchData } from "./useFetchData"; interface UseFetchAtlas { atlas?: HCAAtlasTrackerAtlas; } export const useFetchAtlas = (pathParameter: PathParameter): UseFetchAtlas => { - const { token } = useAuthentication(); - const { data: atlas, run } = useAsync(); - - const fetchAtlas = useCallback( - async (accessToken: string): Promise => { - const res = await fetch( - getRequestURL(API.ATLAS, pathParameter), - getFetchOptions(METHOD.GET, accessToken) - ); - if (isFetchStatusOk(res.status)) { - return await res.json(); - } - throw new Error( - await res - .json() - .then(({ message }) => message) - .catch(() => `Received ${res.status} response`) - ); - }, - [pathParameter] + const { data: atlas } = useFetchData( + getRequestURL(API.ATLAS, pathParameter), + METHOD.GET ); - - useEffect(() => { - if (!token) return; - run(fetchAtlas(token)); - }, [fetchAtlas, run, token]); - return { atlas }; }; diff --git a/app/hooks/useFetchComponentAtlas.ts b/app/hooks/useFetchComponentAtlas.ts deleted file mode 100644 index c53b36aa..00000000 --- a/app/hooks/useFetchComponentAtlas.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { useAsync } from "@databiosphere/findable-ui/lib/hooks/useAsync"; -import { useAuthentication } from "@databiosphere/findable-ui/lib/hooks/useAuthentication/useAuthentication"; -import { useCallback, useEffect } from "react"; -import { API } from "../apis/catalog/hca-atlas-tracker/common/api"; -import { HCAAtlasTrackerComponentAtlas } from "../apis/catalog/hca-atlas-tracker/common/entities"; -import { METHOD, PathParameter } from "../common/entities"; -import { - getFetchOptions, - getRequestURL, - isFetchStatusOk, -} from "../common/utils"; - -interface UseFetchComponentAtlas { - componentAtlas?: HCAAtlasTrackerComponentAtlas; -} - -export const useFetchComponentAtlas = ( - pathParameter: PathParameter -): UseFetchComponentAtlas => { - const { token } = useAuthentication(); - const { data: componentAtlas, run } = useAsync< - HCAAtlasTrackerComponentAtlas | undefined - >(); - - const fetchComponentAtlas = useCallback( - async ( - accessToken: string - ): Promise => { - const res = await fetch( - getRequestURL(API.ATLAS_COMPONENT_ATLAS, pathParameter), - getFetchOptions(METHOD.GET, accessToken) - ); - if (isFetchStatusOk(res.status)) { - return await res.json(); - } - throw new Error( - await res - .json() - .then(({ message }) => message) - .catch(() => `Received ${res.status} response`) - ); - }, - [pathParameter] - ); - - useEffect(() => { - if (!token) return; - run(fetchComponentAtlas(token)); - }, [fetchComponentAtlas, run, token]); - - return { - componentAtlas, - }; -}; diff --git a/app/hooks/useFetchComponentAtlases.ts b/app/hooks/useFetchComponentAtlases.ts deleted file mode 100644 index cdcf4cd5..00000000 --- a/app/hooks/useFetchComponentAtlases.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { useAsync } from "@databiosphere/findable-ui/lib/hooks/useAsync"; -import { useAuthentication } from "@databiosphere/findable-ui/lib/hooks/useAuthentication/useAuthentication"; -import { useCallback, useEffect } from "react"; -import { API } from "../apis/catalog/hca-atlas-tracker/common/api"; -import { HCAAtlasTrackerComponentAtlas } from "../apis/catalog/hca-atlas-tracker/common/entities"; -import { METHOD, PathParameter } from "../common/entities"; -import { - getFetchOptions, - getRequestURL, - isFetchStatusOk, -} from "../common/utils"; - -interface UseFetchComponentAtlases { - componentAtlases?: HCAAtlasTrackerComponentAtlas[]; -} - -export const useFetchComponentAtlases = ( - pathParameter: PathParameter -): UseFetchComponentAtlases => { - const { token } = useAuthentication(); - const { data: componentAtlases, run } = useAsync< - HCAAtlasTrackerComponentAtlas[] | undefined - >(); - - const fetchComponentAtlases = useCallback( - async ( - accessToken: string - ): Promise => { - const res = await fetch( - getRequestURL(API.ATLAS_COMPONENT_ATLASES, pathParameter), - getFetchOptions(METHOD.GET, accessToken) - ); - if (isFetchStatusOk(res.status)) { - return await res.json(); - } - throw new Error( - await res - .json() - .then(({ message }) => message) - .catch(() => `Received ${res.status} response`) - ); - }, - [pathParameter] - ); - - useEffect(() => { - if (!token) return; - run(fetchComponentAtlases(token)); - }, [fetchComponentAtlases, run, token]); - - return { componentAtlases }; -}; diff --git a/app/hooks/useFetchSourceStudies.ts b/app/hooks/useFetchSourceStudies.ts deleted file mode 100644 index 7347e0b9..00000000 --- a/app/hooks/useFetchSourceStudies.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { useAsync } from "@databiosphere/findable-ui/lib/hooks/useAsync"; -import { useAuthentication } from "@databiosphere/findable-ui/lib/hooks/useAuthentication/useAuthentication"; -import { useCallback, useEffect } from "react"; -import { API } from "../apis/catalog/hca-atlas-tracker/common/api"; -import { HCAAtlasTrackerSourceStudy } from "../apis/catalog/hca-atlas-tracker/common/entities"; -import { METHOD, PathParameter } from "../common/entities"; -import { - getFetchOptions, - getRequestURL, - isFetchStatusOk, -} from "../common/utils"; - -interface UseFetchSourceStudies { - sourceStudies?: HCAAtlasTrackerSourceStudy[]; -} - -export const useFetchSourceStudies = ( - pathParameter: PathParameter -): UseFetchSourceStudies => { - const { token } = useAuthentication(); - const { data: sourceStudies, run } = useAsync< - HCAAtlasTrackerSourceStudy[] | undefined - >(); - - const fetchSourceStudies = useCallback( - async ( - accessToken: string - ): Promise => { - const res = await fetch( - getRequestURL(API.ATLAS_SOURCE_STUDIES, pathParameter), - getFetchOptions(METHOD.GET, accessToken) - ); - if (isFetchStatusOk(res.status)) { - return await res.json(); - } - throw new Error( - await res - .json() - .then(({ message }) => message) - .catch(() => `Received ${res.status} response`) - ); - }, - [pathParameter] - ); - - useEffect(() => { - if (!token) return; - run(fetchSourceStudies(token)); - }, [fetchSourceStudies, run, token]); - - return { sourceStudies }; -}; diff --git a/app/hooks/useFetchUser.ts b/app/hooks/useFetchUser.ts index d8d36c2d..b0682f26 100644 --- a/app/hooks/useFetchUser.ts +++ b/app/hooks/useFetchUser.ts @@ -1,40 +1,13 @@ -import { useAsync } from "@databiosphere/findable-ui/lib/hooks/useAsync"; -import { useAuthentication } from "@databiosphere/findable-ui/lib/hooks/useAuthentication/useAuthentication"; -import { useCallback, useEffect } from "react"; import { API } from "../apis/catalog/hca-atlas-tracker/common/api"; import { HCAAtlasTrackerActiveUser } from "../apis/catalog/hca-atlas-tracker/common/entities"; import { METHOD } from "../common/entities"; -import { getFetchOptions, isFetchStatusOk } from "../common/utils"; +import { useFetchData } from "./useFetchData"; export const useFetchUser = (): HCAAtlasTrackerActiveUser | undefined => { - const { token } = useAuthentication(); - const { data: user, run } = useAsync(); - - const fetchUser = useCallback( - async ( - accessToken: string - ): Promise => { - const res = await fetch( - API.USER, - getFetchOptions(METHOD.GET, accessToken) - ); - if (isFetchStatusOk(res.status)) { - return await res.json(); - } - throw new Error( - await res - .json() - .then(({ message }) => message) - .catch(() => `Received ${res.status} response`) - ); - }, - [] + const { data: user } = useFetchData( + API.USER, + METHOD.GET ); - useEffect(() => { - if (!token) return; - run(fetchUser(token)); - }, [fetchUser, run, token]); - return user; }; diff --git a/app/views/ComponentAtlasView/hooks/useEditComponentAtlasForm.ts b/app/views/ComponentAtlasView/hooks/useEditComponentAtlasForm.ts index c6f5342c..b3faa21a 100644 --- a/app/views/ComponentAtlasView/hooks/useEditComponentAtlasForm.ts +++ b/app/views/ComponentAtlasView/hooks/useEditComponentAtlasForm.ts @@ -1,11 +1,11 @@ import { HCAAtlasTrackerComponentAtlas } from "../../../apis/catalog/hca-atlas-tracker/common/entities"; import { PathParameter } from "../../../common/entities"; -import { useFetchComponentAtlas } from "../../../hooks/useFetchComponentAtlas"; import { FormMethod } from "../../../hooks/useForm/common/entities"; import { useForm } from "../../../hooks/useForm/useForm"; import { FIELD_NAME } from "../common/constants"; import { ComponentAtlasEditData } from "../common/entities"; import { componentAtlasEditSchema } from "../common/schema"; +import { useFetchComponentAtlas } from "./useFetchComponentAtlas"; const SCHEMA = componentAtlasEditSchema; diff --git a/app/views/ComponentAtlasView/hooks/useFetchComponentAtlas.ts b/app/views/ComponentAtlasView/hooks/useFetchComponentAtlas.ts new file mode 100644 index 00000000..ebd8ed9a --- /dev/null +++ b/app/views/ComponentAtlasView/hooks/useFetchComponentAtlas.ts @@ -0,0 +1,21 @@ +import { API } from "../../../apis/catalog/hca-atlas-tracker/common/api"; +import { HCAAtlasTrackerComponentAtlas } from "../../../apis/catalog/hca-atlas-tracker/common/entities"; +import { METHOD, PathParameter } from "../../../common/entities"; +import { getRequestURL } from "../../../common/utils"; +import { useFetchData } from "../../../hooks/useFetchData"; + +interface UseFetchComponentAtlas { + componentAtlas?: HCAAtlasTrackerComponentAtlas; +} + +export const useFetchComponentAtlas = ( + pathParameter: PathParameter +): UseFetchComponentAtlas => { + const { data: componentAtlas } = useFetchData< + HCAAtlasTrackerComponentAtlas | undefined + >(getRequestURL(API.ATLAS_COMPONENT_ATLAS, pathParameter), METHOD.GET); + + return { + componentAtlas, + }; +}; diff --git a/app/views/ComponentAtlasesView/componentAtlasesView.tsx b/app/views/ComponentAtlasesView/componentAtlasesView.tsx index b3d8228f..d71c496e 100644 --- a/app/views/ComponentAtlasesView/componentAtlasesView.tsx +++ b/app/views/ComponentAtlasesView/componentAtlasesView.tsx @@ -8,9 +8,9 @@ import { ViewComponentAtlases } from "../../components/Detail/components/ViewCom import { AtlasStatus } from "../../components/Layout/components/Detail/components/DetailViewHero/components/AtlasStatus/atlasStatus"; import { DetailView } from "../../components/Layout/components/Detail/detailView"; import { useFetchAtlas } from "../../hooks/useFetchAtlas"; -import { useFetchComponentAtlases } from "../../hooks/useFetchComponentAtlases"; import { useFormManager } from "../../hooks/useFormManager/useFormManager"; import { getBreadcrumbs } from "../AtlasView/common/utils"; +import { useFetchComponentAtlases } from "./hooks/useFetchComponentAtlases"; interface ComponentAtlasesViewProps { pathParameter: PathParameter; diff --git a/app/views/ComponentAtlasesView/hooks/useFetchComponentAtlases.ts b/app/views/ComponentAtlasesView/hooks/useFetchComponentAtlases.ts new file mode 100644 index 00000000..04fd4c1e --- /dev/null +++ b/app/views/ComponentAtlasesView/hooks/useFetchComponentAtlases.ts @@ -0,0 +1,19 @@ +import { API } from "../../../apis/catalog/hca-atlas-tracker/common/api"; +import { HCAAtlasTrackerComponentAtlas } from "../../../apis/catalog/hca-atlas-tracker/common/entities"; +import { METHOD, PathParameter } from "../../../common/entities"; +import { getRequestURL } from "../../../common/utils"; +import { useFetchData } from "../../../hooks/useFetchData"; + +interface UseFetchComponentAtlases { + componentAtlases?: HCAAtlasTrackerComponentAtlas[]; +} + +export const useFetchComponentAtlases = ( + pathParameter: PathParameter +): UseFetchComponentAtlases => { + const { data: componentAtlases } = useFetchData< + HCAAtlasTrackerComponentAtlas[] | undefined + >(getRequestURL(API.ATLAS_COMPONENT_ATLASES, pathParameter), METHOD.GET); + + return { componentAtlases }; +}; diff --git a/app/views/SourceStudiesView/hooks/useFetchSourceStudies.ts b/app/views/SourceStudiesView/hooks/useFetchSourceStudies.ts new file mode 100644 index 00000000..42db1cb5 --- /dev/null +++ b/app/views/SourceStudiesView/hooks/useFetchSourceStudies.ts @@ -0,0 +1,19 @@ +import { API } from "../../../apis/catalog/hca-atlas-tracker/common/api"; +import { HCAAtlasTrackerSourceStudy } from "../../../apis/catalog/hca-atlas-tracker/common/entities"; +import { METHOD, PathParameter } from "../../../common/entities"; +import { getRequestURL } from "../../../common/utils"; +import { useFetchData } from "../../../hooks/useFetchData"; + +interface UseFetchSourceStudies { + sourceStudies?: HCAAtlasTrackerSourceStudy[]; +} + +export const useFetchSourceStudies = ( + pathParameter: PathParameter +): UseFetchSourceStudies => { + const { data: sourceStudies } = useFetchData< + HCAAtlasTrackerSourceStudy[] | undefined + >(getRequestURL(API.ATLAS_SOURCE_STUDIES, pathParameter), METHOD.GET); + + return { sourceStudies }; +}; diff --git a/app/views/SourceStudiesView/sourceStudiesView.tsx b/app/views/SourceStudiesView/sourceStudiesView.tsx index 55ed21d3..3fbc296e 100644 --- a/app/views/SourceStudiesView/sourceStudiesView.tsx +++ b/app/views/SourceStudiesView/sourceStudiesView.tsx @@ -8,9 +8,9 @@ import { ViewSourceStudies } from "../../components/Detail/components/ViewSource import { AtlasStatus } from "../../components/Layout/components/Detail/components/DetailViewHero/components/AtlasStatus/atlasStatus"; import { DetailView } from "../../components/Layout/components/Detail/detailView"; import { useFetchAtlas } from "../../hooks/useFetchAtlas"; -import { useFetchSourceStudies } from "../../hooks/useFetchSourceStudies"; import { useFormManager } from "../../hooks/useFormManager/useFormManager"; import { getBreadcrumbs } from "../AtlasView/common/utils"; +import { useFetchSourceStudies } from "./hooks/useFetchSourceStudies"; interface SourceStudiesViewProps { pathParameter: PathParameter;