Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dataset data-types count loading #413

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/components/datasets/DatasetOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import PropTypes from "prop-types";
import { Col, Divider, Row, Spin, Statistic, Typography } from "antd";

import { EM_DASH } from "@/constants";
import { useDatasetDataTypes } from "@/modules/datasets/hooks";
import { useDatasetDataTypesByID } from "@/modules/datasets/hooks";
import { datasetPropTypesShape, projectPropTypesShape } from "@/propTypes";

const DatasetOverview = ({ isPrivate, project, dataset }) => {
const datasetsDataTypes = useDatasetDataTypes();
const datasetDataTypes = datasetsDataTypes[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
v-rocheleau marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -54,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
28 changes: 26 additions & 2 deletions src/modules/datasets/hooks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useEffect } from "react";
import { useEffect, useMemo } from "react";
import { useProjects } from "@/modules/metadata/hooks";
import { useAppDispatch, useAppSelector } from "@/store";
import { fetchDatasetsDataTypes } from "@/modules/datasets/actions";
import { fetchDatasetDataTypesSummariesIfPossible, fetchDatasetsDataTypes } from "@/modules/datasets/actions";

export const useDatasetDataTypes = () => {
/**
* Fetches the data type summaries for ALL datasets.
* returns the store's values.
*/
const dispatch = useAppDispatch();
const { datasetsByID } = useProjects();
useEffect(() => {
Expand All @@ -13,3 +17,23 @@ export const useDatasetDataTypes = () => {
}, [dispatch, datasetsByID]);
return useAppSelector((state) => state.datasetDataTypes);
};

export const useDatasetDataTypesByID = (datasetId) => {
/**
* Fetches the data types ONLY for the given dataset.
* Returns the store's value for the given dataset ID.
*/
const dispatch = useAppDispatch();
useEffect(() => {
dispatch(fetchDatasetDataTypesSummariesIfPossible(datasetId));
}, [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