From 9a11845cf2027b482d53ee457d8d913f0156e913 Mon Sep 17 00:00:00 2001 From: Matt Stone Date: Wed, 19 Oct 2022 14:14:02 -0500 Subject: [PATCH] Move boundary details query inside When the query was outside of the component, it would cause re-render of the map each time the query data was updated. --- src/app/src/components/DrawTools/DrawTools.js | 47 +++++++++++++++- src/app/src/pages/Draw.js | 53 ++----------------- 2 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/app/src/components/DrawTools/DrawTools.js b/src/app/src/components/DrawTools/DrawTools.js index 64131a99..ead6ee54 100644 --- a/src/app/src/components/DrawTools/DrawTools.js +++ b/src/app/src/components/DrawTools/DrawTools.js @@ -1,5 +1,5 @@ import { useEffect } from 'react'; -import { useDispatch } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { Button, Icon } from '@chakra-ui/react'; import { ArrowRightIcon } from '@heroicons/react/outline'; @@ -7,14 +7,45 @@ import { ArrowRightIcon } from '@heroicons/react/outline'; import EditToolbar from './EditToolbar'; import MapControlButtons from './MapControlButtons'; +import { useGetBoundaryDetailsQuery } from '../../api/boundaries'; +import { useBoundaryId, useEndpointToastError } from '../../hooks'; + import useAddPolygonCursor from './useAddPolygonCursor'; import useEditingPolygon from './useEditingPolygon'; import useGeocoderResult from './useGeocoderResult'; import useTrackMapZoom from './useTrackMapZoom'; import { setPolygon } from '../../store/mapSlice'; +import { BOUNDARY_STATUS, ROLES } from '../../constants'; +import LoadingModal from '../LoadingModal'; + +const DRAW_MODES = { + FULLY_EDITABLE: 'fully_editable', + ANNOTATIONS_ONLY: 'annotations_only', + READ_ONLY: 'read_only', +}; + +export default function LoadBoundaryDetails() { + const user = useSelector(state => state.auth.user); + const id = useBoundaryId(); + + const { isFetching, data: details, error } = useGetBoundaryDetailsQuery(id); + useEndpointToastError(error); + + if (isFetching) { + return ; + } + + if (error || typeof details !== 'object') { + return null; + } + + const mode = getDrawMode({ status: details.status, userRole: user.role }); -export default function DrawTools({ details }) { + return ; +} + +function DrawTools({ mode, details }) { const dispatch = useDispatch(); // Add the polygon indicated by `details` to the state @@ -45,6 +76,18 @@ export default function DrawTools({ details }) { ); } +function getDrawMode({ status, userRole }) { + if (userRole === ROLES.VALIDATOR && status === BOUNDARY_STATUS.IN_REVIEW) { + return DRAW_MODES.ANNOTATIONS_ONLY; + } + + if (status === BOUNDARY_STATUS.DRAFT && userRole === ROLES.CONTRIBUTOR) { + return DRAW_MODES.FULLY_EDITABLE; + } + + return DRAW_MODES.READ_ONLY; +} + function SaveAndBackButton() { return (