From 4203350268cda492b9b386f991fc73281ac2849e Mon Sep 17 00:00:00 2001
From: Kristjan Eimre
Date: Sat, 16 Nov 2024 00:35:15 +0200
Subject: [PATCH] adapt to the new api organization
---
src/MainPage/DownloadButton.jsx | 80 +++++++++++++++++++++------------
src/MainPage/loadIndexMc2d.js | 10 +++--
src/MainPage/restapiText.jsx | 40 +++++++++++------
src/common/restApiUtils.js | 24 ++++++++--
4 files changed, 104 insertions(+), 50 deletions(-)
diff --git a/src/MainPage/DownloadButton.jsx b/src/MainPage/DownloadButton.jsx
index eac60b3..86fb34d 100644
--- a/src/MainPage/DownloadButton.jsx
+++ b/src/MainPage/DownloadButton.jsx
@@ -1,8 +1,13 @@
+import React, { useState } from "react";
+import { Spinner } from "react-bootstrap";
+
import { saveAs } from "file-saver";
import { HelpButton } from "mc-react-library";
import { Popover } from "react-bootstrap";
+import { loadStructureUuids, AIIDA_REST_API_URL } from "../common/restApiUtils";
+
import "./DownloadButton.css";
const getCurrentDateString = () => {
@@ -27,40 +32,47 @@ const popover = (
the Materials Grid. The data is downloaded in JSON format, as an array.
The array contains a JSON object for each material entry, with key-value
pairs corresponding to the column properties. Additionally, each entry
- includes a link to the corresponding detail page.
+ includes a link to the corresponding detail page and a link to download
+ the file in CIF format.
+
+ Note, to download all of the CIF files directly, see the corresponding
+ Materials Cloud Archive entry.
);
export const DownloadButton = ({ materialSelectorRef, disabled }) => {
- /*
- Note: the plan is to potentially also include direct download links (via the AiiDA rest api)
- to each of the materials in the downloaded file, but currently the index page doesn't have
- the structure UUIDs. Including them in the default index download is not great, as they would
- increase the initial download size considerably, while not really needed for the table.
-
- Therefore, it probably might make sense to implement an additional endpoint, e.g. pbe-v1/uuids
- that is only called when this download button is clicked.
- */
+ const [isLoading, setIsLoading] = useState(false);
- const handleDownload = () => {
+ const handleDownload = async () => {
if (materialSelectorRef.current) {
- const data = materialSelectorRef.current.getFilteredRows();
- // href currently just contains the url subpath to the details page
- // replace it with a better name and full url
- let modData = data.map((entry) => {
- let modEntry = {
- ...entry,
- details_link: `${window.location.origin}${entry.href}`,
- };
- delete modEntry.href;
- return modEntry;
- });
- const json = JSON.stringify(modData, null, 2);
- const blob = new Blob([json], { type: "application/json" });
- const filename = `mc2d_filtered_entries_${getCurrentDateString()}.json`;
- saveAs(blob, filename);
+ setIsLoading(true);
+ try {
+ const structureUuids = await loadStructureUuids();
+ const data = materialSelectorRef.current.getFilteredRows();
+
+ let modData = data.map((entry) => {
+ let uuid = structureUuids[entry.id];
+ let downloadLink = `${AIIDA_REST_API_URL}/nodes/${uuid}/download?download_format=cif`;
+ let modEntry = {
+ ...entry,
+ details_link: `${window.location.origin}${entry.href}`,
+ download_cif: downloadLink,
+ };
+ delete modEntry.href;
+ return modEntry;
+ });
+
+ const json = JSON.stringify(modData, null, 2);
+ const blob = new Blob([json], { type: "application/json" });
+ const filename = `mc2d_filtered_entries_${getCurrentDateString()}.json`;
+ saveAs(blob, filename);
+ } catch (error) {
+ console.error("Error downloading data:", error);
+ } finally {
+ setIsLoading(false);
+ }
}
};
@@ -68,11 +80,21 @@ export const DownloadButton = ({ materialSelectorRef, disabled }) => {