-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #186
- Loading branch information
Showing
4 changed files
with
176 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { submitJob } from '@arcgis/core/rest/geoprocessor'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { Button, Select, SelectItem, Spinner } from '@ugrc/utah-design-system'; | ||
import { useState } from 'react'; | ||
import config from '../config'; | ||
|
||
async function download(eventIds: string[], format: string): Promise<string> { | ||
const jobInfo = await submitJob(config.urls.download, { | ||
Event_Ids: eventIds.join(';'), | ||
Format: format, | ||
}); | ||
|
||
await jobInfo.waitForJobCompletion(); | ||
|
||
const parameter = await jobInfo.fetchResultData('Zip_File'); | ||
const value = parameter.value as __esri.DataFile; | ||
|
||
return value.url.replace('http', 'https'); | ||
} | ||
|
||
type State = { | ||
format: string; | ||
error: string | null; | ||
isBusy: boolean; | ||
}; | ||
|
||
export default function Download({ eventIds }: { eventIds: string[] }): JSX.Element { | ||
const queryClient = useQueryClient(); | ||
const [state, setState] = useState<State>({ | ||
format: '', | ||
error: null, | ||
isBusy: false, | ||
}); | ||
|
||
const updateState = (newState: Partial<State>) => setState((prev) => ({ ...prev, ...newState })); | ||
|
||
const onDownloadClick = async () => { | ||
if (eventIds.length === 0) { | ||
console.warn('No data to download'); | ||
|
||
return; | ||
} | ||
|
||
updateState({ isBusy: true, error: null }); | ||
|
||
let url; | ||
try { | ||
url = await queryClient.fetchQuery({ | ||
queryKey: ['download', eventIds], | ||
queryFn: () => download(eventIds as string[], state.format), | ||
}); | ||
} catch (error) { | ||
console.error('Error downloading data', error); | ||
updateState({ error: 'There was an error with the download service', isBusy: false }); | ||
|
||
return; | ||
} | ||
|
||
const link = document.createElement('a'); | ||
link.href = url; | ||
link.download = 'data.zip'; | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
|
||
updateState({ isBusy: false }); | ||
}; | ||
|
||
return ( | ||
<div className="p-4"> | ||
<p>This can take a few minutes to process.</p> | ||
<Select | ||
placeholder="select format" | ||
selectedKey={state.format} | ||
onSelectionChange={(key) => updateState({ format: key as string })} | ||
aria-label="select format" | ||
className="inline-flex py-2" | ||
> | ||
<SelectItem id="csv" aria-label="CSV"> | ||
CSV | ||
</SelectItem> | ||
<SelectItem id="fgdb" aria-label="File Geodatabase"> | ||
File Geodatabase | ||
</SelectItem> | ||
</Select> | ||
<p> | ||
<Button | ||
isDisabled={!eventIds.length || state.isBusy || state.format === ''} | ||
variant="secondary" | ||
onPress={onDownloadClick} | ||
> | ||
{state.isBusy ? <Spinner /> : 'Download'} | ||
</Button> | ||
</p> | ||
{state.error && <div className="text-sm text-rose-600 forced-colors:text-[Mark]">{state.error}</div>} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters