Skip to content

Commit

Permalink
refactor: use generic usefetchdata hook (#294) (#295)
Browse files Browse the repository at this point in the history
* feat: manage source datasets ui (#277)

* refactor: use generic usefetchdata hook (#294)

* fix: remove unused source datasets tab (#294)

---------

Co-authored-by: Fran McDade <[email protected]>
  • Loading branch information
frano-m and Fran McDade authored Jun 12, 2024
1 parent db0b435 commit a2ddadd
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 226 deletions.
39 changes: 5 additions & 34 deletions app/hooks/useFetchAtlas.ts
Original file line number Diff line number Diff line change
@@ -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<HCAAtlasTrackerAtlas | undefined>();

const fetchAtlas = useCallback(
async (accessToken: string): Promise<HCAAtlasTrackerAtlas | undefined> => {
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<HCAAtlasTrackerAtlas | undefined>(
getRequestURL(API.ATLAS, pathParameter),
METHOD.GET
);

useEffect(() => {
if (!token) return;
run(fetchAtlas(token));
}, [fetchAtlas, run, token]);

return { atlas };
};
54 changes: 0 additions & 54 deletions app/hooks/useFetchComponentAtlas.ts

This file was deleted.

52 changes: 0 additions & 52 deletions app/hooks/useFetchComponentAtlases.ts

This file was deleted.

52 changes: 0 additions & 52 deletions app/hooks/useFetchSourceStudies.ts

This file was deleted.

35 changes: 4 additions & 31 deletions app/hooks/useFetchUser.ts
Original file line number Diff line number Diff line change
@@ -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<HCAAtlasTrackerActiveUser | undefined>();

const fetchUser = useCallback(
async (
accessToken: string
): Promise<HCAAtlasTrackerActiveUser | undefined> => {
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<HCAAtlasTrackerActiveUser | undefined>(
API.USER,
METHOD.GET
);

useEffect(() => {
if (!token) return;
run(fetchUser(token));
}, [fetchUser, run, token]);

return user;
};
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
21 changes: 21 additions & 0 deletions app/views/ComponentAtlasView/hooks/useFetchComponentAtlas.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
2 changes: 1 addition & 1 deletion app/views/ComponentAtlasesView/componentAtlasesView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions app/views/ComponentAtlasesView/hooks/useFetchComponentAtlases.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
19 changes: 19 additions & 0 deletions app/views/SourceStudiesView/hooks/useFetchSourceStudies.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
2 changes: 1 addition & 1 deletion app/views/SourceStudiesView/sourceStudiesView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a2ddadd

Please sign in to comment.