Skip to content

Commit

Permalink
dataset data types fetch/refresh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
v-rocheleau committed Jul 10, 2024
1 parent a93ab92 commit feb4133
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/components/datasets/DatasetOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import { useDatasetDataTypesByID } from "@/modules/datasets/hooks";
import { datasetPropTypesShape, projectPropTypesShape } from "@/propTypes";

const DatasetOverview = ({ isPrivate, project, dataset }) => {
const datasetDataTypes = useDatasetDataTypesByID(dataset.identifier);
const isFetchingDataset = datasetDataTypes?.isFetching;
const { dataTypesByID, isFetchingDataTypes, hasAttemptedDataTypes } = useDatasetDataTypesByID(dataset.identifier);

// Count data types which actually have data in them for showing in the overview
const dataTypeCount = useMemo(
() => Object.values(datasetDataTypes?.itemsByID || {}).filter((value) => (value.count || 0) > 0).length,
[datasetDataTypes, dataset],
const dataTypesCount = useMemo(
() => Object.values(dataTypesByID ?? {}).filter((value) => (value.count || 0) > 0).length,
[dataTypesByID],
);
const dataTypeDisplay = useMemo(() => {
if (isFetchingDataTypes) {
// refresh: display count based on previous state
if (hasAttemptedDataTypes) return dataTypesCount;
// first fetch: wait for data to display count
return EM_DASH;
}
return dataTypesCount;
}, [dataTypesCount, isFetchingDataTypes, hasAttemptedDataTypes]);

return (
<>
Expand Down Expand Up @@ -52,8 +61,8 @@ const DatasetOverview = ({ isPrivate, project, dataset }) => {
<Statistic title="Created" value={new Date(Date.parse(dataset.created)).toLocaleString()} />
</Col>
<Col span={isPrivate ? 12 : 8}>
<Spin spinning={isFetchingDataset}>
<Statistic title="Data types" value={isFetchingDataset ? EM_DASH : dataTypeCount} />
<Spin spinning={isFetchingDataTypes}>
<Statistic title="Data types" value={dataTypeDisplay} />
</Spin>
</Col>
</Row>
Expand Down
12 changes: 10 additions & 2 deletions src/modules/datasets/hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useMemo } from "react";
import { useProjects } from "@/modules/metadata/hooks";
import { useAppDispatch, useAppSelector } from "@/store";
import { fetchDatasetDataTypesSummariesIfPossible, fetchDatasetsDataTypes } from "@/modules/datasets/actions";
Expand Down Expand Up @@ -27,5 +27,13 @@ export const useDatasetDataTypesByID = (datasetId) => {
useEffect(() => {
dispatch(fetchDatasetDataTypesSummariesIfPossible(datasetId));
}, [datasetId]);
return useAppSelector((state) => state.datasetDataTypes.itemsByID[datasetId]);

const dataTypes = useAppSelector((state) => state.datasetDataTypes.itemsByID[datasetId]);
return useMemo(() => {
return {
dataTypesByID: dataTypes?.itemsByID,
isFetchingDataTypes: dataTypes?.isFetching ?? true,
hasAttemptedDataTypes: dataTypes?.hasAttempted ?? false,
};
}, [dataTypes]);
};
2 changes: 2 additions & 0 deletions src/modules/datasets/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const datasetDataTypes = (
...state.itemsByID,
[datasetID]: {
itemsByID: state.itemsByID[datasetID]?.itemsByID ?? {},
hasAttempted: state.itemsByID[datasetID]?.hasAttempted ?? false,
isFetching: true,
},
},
Expand Down Expand Up @@ -57,6 +58,7 @@ export const datasetDataTypes = (
[datasetID]: {
...state.itemsByID[datasetID],
isFetching: false,
hasAttempted: true,
},
},
};
Expand Down

0 comments on commit feb4133

Please sign in to comment.