From 18cb9df6ab7410e5893917cd3065a005cb13ee53 Mon Sep 17 00:00:00 2001 From: himeshr Date: Fri, 31 May 2024 12:30:18 +0530 Subject: [PATCH] avniproject/avni-product#1531 | WIP: Code changes to correctly map /web/locationHierarchies response --- src/upload/Dashboard.js | 41 ++++++++++++++++++++------------- src/upload/LocationHierarchy.js | 8 +------ src/upload/api.js | 4 ++-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/upload/Dashboard.js b/src/upload/Dashboard.js index b22ca724f..ac8449000 100644 --- a/src/upload/Dashboard.js +++ b/src/upload/Dashboard.js @@ -55,6 +55,7 @@ const Dashboard = ({ getStatuses, getUploadTypes, uploadTypes = new UploadTypes( } setFile(); setUploadType(""); + setEntityForDownload(""); setTimeout(() => { getStatuses(0); }, 1000); @@ -69,12 +70,14 @@ const Dashboard = ({ getStatuses, getUploadTypes, uploadTypes = new UploadTypes( ) { await api.downloadDynamicSample(uploadType); } - setEntityForDownload(""); }; React.useEffect(() => { getUploadTypes(); - api.fetchLocationHierarchies().then(json => setConfiguredHierarchies(JSON.parse(json))); + api.fetchLocationHierarchies().then(locHierarchies => { + setHierarchy(get(locHierarchies[0], "value")); + setConfiguredHierarchies(locHierarchies); + }); }, []); const uploadAndDownloadOptions = () => @@ -87,6 +90,11 @@ const Dashboard = ({ getStatuses, getUploadTypes, uploadTypes = new UploadTypes( option !== staticTypesWithStaticDownload.getName("metadataZip") && setEntityForDownload(option); }; + const isSampleDownloadDisallowed = + isEmpty(entityForDownload) || + (uploadType === "Locations" && isEmpty(mode)) || + (uploadType === "Locations" && mode === "relaxed" && isEmpty(hierarchy)); + return ( @@ -98,16 +106,16 @@ const Dashboard = ({ getStatuses, getUploadTypes, uploadTypes = new UploadTypes( Upload </Grid> <Grid container item> - <Grid container item direction="column" justifyContent="center" alignItems="flex-start" xs={12} sm={6} spacing={2}> + <Grid container item direction="column" justifyContent="center" alignItems="flex-start" xs={8} sm={4} spacing={2}> <DropDown name="Type" value={uploadType} onChange={dropdownHandler} options={uploadAndDownloadOptions()} /> <Tooltip title="Download Sample file for selected Upload type" placement="bottom-start" arrow> - <Button color="primary" onClick={downloadSampleFile} disabled={isEmpty(entityForDownload)}> - <CloudDownload disabled={isEmpty(entityForDownload)} /> + <Button color="primary" onClick={downloadSampleFile} disabled={isSampleDownloadDisallowed}> + <CloudDownload disabled={isSampleDownloadDisallowed} /> <span style={{ marginLeft: "1em" }}>Download Sample</span> </Button> </Tooltip> </Grid> - <Grid container item direction="column" justifyContent="center" alignItems="flex-start" xs={12} sm={6} spacing={2}> + <Grid container item direction="column" justifyContent="center" alignItems="flex-start" xs={8} sm={4} spacing={2}> <Grid item> <FileUpload canSelect={!isEmpty(uploadType)} canUpload={!isNil(file)} onSelect={selectFile} onUpload={uploadFile} /> </Grid> @@ -137,16 +145,17 @@ const Dashboard = ({ getStatuses, getUploadTypes, uploadTypes = new UploadTypes( {uploadType === "Locations" && <LocationModes mode={mode} setMode={setMode} />} </Grid> <Grid container item> - {uploadType === "Locations" && mode === "relaxed" && configuredHierarchies && configuredHierarchies.length > 0 && ( - <LocationHierarchy hierarchy={hierarchy} setHierarchy={setHierarchy} configuredHierarchies={configuredHierarchies} /> - )} - {uploadType === "Locations" && mode === "relaxed" && (!configuredHierarchies || configuredHierarchies.length === 0) && ( - <Box> - <Typography color="error" display="block" gutterBottom> - Invalid or missing Location Hierarchy. - </Typography> - </Box> - )} + {uploadType === "Locations" && + mode === "relaxed" && + (configuredHierarchies && configuredHierarchies.length > 0 ? ( + <LocationHierarchy hierarchy={hierarchy} setHierarchy={setHierarchy} configuredHierarchies={configuredHierarchies} /> + ) : ( + <Box> + <Typography color="error" display="block" gutterBottom> + Invalid or missing Location Hierarchy. + </Typography> + </Box> + ))} </Grid> </Grid> </DocumentationContainer> diff --git a/src/upload/LocationHierarchy.js b/src/upload/LocationHierarchy.js index 270786625..c27a945cd 100644 --- a/src/upload/LocationHierarchy.js +++ b/src/upload/LocationHierarchy.js @@ -4,7 +4,6 @@ import FormLabel from "@material-ui/core/FormLabel"; import RadioGroup from "@material-ui/core/RadioGroup"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import Radio from "@material-ui/core/Radio"; -import _ from "lodash"; export const LocationHierarchy = ({ hierarchy, setHierarchy, configuredHierarchies }) => { const handleChange = event => { @@ -14,12 +13,7 @@ export const LocationHierarchy = ({ hierarchy, setHierarchy, configuredHierarchi return ( <FormControl component="fieldset" style={{ marginTop: "20px" }}> <FormLabel component="legend">Select Location Hierarchy</FormLabel> - <RadioGroup - aria-label="hierarchy" - name="hierarchy1" - value={hierarchy || _.get(configuredHierarchies[0], "value")} - onChange={handleChange} - > + <RadioGroup aria-label="hierarchy" name="hierarchy1" value={hierarchy} onChange={handleChange}> {configuredHierarchies.map(hierarchicalValue => ( <FormControlLabel disabled={configuredHierarchies.length <= 1} diff --git a/src/upload/api.js b/src/upload/api.js index f5f888956..fe9975786 100644 --- a/src/upload/api.js +++ b/src/upload/api.js @@ -18,8 +18,8 @@ export default { fetchLocationHierarchies: () => { return http .fetchJson(http.withParams("/web/locationHierarchies")) - .then(r => r.json) - .catch(r => "[]"); + .then(response => Object.entries(response.json).map(([value, label]) => ({ value, label }))) + .catch(() => []); }, async downloadSample(type) { const file = await fetch(`/bulkuploads/sample/${type}.csv`);