-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from Azure/imdf-save-edits
Downloadable GeoJSON Files with Edits + Improved GeoJSON Editor
- Loading branch information
Showing
10 changed files
with
788 additions
and
216 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
import { cx } from '@emotion/css'; | ||
import { PrimaryButton } from '@fluentui/react'; | ||
import { failedLogsButton, logsButton } from './style'; | ||
import { cx } from '@emotion/css'; | ||
import { PrimaryButton } from '@fluentui/react'; | ||
import { logsButton } from './style'; | ||
import React, { useState } from 'react'; | ||
import JSZip from 'jszip'; | ||
import { saveAs } from 'file-saver'; | ||
|
||
// This component downloads the updated IMDF zip file and is triggered by users clicking "Download IMDF" button | ||
export const DownloadIMDF = ({ imdfPackageLocation, units, levels, footprint, building }) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
// Fetches current zip file, updates the geojson files and downloads the updated zip | ||
const handleUpdateZip = () => { | ||
setIsLoading(true); | ||
fetch(imdfPackageLocation) | ||
.then(response => response.arrayBuffer()) | ||
.then(data => { | ||
const zip = new JSZip(); | ||
return zip.loadAsync(data); | ||
}) | ||
.then(zip => { | ||
zip.file('unit.geojson', JSON.stringify(units, null, 2)); | ||
zip.file('level.geojson', JSON.stringify(levels, null, 2)); | ||
zip.file('footprint.geojson', JSON.stringify(footprint, null, 2)); | ||
zip.file('building.geojson', JSON.stringify(building, null, 2)); | ||
return zip.generateAsync({ type: 'blob' }); | ||
}) | ||
.then(updatedZip => { | ||
saveAs(updatedZip, 'updated_imdf_package.zip'); | ||
setIsLoading(false); | ||
}) | ||
.catch(() => { | ||
setIsLoading(false); | ||
}); | ||
}; | ||
|
||
export const DownloadIMDF = ({ isFailed, link }) => { | ||
return ( | ||
<a href={link} download target="_blank" rel="noreferrer"> | ||
<PrimaryButton className={cx(logsButton, { [failedLogsButton]: isFailed })}>Download IMDF</PrimaryButton> | ||
</a> | ||
<div style={{ cursor: isLoading ? 'wait' : 'default' }}> | ||
<PrimaryButton onClick={handleUpdateZip} disabled={isLoading} className={cx(logsButton)}> | ||
{isLoading ? 'Downloading..' : 'Download IMDF'} | ||
</PrimaryButton> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.