From 09091434ce3a13191085596d14ba62c47f9036b9 Mon Sep 17 00:00:00 2001 From: Julia Lavrova Date: Mon, 7 Dec 2020 20:43:30 +0300 Subject: [PATCH] Fix problem #188 and #160 --- src/components/Map/index.tsx | 50 +++++++++++++++++++++++------------- src/interfaces/Map.ts | 4 +-- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/components/Map/index.tsx b/src/components/Map/index.tsx index 92d94a4a..82c6686e 100644 --- a/src/components/Map/index.tsx +++ b/src/components/Map/index.tsx @@ -31,21 +31,33 @@ const ACCESS_TOKEN = 'pk.eyJ1IjoieXVsaWEtYXZkZWV2YSIsImEiOiJjazh0enUyOGEwNTR1M29 const PAGES_RESOURCE = '/_api/heritage/?action=search&format=json&limit=5000&srcountry=ru&&props=id|name|address|municipality|lat|lon|image|source&bbox='; const MIN_ZOOM_LEVEL = 0; -class MyMap extends Component { - state: MyMapParams = { - viewport: { - latitude: 55.7522, - longitude: 37.6155, - zoom: 10, - bearing: 0, - pitch: 0, - width: undefined, - height: undefined, - }, - searchValue: '', - monuments: [], - loading: false, - }; +class MyMap extends Component { + constructor(props: MapPropsInterface) { + super(props); + + let prevPosition: { lat?: string, lon?: string } = {}; + + try { + prevPosition = JSON.parse(window.localStorage.getItem('viewport') || ''); + } catch (err) { + console.log(err); + } + + this.state = { + viewport: { + latitude: prevPosition?.lat || 55.7522, + longitude: prevPosition?.lon || 37.6155, + zoom: 10, + bearing: 0, + pitch: 0, + width: undefined, + height: undefined, + }, + searchValue: '', + monuments: [], + loading: false, + }; + } abortController: { abort: () => void; signal: any } | undefined = undefined; @@ -92,20 +104,22 @@ class MyMap extends Component { const width = document.body.offsetWidth; const height = document.body.offsetHeight; const { longitude, latitude, zoom } = viewport; + const maxZoom = Math.max(MIN_ZOOM_LEVEL, Number(zoom)); + const bbox = getBbox({ longitude, latitude, width, height, - zoom: Math.max(MIN_ZOOM_LEVEL, Number(zoom)), + zoom: maxZoom, }); - // eslint-disable-next-line react/no-unused-state - this.setState({ viewport, zoom: Math.max(MIN_ZOOM_LEVEL, zoom) }); + this.setState((prevState) => ({ viewport: { ...prevState.viewport, zoom: maxZoom } })); window.localStorage.setItem('viewport', JSON.stringify(viewport)); this.loadPointsWithDebounce(bbox); + this.props.history.push(getRoute({ lat: latitude, lon: longitude })); }; handleMapLoad = () => { diff --git a/src/interfaces/Map.ts b/src/interfaces/Map.ts index 3c85c536..acfd6e0a 100644 --- a/src/interfaces/Map.ts +++ b/src/interfaces/Map.ts @@ -1,8 +1,8 @@ import { AlertManager } from 'react-alert'; export interface ViewportInterface { - latitude?: number, - longitude?: number, + latitude?: number | string, + longitude?: number | string, zoom?: number, bearing?: number, pitch?: number,