|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { RoadIcon, HomeIcon, WavesIcon, TaskIcon, AsteriskIcon } from '../svgIcons'; |
| 3 | +import FileFormatCard from './fileFormatCard'; |
| 4 | +import Popup from 'reactjs-popup'; |
| 5 | +import { EXPORT_TOOL_S3_URL } from '../../config'; |
| 6 | + |
| 7 | +export const TITLED_ICONS = [ |
| 8 | + { Icon: RoadIcon, title: 'roads', value: 'ROADS' }, |
| 9 | + { Icon: HomeIcon, title: 'buildings', value: 'BUILDINGS' }, |
| 10 | + { Icon: WavesIcon, title: 'waterways', value: 'WATERWAYS' }, |
| 11 | + { Icon: TaskIcon, title: 'landUse', value: 'LAND_USE' }, |
| 12 | + { Icon: AsteriskIcon, title: 'other', value: 'OTHER' }, |
| 13 | +]; |
| 14 | + |
| 15 | +const fileFormats = [{ format: 'SHP' }, { format: 'GEOJSON' }, { format: 'KML' }]; |
| 16 | + |
| 17 | +/** |
| 18 | + * Renders a list of download options for OSM data based on the project mapping types. |
| 19 | + * |
| 20 | + * @param {Array<string>} projectMappingTypes - The mapping types of the project. |
| 21 | + * @return {JSX.Element} - The JSX element containing the download options. |
| 22 | + */ |
| 23 | + |
| 24 | +export const DownloadOsmData = ({ projectMappingTypes, project }) => { |
| 25 | + const [showPopup, setShowPopup] = useState(false); |
| 26 | + const [isDownloadingState, setIsDownloadingState] = useState(null); |
| 27 | + |
| 28 | + /** |
| 29 | + * Downloads an S3 file from the given URL and saves it as a file. |
| 30 | + * |
| 31 | + * @param {string} title - The title of the file. |
| 32 | + * @param {string} fileFormat - The format of the file. |
| 33 | + * @return {Promise<void>} Promise that resolves when the download is complete. |
| 34 | + */ |
| 35 | + const downloadS3File = async (title, fileFormat) => { |
| 36 | + // Create the base URL for the S3 file |
| 37 | + const baseUrl = `${EXPORT_TOOL_S3_URL}/TM/${project.projectId}/hotosm_project_${ |
| 38 | + project.projectId |
| 39 | + }_${title}_${fileFormat?.toLowerCase()}.zip`; |
| 40 | + |
| 41 | + // Set the state to indicate that the file download is in progress |
| 42 | + setIsDownloadingState({ title: title, fileFormat: fileFormat, isDownloading: true }); |
| 43 | + |
| 44 | + try { |
| 45 | + // Fetch the file from the S3 URL |
| 46 | + const response = await fetch(baseUrl); |
| 47 | + |
| 48 | + // Check if the request was successful |
| 49 | + if (response.ok) { |
| 50 | + // Set the state to indicate that the file download is complete |
| 51 | + setIsDownloadingState({ title: title, fileFormat: fileFormat, isDownloading: false }); |
| 52 | + |
| 53 | + // Get the file data as a blob |
| 54 | + const blob = await response.blob(); |
| 55 | + |
| 56 | + // Create a download link for the file |
| 57 | + const href = window.URL.createObjectURL(blob); |
| 58 | + const link = document.createElement('a'); |
| 59 | + link.href = href; |
| 60 | + link.setAttribute( |
| 61 | + 'download', |
| 62 | + `hotosm_project_${project.projectId}_${title}_${fileFormat?.toLowerCase()}.zip`, |
| 63 | + ); |
| 64 | + |
| 65 | + // Add the link to the document body, click it, and then remove it |
| 66 | + document.body.appendChild(link); |
| 67 | + link.click(); |
| 68 | + document.body.removeChild(link); |
| 69 | + } else { |
| 70 | + // Show a popup and throw an error if the request was not successful |
| 71 | + setShowPopup(true); |
| 72 | + throw new Error(`Request failed with status: ${response.status}`); |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + // Show a popup and log the error if an error occurs during the download |
| 76 | + setShowPopup(true); |
| 77 | + setIsDownloadingState({ title: title, fileFormat: fileFormat, isDownloading: false }); |
| 78 | + console.error('Error:', error.message); |
| 79 | + } |
| 80 | + }; |
| 81 | + const filteredMappingTypes = TITLED_ICONS?.filter((icon) => |
| 82 | + projectMappingTypes?.includes(icon.value), |
| 83 | + ); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="mb5 w-100 pa5 ph flex flex-wrap"> |
| 87 | + <Popup modal open={showPopup} closeOnDocumentClick nested onClose={() => setShowPopup(false)}> |
| 88 | + {(close) => ( |
| 89 | + <div class="blue-dark bg-white pv2 pv4-ns ph2 ph4-ns"> |
| 90 | + <h3 class="barlow-condensed f3 fw6 mv0">Data Not Available.</h3> |
| 91 | + <div class="w-100 pt3 flex justify-end"> |
| 92 | + <button |
| 93 | + aria-pressed="false" |
| 94 | + focusindex="0" |
| 95 | + class="mr2 bg-red white br1 f5 bn pointer" |
| 96 | + style={{ padding: '0.75rem 1.5rem' }} |
| 97 | + onClick={() => { |
| 98 | + setShowPopup(false); |
| 99 | + close(); |
| 100 | + }} |
| 101 | + > |
| 102 | + Close |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + )} |
| 107 | + </Popup> |
| 108 | + {filteredMappingTypes.map((type) => ( |
| 109 | + <div |
| 110 | + className="osm-card bg-white pa3 mr4 mt4 w-auto-m flex flex-wrap items-center " |
| 111 | + style={{ |
| 112 | + width: '560px', |
| 113 | + gap: '16px', |
| 114 | + }} |
| 115 | + key={type.title} |
| 116 | + > |
| 117 | + <div |
| 118 | + style={{ |
| 119 | + justifyContent: 'center', |
| 120 | + display: 'flex', |
| 121 | + alignItems: 'center', |
| 122 | + }} |
| 123 | + > |
| 124 | + <type.Icon |
| 125 | + title={type.title} |
| 126 | + color="#D73F3F" |
| 127 | + className="br1 h2 w2 pa1 ma1 ba b--white bw1 dib h-65 w-65" |
| 128 | + style={{ height: '56px' }} |
| 129 | + /> |
| 130 | + </div> |
| 131 | + |
| 132 | + <div |
| 133 | + className="file-list flex barlow-condensed f3" |
| 134 | + style={{ display: 'flex', gap: '12px' }} |
| 135 | + > |
| 136 | + <p className="fw5 ttc">{type.title}</p> |
| 137 | + <FileFormatCard |
| 138 | + title={type.title} |
| 139 | + fileFormats={fileFormats} |
| 140 | + downloadS3Data={downloadS3File} |
| 141 | + isDownloadingState={isDownloadingState} |
| 142 | + /> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ))} |
| 146 | + </div> |
| 147 | + ); |
| 148 | +}; |
0 commit comments