Skip to content

Commit

Permalink
resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar-s committed Jan 19, 2024
2 parents 10763c5 + 0960280 commit c803191
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 44 deletions.
8 changes: 6 additions & 2 deletions packages/naksha-components-react/package.json
@@ -1,6 +1,10 @@
{
"name": "naksha-components-react",
<<<<<<< HEAD
"version": "4.5.7",
=======
"version": "4.5.6-beta.3",
>>>>>>> 09602807c9d3a46e710cd4e1b103bad37fbe15bf
"author": "harshzalavadiya",
"main": "./dist/index.js",
"module": "./dist/esm/index.js",
Expand All @@ -21,7 +25,7 @@
},
"dependencies": {
"@biodiv-platform/eslint-preset": "4.4.9",
"@biodiv-platform/naksha-gmaps-draw": "4.5.2",
"@biodiv-platform/naksha-gmaps-draw": "4.5.3-beta.2",
"@biodiv-platform/naksha-gmaps-view": "4.4.9",
"@biodiv-platform/naksha-mapbox-draw": "4.4.9",
"@biodiv-platform/naksha-mapbox-list": "4.5.0",
Expand All @@ -38,4 +42,4 @@
"publishConfig": {
"access": "public"
}
}
}
4 changes: 2 additions & 2 deletions packages/naksha-gmaps-draw/package.json
@@ -1,6 +1,6 @@
{
"name": "@biodiv-platform/naksha-gmaps-draw",
"version": "4.5.2",
"version": "4.5.3-beta.2",
"author": "harshzalavadiya",
"main": "./dist/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -36,4 +36,4 @@
"@biodiv-platform/naksha-commons": "*",
"@turf/bbox": "^6.5.0"
}
}
}
60 changes: 60 additions & 0 deletions packages/naksha-gmaps-draw/src/ModalImport/index.tsx
@@ -0,0 +1,60 @@
import React, { useRef, useState, useEffect } from "react";
import { GMAP_FEATURE_TYPES } from "../static/constants";
import NakshaImport from "../import";
import GeojsonImport from "../geojson";

export default function ModalImport({
ButtonComponent,
ModalComponent,
addFeature,
InputComponent,
ButtonComponentModal,
DeleteIcon,
LocationIcon,
FileIcon,
SuccessIcon,
FailureIcon,
}) {
const [isModalOpen, setIsModalOpen] = useState(false);

const handleModalClose = () => {
setIsModalOpen(false);
};

const handleOpenModal = () => {
setIsModalOpen(true);
};

return (
<div style={{ display: "flex" }}>
{React.cloneElement(ModalComponent, {
isOpen: isModalOpen,
onClose: handleModalClose,
nakshaImport: (
<NakshaImport
addFeature={addFeature}
InputComponent={InputComponent}
ButtonComponent={ButtonComponent}
LocationIcon={LocationIcon}
SuccessIcon={SuccessIcon}
FailureIcon={FailureIcon}
/>
),
geoJSONImport: (
<GeojsonImport
addFeature={addFeature}
ButtonComponent={ButtonComponent}
DeleteIcon={DeleteIcon}
FileIcon={FileIcon}
SuccessIcon={SuccessIcon}
FailureIcon={FailureIcon}
/>
),
})}
{React.cloneElement(ButtonComponentModal, {
onClick: handleOpenModal,
type: "button",
})}
</div>
);
}
292 changes: 292 additions & 0 deletions packages/naksha-gmaps-draw/src/geojson/index.tsx
@@ -0,0 +1,292 @@
import React, { useState, useRef } from "react";

