Skip to content

Commit

Permalink
enforcing location fields
Browse files Browse the repository at this point in the history
  • Loading branch information
erasta committed Nov 12, 2024
1 parent a9147d8 commit 6f0f558
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
18 changes: 15 additions & 3 deletions client/src/IO/UploadDevices/UploadDevicesFieldsDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { groupBy } from "lodash";
import { UploadDevicesTypeFieldsMatcher } from "./UploadDevicesTypeFieldsMatcher";
import { useState } from "react";
import { changeDeviceOnTrial } from "./changeDeviceOnTrial";
import { FIELD_UNASSIGNED, LOCATION_FIELDS } from "./uploadDefs";

export const UploadDevicesFieldsDialog = ({ devicesToUpload, setDevicesToUpload, data, setData, experiment }) => {

Expand All @@ -19,8 +20,14 @@ export const UploadDevicesFieldsDialog = ({ devicesToUpload, setDevicesToUpload,

const devicesByType = Object.values(groupBy(devicesToUpload, x => x[1]));

/// TODO: attach fields from csv to device attributes for each device type
// nice to have: allow changing the csv fields for name, type, coords etc..
let disabled = false;
for (const a of Object.values(attrMatch)) {
for (const f of LOCATION_FIELDS) {
if ((a[f] || FIELD_UNASSIGNED) === FIELD_UNASSIGNED) {
disabled = true;
}
}
}

return (
<Dialog
Expand Down Expand Up @@ -51,7 +58,12 @@ export const UploadDevicesFieldsDialog = ({ devicesToUpload, setDevicesToUpload,
)
})}
<DialogActions>
<Button onClick={uploadTrial}>Upload</Button>
<Button
onClick={uploadTrial}
disabled={disabled}
>
Upload
</Button>
</DialogActions>
</Dialog>
)
Expand Down
44 changes: 26 additions & 18 deletions client/src/IO/UploadDevices/UploadDevicesTypeFieldsMatcher.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Grid, Stack, Typography } from "@mui/material"
import { SelectProperty } from "../../Property/SelectProperty";
import { useEffect } from "react";
import { FIELD_LATITUDE, FIELD_LONGITUDE, FIELD_MAPNAME, FIELD_UNASSIGNED, IGNORE_FIELDS } from "./uploadDefs";
import { FIELD_UNASSIGNED, IGNORE_FIELDS, LOCATION_FIELDS } from "./uploadDefs";

function uniq(list) {
return list.reduce((acc, d) => acc.includes(d) ? acc : acc.concat(d), []);
Expand All @@ -12,9 +12,8 @@ export const UploadDevicesTypeFieldsMatcher = ({ devicesDetails, deviceType, att
return Object.keys(x.attributes).filter(f => !IGNORE_FIELDS.includes(f));
}));

// TODO: enforce location fields
const attributeTypeNames = deviceType?.attributeTypes?.map(x => x.name) || [];
attributeTypeNames.unshift(FIELD_LATITUDE, FIELD_LONGITUDE, FIELD_MAPNAME);
attributeTypeNames.unshift(...LOCATION_FIELDS);

useEffect(() => {
const matches = {};
Expand All @@ -29,22 +28,31 @@ export const UploadDevicesTypeFieldsMatcher = ({ devicesDetails, deviceType, att

return (
<Stack direction='column' spacing={1} sx={{ margin: 1 }}>
{attributeTypeNames.map((attrName, i) => (
<Grid container direction='row' key={i}>
<Grid item xs={3} alignSelf={'center'}>
<Typography key={'a' + i}>{attrName}</Typography>
{attributeTypeNames.map((attrName, i) => {
const oneMatch = attrMatch[attrName] || FIELD_UNASSIGNED;
const locationUnassigned = oneMatch === FIELD_UNASSIGNED && LOCATION_FIELDS.includes(attrName);
return (
<Grid container direction='row' key={i}>
<Grid item xs={3} alignSelf={'center'}>
<Typography key={'a' + i}>{attrName}</Typography>
</Grid>
<Grid item xs={4}>
<SelectProperty
styleFormControl={{ width: '100%' }}
label={attrName}
data={attrMatch[attrName] || FIELD_UNASSIGNED}
setData={v => setAttrMatch({ ...attrMatch, [attrName]: v })}
options={attrOptions}
/>
</Grid>
<Grid item xs={2} alignSelf={'center'} sx={{ margin: 1, color: 'red' }}>
{!locationUnassigned ? null :
<Typography>Assign location fields</Typography>
}
</Grid>
</Grid>
<Grid item xs={4}>
<SelectProperty
styleFormControl={{ width: '100%' }}
label={attrName}
data={attrMatch[attrName] || FIELD_UNASSIGNED}
setData={v => setAttrMatch({ ...attrMatch, [attrName]: v })}
options={attrOptions}
/>
</Grid>
</Grid>
))}
)
})}
</Stack>
)
}
1 change: 1 addition & 0 deletions client/src/IO/UploadDevices/uploadDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const FIELD_MAPNAME = 'MapName';
export const FIELD_LATITUDE = 'Latitude';
export const FIELD_LONGITUDE = 'Longitude';
export const IGNORE_FIELDS = [FIELD_TYPE, FIELD_TYPE];
export const LOCATION_FIELDS = [FIELD_LATITUDE, FIELD_LONGITUDE, FIELD_MAPNAME];

export interface DevicesFromFile {
type: string;
Expand Down

0 comments on commit 6f0f558

Please sign in to comment.