Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show in Web Map toggle #1104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/components/GrowerDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ListItem,
ListItemAvatar,
ListItemText,
Switch,
Typography,
} from '@material-ui/core';
import {
Expand Down Expand Up @@ -148,7 +149,7 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
// log.debug('render: grower detail', growerId);
const classes = useStyle();
const appContext = useContext(AppContext);
const { growers } = useContext(GrowerContext);
const { growers, updateGrower } = useContext(GrowerContext);
const { sendMessageFromGrower } = useContext(MessagingContext);
const [editDialogOpen, setEditDialogOpen] = useState(false);
const [grower, setGrower] = useState({});
Expand All @@ -160,6 +161,9 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
const [isImageLoading, setIsImageLoading] = useState(true);
const [errorMessage, setErrorMessage] = useState(null);

// show in webmap
const [showInWebmap, setShowInWebmap] = useState(false);

function formatDevices(grower) {
// deduplicate and format
const devices = grower?.devices?.reduce((result, device) => {
Expand All @@ -182,6 +186,7 @@ const GrowerDetail = ({ open, growerId, onClose }) => {

useEffect(() => {
setErrorMessage(null);

async function loadGrowerDetail() {
if (grower && grower.grower_account_id !== growerId) {
setGrower({});
Expand All @@ -194,13 +199,15 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
}

setGrower(match);
setShowInWebmap(match.show_in_map);

if (match?.devices?.length) {
const devices = formatDevices(match);
setDeviceIdentifiers(devices);
}
}
}

loadGrowerDetail();
// eslint-disable-next-line
}, [growerId, growers]);
Expand All @@ -227,6 +234,7 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
setLoading(false);
}
}

loadCaptures();
}, [grower]);

Expand Down Expand Up @@ -270,6 +278,23 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
setSnackbarLabel(label);
setSnackbarOpen(true);
}

async function handleSwitchChange(e) {
const toggle = e.target.checked;
setShowInWebmap(toggle);

try {
const updatedGrower = {
id: grower.id,
show_in_map: toggle,
};
await updateGrower(updatedGrower);
} catch (error) {
setErrorMessage('Error updating grower detail');
setShowInWebmap(!toggle);
}
}

return (
<>
<Drawer
Expand Down Expand Up @@ -407,6 +432,19 @@ const GrowerDetail = ({ open, growerId, onClose }) => {
</Grid>
)}
<Divider />
<Grid
container
direction="row"
className={classes.box}
style={{
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Typography variant="subtitle1">Show in Web Map</Typography>
<Switch checked={showInWebmap} onChange={handleSwitchChange} />
</Grid>
<Divider />
<Grid container direction="column" className={classes.box}>
<Typography variant="subtitle1" className={classes.captures}>
Captures
Expand Down
1 change: 1 addition & 0 deletions src/components/OptimizedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function OptimizedImage(props) {
src={cdnUrl || src}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
currentTarget.srcset = null;
currentTarget.src = null;
}}
alt=".."
Expand Down
Loading