const GeojsonImport = ({ addFeature, ButtonComponent, FileIcon, SuccessIcon, FailureIcon, DeleteIcon }) => {
const [isGeoJSONValid, setIsGeoJSONValid] = useState(true);
const [uploadStatus, setUploadStatus] = useState("");
const [filesToUpload, setFilesToUpload] = useState([]);
const [uploadedFiles, setUploadedFiles] = useState([]);
const fileInputRef = useRef(null);

const handleDragEnter = (e) => {
e.preventDefault();
};

const handleDragOver = (e) => {
e.preventDefault();
};

const handleDrop = (e) => {
e.preventDefault();
const droppedFiles = Array.from(e.dataTransfer.files);
setFilesToUpload(droppedFiles);
};

const handleFileInputChange = () => {
const selectedFiles = Array.from(fileInputRef.current.files);
setFilesToUpload(selectedFiles);
};

const handleAddFeature = () => {
try {
filesToUpload.forEach((file) => {
const reader = new FileReader();

reader.onabort = () =>
handleError("File reading was aborted. Please try again.");
reader.onerror = () =>
handleError("File reading has failed. Please try again.");

reader.onload = () => {
const result = reader.result;

if (result) {
const jsonString =
typeof result === "string"
? result
: new TextDecoder().decode(result);

try {
const geojsonObject = JSON.parse(jsonString);

const featuresToAdd = Array.isArray(geojsonObject.features)
? geojsonObject.features.map((feature) => ({
...feature,
isGeojson: true,
}))
: [{ ...geojsonObject, isGeojson: true }];

featuresToAdd.forEach((featureToAdd) => {
addFeature([featureToAdd]);
});

setUploadStatus("Geojson added successfully");
setIsGeoJSONValid(true);

// Update the list of successfully uploaded files
setUploadedFiles((prevFiles) => [
...prevFiles,
{ name: file.name },
]);

// Reset the dropzone content
setFilesToUpload([]);
} catch (error) {
console.error(error);
handleError("Upload failed. Invalid GeoJSON format.");
}
} else {
handleError("File reading result is null.");
}
};

reader.readAsText(file);
});
} catch (error) {
console.error(error);
handleError("File upload failed. Please try again.");
} finally {
setTimeout(() => {
setUploadStatus("");
setIsGeoJSONValid(true);
}, 2000);
}
};

const handleError = (message) => {
setUploadStatus(message);
setIsGeoJSONValid(false);
};

const removeFile = () => {
setFilesToUpload([]);
};

return (
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
marginTop: "10px",
}}
onDragEnter={handleDragEnter}
onDragOver={handleDragOver}
onDrop={handleDrop}
>
<h1
style={{
marginBottom: "10px",
fontWeight: "bold",
fontSize: "1.25rem",
}}
>
GeoJSON
</h1>

<div
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
}}
>
<div
style={{
border: "2px solid #519895",
borderRadius: "4px",
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "10px",
cursor: "pointer",
marginLeft: "15px",
width: "300px",
height: "100px",
}}
onClick={() => fileInputRef.current.click()}
>
<input
type="file"
ref={fileInputRef}
onChange={handleFileInputChange}
style={{ display: "none" }}
/>

<div
style={{
display: "flex",
alignItems: "center",
}}
>
{filesToUpload.length > 0 ? (
<div
style={{
marginLeft: "10px",
display: "flex",
alignItems: "center",
}}
>
<div
style={{
marginTop: "10px",
overflow: "hidden",
whiteSpace: "pre-wrap",
textOverflow: "ellipsis",
maxWidth: "200px",
}}
>
{filesToUpload[0].name}
</div>
<div
style={{
color: "red",
marginLeft: "10px",
marginTop: "10px",
cursor: "pointer",
}}
onClick={removeFile}
>
{React.cloneElement(DeleteIcon, { boxSize: 6 })}
</div>
</div>
) : (
<>
<div>Drag 'n' drop GeoJSON file here, or click to select a file</div>
</>
)}
</div>
</div>

{React.cloneElement(ButtonComponent, {
type: "button",
marginLeft: "30px",
style: { paddingLeft: "15px" },
onClick: handleAddFeature,
})}
</div>

{uploadStatus && (
<div
style={{
fontWeight: "bold",
color: isGeoJSONValid ? "green" : "red",
marginTop: "10px",
display: "flex",
alignItems: "center",
}}
>
{isGeoJSONValid ? (
<span
style={{
marginRight: "5px",
fontSize: "1.25rem",
}}
>
{React.cloneElement(SuccessIcon, { boxSize: 6 })}
</span>
) : (
<span
style={{
marginRight: "5px",
fontSize: "1.25rem",
color: "red",
}}
>
{React.cloneElement(FailureIcon, { boxSize: 4 })}
</span>
)}
{uploadStatus}
</div>
)}

{/* Display the list of successfully uploaded files */}
{uploadedFiles.length > 0 && (
<div
style={{
marginTop: "10px",
paddingLeft: "15px",
}}
>
<div
style={{
fontSize: "sm",
}}
>
Added GeoJSONs:
</div>
<ul
style={{
listStyleType: "none",
padding: "0",
}}
>
{uploadedFiles.map((file, index) => (
<li
key={index}
style={{
display: "flex",
alignItems: "center",
}}
>
{React.cloneElement(FileIcon, { boxSize: 6, marginRight: 2 })}
<div
style={{
marginTop: "10px",
overflow: "hidden",
whiteSpace: "pre-wrap",
textOverflow: "ellipsis",
maxWidth: "300px",
}}
>
{file.name}
</div>
</li>
))}
</ul>
</div>
)}
</div>
);
};

export default GeojsonImport;

0 comments on commit c803191

Please sign in to comment.