Skip to content

Commit

Permalink
feat (projectDetails): generateProjectTiles - validations added when …
Browse files Browse the repository at this point in the history
…generating projectTiles
  • Loading branch information
NSUWAL123 committed Nov 20, 2023
1 parent 11e2697 commit 1663e79
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/frontend/src/components/GenerateBasemap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
<CoreModules.CustomizedModal
isOpen={!!toggleGenerateModal}
Expand Down Expand Up @@ -105,6 +135,9 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI
))}
</CoreModules.Select>
</CoreModules.FormControl>
{error.includes('selectedOutputFormat') && (
<p className="fmtm-text-sm fmtm-text-red-500">Output Format is Required.</p>
)}
</CoreModules.Grid>

{/* Tile Source Dropdown or TMS URL Input */}
Expand Down Expand Up @@ -140,6 +173,9 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI
))}
</CoreModules.Select>
</CoreModules.FormControl>
{error.includes('selectedTileSource') && (
<p className="fmtm-text-sm fmtm-text-red-500">Tile Source is Required.</p>
)}
</CoreModules.Grid>
{selectedTileSource === 'tms' && (
<CoreModules.Grid item xs={12} sm={12} md={4}>
Expand All @@ -165,6 +201,7 @@ const GenerateBasemap = ({ setToggleGenerateModal, toggleGenerateModal, projectI
onChange={handleTmsUrlChange}
/>
</CoreModules.FormControl>
{error.includes('tmsUrl') && <p className="fmtm-text-sm fmtm-text-red-500">Tile Source is Required.</p>}
</CoreModules.Grid>
)}
<CoreModules.Grid item xs={12} sm={12} md={selectedTileSource === 'tms' ? 12 : 4}>
Expand All @@ -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
</CoreModules.LoadingButton>
Expand Down

0 comments on commit 1663e79

Please sign in to comment.