Skip to content

Commit

Permalink
feat(clusters-ui): use async loading from GroupedDataTable
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Mar 21, 2024
1 parent 4c1e1f3 commit 295f2d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import {
capacityAggregationFn,
useClusterDataWithCapacity,
} from "../common/utils";
import SimpleLoader from "../../../../../../common/loaders/SimpleLoader";
import SimpleContent from "../../../../../../common/page/SimpleContent";
import UsePromiseCond from "../../../../../../common/utils/UsePromiseCond";

function Renewables() {
const { study } = useOutletContext<{ study: StudyMetadata }>();
Expand All @@ -32,11 +29,11 @@ function Renewables() {
const columnHelper = createMRTColumnHelper<RenewableClusterWithCapacity>();

const {
clusters,
clustersWithCapacity,
totalUnitCount,
totalInstalledCapacity,
totalEnabledCapacity,
isLoading,
} = useClusterDataWithCapacity<RenewableCluster>(
() => getRenewableClusters(study.id, areaId),
t("studies.error.retrieveData"),
Expand Down Expand Up @@ -130,20 +127,14 @@ function Renewables() {
////////////////////////////////////////////////////////////////

return (
<UsePromiseCond
response={clusters}
ifPending={() => <SimpleLoader />}
ifResolved={() => (
<GroupedDataTable
data={clustersWithCapacity}
columns={columns}
groups={RENEWABLE_GROUPS}
onCreate={handleCreateRow}
onDelete={handleDeleteSelection}
onNameClick={handleNameClick}
/>
)}
ifRejected={(error) => <SimpleContent title={error?.toString()} />}
<GroupedDataTable
isLoading={isLoading}
data={clustersWithCapacity}
columns={columns}
groups={RENEWABLE_GROUPS}
onCreate={handleCreateRow}
onDelete={handleDeleteSelection}
onNameClick={handleNameClick}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import { StudyMetadata } from "../../../../../../../common/types";
import useAppSelector from "../../../../../../../redux/hooks/useAppSelector";
import { getCurrentAreaId } from "../../../../../../../redux/selectors";
import GroupedDataTable from "../../../../../../common/GroupedDataTable";
import SimpleLoader from "../../../../../../common/loaders/SimpleLoader";
import {
Storage,
getStorages,
deleteStorages,
createStorage,
STORAGE_GROUPS,
} from "./utils";
import SimpleContent from "../../../../../../common/page/SimpleContent";
import UsePromiseCond from "../../../../../../common/utils/UsePromiseCond";
import usePromiseWithSnackbarError from "../../../../../../../hooks/usePromiseWithSnackbarError";

function Storages() {
Expand All @@ -27,7 +24,7 @@ function Storages() {
const areaId = useAppSelector(getCurrentAreaId);
const columnHelper = createMRTColumnHelper<Storage>();

const storages = usePromiseWithSnackbarError(
const { data: storages, isLoading } = usePromiseWithSnackbarError(
() => getStorages(study.id, areaId),
{
errorMessage: t("studies.error.retrieveData"),
Expand All @@ -37,14 +34,14 @@ function Storages() {

const { totalWithdrawalNominalCapacity, totalInjectionNominalCapacity } =
useMemo(() => {
if (!storages.data) {
if (!storages) {
return {
totalWithdrawalNominalCapacity: 0,
totalInjectionNominalCapacity: 0,
};
}

return storages.data.reduce(
return storages.reduce(
(acc, { withdrawalNominalCapacity, injectionNominalCapacity }) => {
acc.totalWithdrawalNominalCapacity += withdrawalNominalCapacity;
acc.totalInjectionNominalCapacity += injectionNominalCapacity;
Expand Down Expand Up @@ -175,20 +172,14 @@ function Storages() {
////////////////////////////////////////////////////////////////

return (
<UsePromiseCond
response={storages}
ifPending={() => <SimpleLoader />}
ifResolved={(data) => (
<GroupedDataTable
data={data}
columns={columns}
groups={STORAGE_GROUPS}
onCreate={handleCreateRow}
onDelete={handleDeleteSelection}
onNameClick={handleNameClick}
/>
)}
ifRejected={(error) => <SimpleContent title={error?.toString()} />}
<GroupedDataTable
isLoading={isLoading}
data={storages || []}
columns={columns}
groups={STORAGE_GROUPS}
onCreate={handleCreateRow}
onDelete={handleDeleteSelection}
onNameClick={handleNameClick}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import {
import useAppSelector from "../../../../../../../redux/hooks/useAppSelector";
import { getCurrentAreaId } from "../../../../../../../redux/selectors";
import GroupedDataTable from "../../../../../../common/GroupedDataTable";
import SimpleLoader from "../../../../../../common/loaders/SimpleLoader";
import SimpleContent from "../../../../../../common/page/SimpleContent";
import {
capacityAggregationFn,
useClusterDataWithCapacity,
} from "../common/utils";
import UsePromiseCond from "../../../../../../common/utils/UsePromiseCond";

function Thermal() {
const { study } = useOutletContext<{ study: StudyMetadata }>();
Expand All @@ -32,11 +29,11 @@ function Thermal() {
const columnHelper = createMRTColumnHelper<ThermalClusterWithCapacity>();

const {
clusters,
clustersWithCapacity,
totalUnitCount,
totalInstalledCapacity,
totalEnabledCapacity,
isLoading,
} = useClusterDataWithCapacity<ThermalCluster>(
() => getThermalClusters(study.id, areaId),
t("studies.error.retrieveData"),
Expand Down Expand Up @@ -144,20 +141,14 @@ function Thermal() {
////////////////////////////////////////////////////////////////

return (
<UsePromiseCond
response={clusters}
ifPending={() => <SimpleLoader />}
ifResolved={() => (
<GroupedDataTable
data={clustersWithCapacity}
columns={columns}
groups={THERMAL_GROUPS}
onCreate={handleCreateRow}
onDelete={handleDeleteSelection}
onNameClick={handleNameClick}
/>
)}
ifRejected={(error) => <SimpleContent title={error?.toString()} />}
<GroupedDataTable
isLoading={isLoading}
data={clustersWithCapacity}
columns={columns}
groups={THERMAL_GROUPS}
onCreate={handleCreate}
onDelete={handleDelete}
onNameClick={handleNameClick}
/>
);
}
Expand Down

0 comments on commit 295f2d8

Please sign in to comment.