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

DT-903: Change export button to link directly to terra-ui import page #2711

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 12 additions & 43 deletions src/components/data_search/DatasetExportButton.jsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
import * as React from 'react';
import { CircularProgress, IconButton, Link } from '@mui/material';
import { useState } from 'react';
import IosShareIcon from '@mui/icons-material/IosShare';
import { TerraDataRepo } from '../../libs/ajax/TerraDataRepo';
import {useEffect, useState} from 'react';
import {Link} from '@mui/material';
import {Config} from '../../libs/config';

export const DatasetExportButton = (props) => {
const { snapshot, title } = props;
// The exportStatus flow is: initial -> prepping -> ready
// TODO: error handling?
const [exportStatus, setExportStatus] = useState('initial');
const [exportResult, setExportResult] = useState(null);
const {snapshot, title} = props;

// Not a supported export location
if (!snapshot) {
return null;
}
const [terraUrl, setTerraUrl] = useState('');

const prepExportHandler = async () => {
setExportStatus('prepping');
const job = await TerraDataRepo.prepareExport(snapshot.id);
const result = await TerraDataRepo.waitForJob(job.id);
setExportResult(result);
setExportStatus('ready');
};
useEffect(() => {
(async () => {
setTerraUrl(await Config.getTerraUrl());
})();
}, []);

if (exportStatus === 'initial') {
return (
<IconButton aria-label="prepare export to Terra" size="medium" onClick={prepExportHandler}>
<IosShareIcon size={15} />
</IconButton>
);
}

if (exportStatus === 'prepping') {
return (
<IconButton aria-label="prepare export to Terra" size="medium" onClick={() => ({})} disabled>
<CircularProgress size={15} />,
</IconButton>
);
}

if (exportStatus === 'ready') {
return (
<Link href={exportResult.terraImportLink} target="_blank" rel="noopener noreferrer" title={title} aria-label={title}>Export</Link>
);
}

return null;
const link = `${terraUrl}/#import-data?snapshotId=${snapshot.id}&format=tdrexport&tdrSyncPermissions=false`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return <Link href={link} target="_blank" rel="noopener noreferrer" title={title} aria-label={title}>Export</Link>;
};

export default DatasetExportButton;
28 changes: 0 additions & 28 deletions src/libs/ajax/TerraDataRepo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Config } from '../config';
import axios from 'axios';
import { sleep, reportError } from '../ajax';


export const TerraDataRepo = {
Expand All @@ -10,31 +9,4 @@ export const TerraDataRepo = {
const res = await axios.get(url, Config.authOpts());
return res.data;
},

prepareExport: async (snapshotId) => {
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/snapshots/${snapshotId}/export`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

waitForJob: async (jobId) => {
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/jobs/${jobId}`;
const resultsUrl = `${await Config.getTdrApiUrl()}/api/repository/v1/jobs/${jobId}/result`;

// eslint-disable-next-line no-constant-condition
while (true) {
const res = await axios.get(url, Config.authOpts());
if (res.data.job_status === 'running') {
await sleep(1000);
} else if (res.data.job_status === 'succeeded') {
const finalResult = await axios.get(resultsUrl, Config.authOpts());
// Add the URL to link to
finalResult.data.terraImportLink =
`${await Config.getTerraUrl()}/#import-data?url=${window.location.origin}&snapshotId=${finalResult.data.snapshot.id}&format=tdrexport&snapshotName=${finalResult.data.snapshot.name}&tdrmanifest=${encodeURIComponent(finalResult.data.format.parquet.manifest)}&tdrSyncPermissions=false`;
return finalResult.data;
} else if (res.data.job_status === 'failed') {
return reportError(url, res.data.status_code);
}
}
},
};
Loading