From 3363dffe89970236d182bf3ae1666b79de4f3a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20V=C3=A1clav=C3=ADk?= Date: Mon, 9 Dec 2024 07:31:29 +0100 Subject: [PATCH] Directions: Move from/to to array of points --- .../Directions/DirectionsAutocomplete.tsx | 44 +++--- src/components/Directions/DirectionsBox.tsx | 3 +- .../Directions/DirectionsContext.tsx | 17 +-- src/components/Directions/DirectionsForm.tsx | 138 +++++++++++++----- src/components/Directions/ModeToggler.tsx | 6 +- src/components/Directions/Result.tsx | 9 +- src/components/Directions/useGetOnSubmit.tsx | 36 +++-- .../RouteList/ClimbingRouteTableRow.tsx | 1 + .../FeaturePanel/Climbing/utils/array.ts | 7 + .../SearchBox/renderOptionFactory.tsx | 1 - src/helpers/GlobalStyle.tsx | 4 + 11 files changed, 175 insertions(+), 91 deletions(-) diff --git a/src/components/Directions/DirectionsAutocomplete.tsx b/src/components/Directions/DirectionsAutocomplete.tsx index 5522d05bd..46e4effe6 100644 --- a/src/components/Directions/DirectionsAutocomplete.tsx +++ b/src/components/Directions/DirectionsAutocomplete.tsx @@ -14,6 +14,7 @@ import { InputAdornment, TextField, Tooltip, + useTheme, } from '@mui/material'; import { useMapCenter } from '../SearchBox/utils'; import { useUserThemeContext } from '../../helpers/theme'; @@ -30,8 +31,9 @@ import { AlphabeticalMarker } from './TextMarker'; import MyLocationIcon from '@mui/icons-material/MyLocation'; import { DotLoader, useIsClient } from '../helpers'; import { useSnackbar } from '../utils/SnackbarContext'; -import { useGetOnSubmitFactory } from './useGetOnSubmit'; +import { useGetOnSubmitFactory, useUpdatePoint } from './useGetOnSubmit'; import { useDirectionsContext } from './DirectionsContext'; +import { removeElementOnIndex } from '../FeaturePanel/Climbing/utils/array'; const DotLoaderContainer = styled.div` font-size: 16px; right: 6px; @@ -60,6 +62,7 @@ const DirectionsInput = ({ const { InputLabelProps, InputProps, ...restParams } = params; const isClient = useIsClient(); const { showToast } = useSnackbar(); + const theme = useTheme(); useEffect(() => { // @ts-ignore @@ -171,15 +174,16 @@ const Row = styled.div` `; const useInputMapClickOverride = ( - setValue: (value: Option) => void, + pointIndex: number, setInputValue: (value: string) => void, selectedOptionInputValue: React.MutableRefObject, ) => { const { mapClickOverrideRef } = useMapStateContext(); const previousBehaviourRef = useRef(); + const updatePoint = useUpdatePoint(); const mapClickCallback = (coords: LonLat, label: string) => { - setValue(getCoordsOption(coords, label)); + updatePoint(pointIndex, getCoordsOption(coords, label)); setInputValue(label); selectedOptionInputValue.current = label; @@ -203,16 +207,10 @@ const useInputMapClickOverride = ( type Props = { label: string; value: Option; - setValue: (value: Option) => void; pointIndex: number; }; -export const DirectionsAutocomplete = ({ - label, - value, - setValue, - pointIndex, -}: Props) => { +export const DirectionsAutocomplete = ({ label, value, pointIndex }: Props) => { const autocompleteRef = useRef(); const { inputValue, setInputValue } = useInputValueState(); const selectedOptionInputValue = useRef(null); @@ -220,6 +218,7 @@ export const DirectionsAutocomplete = ({ const { currentTheme } = useUserThemeContext(); const { userSettings } = useUserSettingsContext(); const { isImperial } = userSettings; + const updatePoint = useUpdatePoint(); const ALPHABETICAL_MARKER = useMemo(() => { let svgElement; @@ -239,7 +238,9 @@ export const DirectionsAutocomplete = ({ }, [pointIndex]); const markerRef = useRef(); - const { from, to, mode, setResult, setLoading } = useDirectionsContext(); + const { points, mode, setResult, setLoading, setPoints } = + useDirectionsContext(); + const submitFactory = useGetOnSubmitFactory(setResult, setLoading); useEffect(() => { const map = getGlobalMap(); @@ -254,20 +255,15 @@ export const DirectionsAutocomplete = ({ }, [ALPHABETICAL_MARKER, value]); const handleUpdate = (coordsOption: Option) => { - if (pointIndex === 0) { - submitFactory(coordsOption, to, mode); - } - if (pointIndex === 1) { - submitFactory(from, coordsOption, mode); - } + const newPoints = updatePoint(pointIndex, coordsOption); + submitFactory(newPoints, mode); }; - const submitFactory = useGetOnSubmitFactory(setResult, setLoading); const onDragEnd = () => { const lngLat = markerRef.current?.getLngLat(); if (lngLat) { const coordsOption = getCoordsOption([lngLat.lng, lngLat.lat]); - setValue(coordsOption); + updatePoint(pointIndex, coordsOption); handleUpdate(coordsOption); } }; @@ -279,13 +275,14 @@ export const DirectionsAutocomplete = ({ const onChange = (_: unknown, option: Option) => { console.log('selected', option); // eslint-disable-line no-console setInputValue(getOptionLabel(option)); - setValue(option); + updatePoint(pointIndex, option); + selectedOptionInputValue.current = getOptionLabel(option); handleUpdate(option); }; const { onInputFocus, onInputBlur } = useInputMapClickOverride( - setValue, + pointIndex, setInputValue, selectedOptionInputValue, ); @@ -296,8 +293,8 @@ export const DirectionsAutocomplete = ({ if (selectedOptionInputValue.current !== inputValue) { if (options.length > 0 && inputValue) { onChange(null, options[0]); - } else { - setValue(null); + } else if (points) { + setPoints(removeElementOnIndex(points, pointIndex)); } } }; @@ -321,6 +318,7 @@ export const DirectionsAutocomplete = ({ getOptionKey={(option) => JSON.stringify(option)} onChange={onChange} autoComplete + noOptionsText="" disableClearable autoHighlight clearOnEscape diff --git a/src/components/Directions/DirectionsBox.tsx b/src/components/Directions/DirectionsBox.tsx index bf1612103..6a605545c 100644 --- a/src/components/Directions/DirectionsBox.tsx +++ b/src/components/Directions/DirectionsBox.tsx @@ -15,9 +15,10 @@ const Wrapper = styled(Stack)<{ $isMobileMode: boolean }>` right: 8px; z-index: 1001; // over the LayerSwitcherButton max-height: calc(100vh - 16px); + box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.12); @media ${isTabletResolution} { - max-width: 340px; + max-width: 394px; } `; diff --git a/src/components/Directions/DirectionsContext.tsx b/src/components/Directions/DirectionsContext.tsx index be6e91c43..2528d1796 100644 --- a/src/components/Directions/DirectionsContext.tsx +++ b/src/components/Directions/DirectionsContext.tsx @@ -2,19 +2,15 @@ import React, { createContext, useContext, useState } from 'react'; import { Profile, RoutingResult } from './routing/types'; import { Option } from '../SearchBox/types'; -type CragViewLayout = 'vertical' | 'horizontal' | 'auto'; - type DirectionsContextType = { loading: boolean; setLoading: (loading: boolean) => void; mode: Profile; setMode: (mode: Profile) => void; - from: Option; - setFrom: (from: Option) => void; - to: Option; - setTo: (to: Option) => void; result: RoutingResult; setResult: (result: RoutingResult) => void; + points: Array