Skip to content

Commit

Permalink
Fix problem #188 and #160
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia-Lavrova committed Dec 7, 2020
1 parent 253c824 commit 0909143
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
50 changes: 32 additions & 18 deletions src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapPropsInterface> {
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<MapPropsInterface, MyMapParams> {
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;

Expand Down Expand Up @@ -92,20 +104,22 @@ class MyMap extends Component<MapPropsInterface> {
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 = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Map.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 0909143

Please sign in to comment.