From 1663e79bc8cbe47cad4300c474b32d76644eabfd Mon Sep 17 00:00:00 2001 From: NSUWAL123 Date: Mon, 20 Nov 2023 16:55:08 +0545 Subject: [PATCH] feat (projectDetails): generateProjectTiles - validations added when generating projectTiles --- .../src/components/GenerateBasemap.jsx | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/frontend/src/components/GenerateBasemap.jsx b/src/frontend/src/components/GenerateBasemap.jsx index a569d38239..d1091de09b 100644 --- a/src/frontend/src/components/GenerateBasemap.jsx +++ b/src/frontend/src/components/GenerateBasemap.jsx @@ -15,6 +15,7 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI const [selectedTileSource, setSelectedTileSource] = useState(null); const [selectedOutputFormat, setSelectedOutputFormat] = useState(null); const [tmsUrl, setTmsUrl] = useState(''); + const [error, setError] = useState([]); const modalStyle = (theme) => ({ width: '90vw', // Responsive modal width using vw @@ -51,6 +52,35 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI setTmsUrl(e.target.value); }; + const generateProjectTilesValidation = () => { + const currentError = []; + if (!selectedTileSource) { + currentError.push('selectedTileSource'); + } + if (!selectedOutputFormat) { + currentError.push('selectedOutputFormat'); + } + if (!tmsUrl && selectedTileSource === 'tms') { + currentError.push('tmsUrl'); + } + setError(currentError); + return currentError; + }; + + const generateProjectTiles = () => { + const currentErrors = generateProjectTilesValidation(); + if (currentErrors.length === 0) { + dispatch( + GenerateProjectTiles( + `${ + import.meta.env.VITE_API_URL + }/projects/tiles/${decodedId}?source=${selectedTileSource}&format=${selectedOutputFormat}&tms=${tmsUrl}`, + decodedId, + ), + ); + } + }; + return ( + {error.includes('selectedOutputFormat') && ( +

Output Format is Required.

+ )} {/* Tile Source Dropdown or TMS URL Input */} @@ -140,6 +173,9 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI ))} + {error.includes('selectedTileSource') && ( +

Tile Source is Required.

+ )} {selectedTileSource === 'tms' && ( @@ -165,6 +201,7 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI onChange={handleTmsUrlChange} /> + {error.includes('tmsUrl') &&

Tile Source is Required.

}
)} @@ -175,23 +212,7 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI variant="contained" loading={generateProjectTilesLoading} color="error" - onClick={() => { - // Check if 'tms' is selected and tmsUrl is not empty - if (selectedTileSource === 'tms' && !tmsUrl) { - // Handle error, TMS URL is required - console.log('TMS URL is required'); - return; - } - - dispatch( - GenerateProjectTiles( - `${ - import.meta.env.VITE_API_URL - }/projects/tiles/${decodedId}?source=${selectedTileSource}&format=${selectedOutputFormat}&tms=${tmsUrl}`, - decodedId, - ), - ); - }} + onClick={() => generateProjectTiles()} > Generate