Skip to content

Commit

Permalink
DT-903: Change export button to link directly to terra-ui import page
Browse files Browse the repository at this point in the history
Instead of performing a two-step operation in the duos UI, use the new TDR import link to have terra-ui perform the TDR export step.
  • Loading branch information
pshapiro4broad authored Nov 5, 2024
1 parent 1f4568c commit 2bada7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 71 deletions.
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`;

return <Link style={{marginRight: '5px'}} 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);
}
}
},
};

0 comments on commit 2bada7b

Please sign in to comment.