Skip to content

Commit 15dd5e6

Browse files
Refactor downloadButton component
Make download button generic and reusable in other components
1 parent 090289c commit 15dd5e6

4 files changed

Lines changed: 51 additions & 26 deletions

File tree

frontend/src/components/DownloadButton.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22
import { Button, Icon } from "@equinor/eds-core-react";
33
import { save } from "@equinor/eds-icons";
44
import { downloadTabulatedDataAsCSV } from "@/functions/Formatting";
5-
import { ExperimentResult } from "@/dto/ExperimentResult";
6-
import { SimulationResults } from "@/dto/SimulationResults";
75

86
interface DownloadButtonProps {
9-
simulationResultsPerExperiment: Record<string, SimulationResults[]>;
10-
experimentResults: ExperimentResult[];
11-
isDisabled?: boolean;
12-
isLoading?: boolean;
7+
csvContent: string;
8+
fileName: string;
9+
isLoading: boolean;
1310
}
1411

15-
const DownloadButton: React.FC<DownloadButtonProps> = ({ simulationResultsPerExperiment, experimentResults }) => {
12+
const DownloadButton: React.FC<DownloadButtonProps> = ({ csvContent, isLoading, fileName }) => {
1613
const handleDownload = () => {
17-
downloadTabulatedDataAsCSV(simulationResultsPerExperiment, experimentResults);
14+
downloadTabulatedDataAsCSV(csvContent, fileName);
1815
};
1916

20-
const hasData = simulationResultsPerExperiment || experimentResults.length > 0;
21-
2217
return (
23-
<Button variant="outlined" onClick={handleDownload} disabled={!hasData} style={{ marginLeft: "1rem" }}>
18+
<Button variant="outlined" onClick={handleDownload} disabled={isLoading} style={{ marginLeft: "1rem" }}>
2419
<Icon data={save} />
25-
{hasData ? "Processing..." : "Download CSV"}
20+
{"Download CSV"}
2621
</Button>
2722
);
2823
};

frontend/src/functions/Formatting.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,8 @@ export const convertExperimentResultsToTabulatedData = (
119119
);
120120
};
121121

122-
export const downloadTabulatedDataAsCSV = (
123-
simulationResultsPerExperiment: Record<string, SimulationResults[]>,
124-
experimentResults: ExperimentResult[]
125-
) => {
126-
const tabulatedData = [
127-
...convertSimulationQueriesResultToTabulatedData(simulationResultsPerExperiment),
128-
...convertExperimentResultsToTabulatedData(experimentResults),
129-
];
130-
131-
if (tabulatedData.length === 0) return;
122+
export function convertTabulatedDataToCSVFormat(tabulatedData: TabulatedResultRow[]): string {
123+
if (tabulatedData.length === 0) return "";
132124

133125
const allKeys = Array.from(
134126
tabulatedData.reduce((set, row) => {
@@ -151,11 +143,14 @@ export const downloadTabulatedDataAsCSV = (
151143
.map((row) => row.map((val) => `"${String(val).replace(/"/g, '""')}"`).join(","))
152144
.join("\r\n");
153145

146+
return csvContent;
147+
}
148+
export const downloadTabulatedDataAsCSV = (csvContent: string, fileName: string) => {
154149
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
155150
const url = URL.createObjectURL(blob);
156151
const a = document.createElement("a");
157152
a.href = url;
158-
a.download = "tabulated_data.csv";
153+
a.download = fileName;
159154
document.body.appendChild(a);
160155
a.click();
161156
document.body.removeChild(a);

frontend/src/pages/LabResults.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { ExperimentResult } from "@/dto/ExperimentResult.tsx";
99
import { useSimulationQueries } from "@/hooks/useSimulationQueriesResult.ts";
1010
import DownloadButton from "@/components/DownloadButton.tsx";
1111
import LabResultSimulationRunsStatus from "@/components/LabResultSimulationRunsStatus.tsx";
12+
import {
13+
convertExperimentResultsToTabulatedData,
14+
convertSimulationQueriesResultToTabulatedData,
15+
convertTabulatedDataToCSVFormat,
16+
} from "@/functions/Formatting";
1217

1318
const LabResults: React.FC = () => {
1419
const [selectedExperiments, setSelectedExperiments] = useState<ExperimentResult[]>([]);
@@ -72,8 +77,14 @@ const LabResults: React.FC = () => {
7277
}}
7378
>
7479
<DownloadButton
75-
simulationResultsPerExperiment={simulationQueryResults.data}
76-
experimentResults={selectedExperiments}
80+
csvContent={convertTabulatedDataToCSVFormat([
81+
...convertSimulationQueriesResultToTabulatedData(simulationQueryResults.data),
82+
...convertExperimentResultsToTabulatedData(selectedExperiments),
83+
])}
84+
fileName={`AcidWatch-LabResults-${new Date().toISOString().replace(/[:.]/g, "-")}.csv`}
85+
isLoading={
86+
isLoading || simulationQueryResults.statuses.some((status) => status.status === "pending")
87+
}
7788
/>
7889
</div>
7990
)}

frontend/src/pages/Models.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { MainContainer } from "@/components/styles";
1414
import CenteredImage from "@/components/CenteredImage";
1515
import noModelImage from "@/assets/no-model-light.svg";
1616
import { useNavigate, useParams } from "react-router-dom";
17+
import DownloadButton from "@/components/DownloadButton";
18+
import { convertSimulationQueriesResultToTabulatedData, convertTabulatedDataToCSVFormat } from "@/functions/Formatting";
1719

1820
const Models: React.FC = () => {
1921
const [currentModel, setCurrentModel] = useState<ModelConfig | undefined>(undefined);
@@ -70,7 +72,29 @@ const Models: React.FC = () => {
7072
} else if (simulationResults === undefined) {
7173
resultsStep = <NoResults />;
7274
} else {
73-
resultsStep = <Results simulationResults={simulationResults} />;
75+
resultsStep = (
76+
<>
77+
<Results simulationResults={simulationResults} />
78+
<div
79+
style={{
80+
display: "flex",
81+
justifyContent: "flex-end",
82+
marginTop: "1rem",
83+
marginBottom: "2rem",
84+
}}
85+
>
86+
<DownloadButton
87+
csvContent={convertTabulatedDataToCSVFormat([
88+
...convertSimulationQueriesResultToTabulatedData({
89+
[`${simulationResults.modelInput.modelId}`]: [simulationResults],
90+
}),
91+
])}
92+
fileName={`AcidWatch-ModelResults-${new Date().toISOString().replace(/[:.]/g, "-")}.csv`}
93+
isLoading={isLoading}
94+
/>
95+
</div>
96+
</>
97+
);
7498
}
7599

76100
return (

0 commit comments

Comments
 (0)