Skip to content

Commit

Permalink
Merge pull request #219 from bento-platform/refact/rewrite-metadata-r…
Browse files Browse the repository at this point in the history
…equeststatus

refact: rewrite metadata feature with RequestStatus
  • Loading branch information
davidlougheed authored Nov 13, 2024
2 parents c456a41 + ec53788 commit b904044
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/js/components/BentoAppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getProjects, markScopeSet, selectScope } from '@/features/metadata/meta
import Loader from '@/components/Loader';
import DefaultLayout from '@/components/Util/DefaultLayout';
import { BEACON_NETWORK_ENABLED } from '@/config';
import { RequestStatus } from '@/types/requests';
import { BentoRoute } from '@/types/routes';
import { scopeEqual, validProjectDataset } from '@/utils/router';

Expand Down Expand Up @@ -77,7 +78,7 @@ const BentoAppRouter = () => {

const { isAutoAuthenticating } = useAutoAuthenticate();
const isAuthenticated = useIsAuthenticated();
const { selectedScope, isFetching: isFetchingProjects } = useMetadata();
const { selectedScope, projectsStatus } = useMetadata();

useEffect(() => {
if (!selectedScope.scopeSet) return;
Expand Down Expand Up @@ -108,7 +109,7 @@ const BentoAppRouter = () => {
}
}, [dispatch, isAuthenticated]);

if (isAutoAuthenticating || isFetchingProjects) {
if (isAutoAuthenticating || projectsStatus === RequestStatus.Pending) {
return <Loader />;
}

Expand Down
5 changes: 3 additions & 2 deletions src/js/components/Provenance/ProvenanceTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Row } from 'antd';

import { useMetadata } from '@/features/metadata/hooks';
import type { Dataset } from '@/types/metadata';
import { RequestStatus } from '@/types/requests';

import DatasetProvenance from './DatasetProvenance';

const ProvenanceTab = () => {
const {
projects,
isFetching: loading,
projectsStatus,
selectedScope: { scope },
} = useMetadata();

Expand All @@ -24,7 +25,7 @@ const ProvenanceTab = () => {
return (
<Row justify="center">
{datasets.map((dataset, i) => (
<DatasetProvenance key={i} dataset={dataset} loading={loading} />
<DatasetProvenance key={i} dataset={dataset} loading={projectsStatus === RequestStatus.Pending} />
))}
</Row>
);
Expand Down
17 changes: 11 additions & 6 deletions src/js/features/metadata/metadata.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PayloadAction } from '@reduxjs/toolkit';
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import axios from 'axios';
import type { PaginatedResponse, Project } from '@/types/metadata';
import { RequestStatus } from '@/types/requests';
import type { RootState } from '@/store';
import { printAPIError } from '@/utils/error.util';
import { validProjectDataset } from '@/utils/router';
Expand All @@ -18,13 +19,13 @@ export type DiscoveryScopeSelection = {

export interface MetadataState {
projects: Project[];
isFetching: boolean;
projectsStatus: RequestStatus;
selectedScope: DiscoveryScopeSelection;
}

const initialState: MetadataState = {
projects: [],
isFetching: false,
projectsStatus: RequestStatus.Idle,
selectedScope: {
scope: { project: undefined, dataset: undefined },
// Whether scope has been set from URL/action yet. If it hasn't, we need to wait before fetching scoped data.
Expand All @@ -48,7 +49,11 @@ export const getProjects = createAsyncThunk<
},
{
condition(_, { getState }) {
return !getState().metadata.isFetching;
// Only need to fetch projects once - if the projects are being/have already been fetched, don't re-execute.
const { projectsStatus } = getState().metadata;
const cond = projectsStatus === RequestStatus.Idle;
if (!cond) console.debug(`getProjects() was attempted, but a prior attempt gave status: ${projectsStatus}`);
return cond;
},
}
);
Expand All @@ -69,14 +74,14 @@ const metadata = createSlice({
},
extraReducers(builder) {
builder.addCase(getProjects.pending, (state) => {
state.isFetching = true;
state.projectsStatus = RequestStatus.Pending;
});
builder.addCase(getProjects.fulfilled, (state, { payload }) => {
state.projects = payload?.results ?? [];
state.isFetching = false;
state.projectsStatus = RequestStatus.Fulfilled;
});
builder.addCase(getProjects.rejected, (state) => {
state.isFetching = false;
state.projectsStatus = RequestStatus.Rejected;
});
},
});
Expand Down

0 comments on commit b904044

Please sign in to comment.