Skip to content

Commit

Permalink
create location selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaya committed Jun 29, 2020
1 parent 88b464d commit eb54036
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
19 changes: 11 additions & 8 deletions src/components/RunMap/RunMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { DragEndEvent, Icon, LeafletMouseEvent } from 'leaflet';
import { useTheme } from '@material-ui/core';
import Polyline from 'react-leaflet-arrowheads';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import 'leaflet/dist/leaflet.css';

Expand All @@ -18,11 +18,13 @@ import {
} from '../../state/location';
import { LatLng } from '../../server/types';
import { safeStoredLocation, storeLocation } from '../../utils/localStorage';
import { flippedSelector, routeSelector } from '../../store/route/selectors';
import { flippedSelector, locationSelector, routeSelector } from '../../store/route/selectors';

import iconUrl from './Marker.png';

import './RunMap.css';
import { findLocationByLatLng } from '../../store/location/actions';
import { locationByKeySelector } from '../../store/location/selectors';

const MarkerIcon = new Icon({
iconUrl,
Expand All @@ -32,11 +34,13 @@ const MarkerIcon = new Icon({

const RunMap: React.FC = () => {
const theme = useTheme();
const dispatch = useDispatch();

const route = useSelector(routeSelector);
const location = useSelector(locationSelector);
const flipped = useSelector(flippedSelector);

const startLocation = useRecoilValueLoadable(locationByRouteLocation);
const startLocation = useSelector(locationByKeySelector(location));

const defaultCenter: LatLng = safeStoredLocation()?.coordinates || [52.132633, 5.291266];

Expand All @@ -57,12 +61,12 @@ const RunMap: React.FC = () => {
const bounds = coordinates.length > 0 ? coordinates : undefined;

const handleClick = ({ latlng }: LeafletMouseEvent): void => {
// setLocationSearch([latlng.lat, latlng.lng]);
dispatch(findLocationByLatLng([latlng.lat, latlng.lng]));
};

const onDragend = (e: DragEndEvent): void => {
const latLng = e.target.getLatLng();
// setLocationSearch([latLng.lat, latLng.lng]);
dispatch(findLocationByLatLng(latLng));
};

return (
Expand All @@ -77,11 +81,10 @@ const RunMap: React.FC = () => {
attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{startLocation.state === 'hasValue'
&& startLocation.contents?.coordinates
{startLocation
&& (
<Marker
position={startLocation.contents?.coordinates}
position={startLocation.coordinates}
icon={MarkerIcon}
title="Starting Point"
alt="Starting Point"
Expand Down
16 changes: 1 addition & 15 deletions src/state/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ export const locationSearchState = atom<string | LatLng>({
default: safeStoredLocation()?.name || '',
});

type onCompleteLocation = (locations: LocationResponse[]) => void;

let onCompleteLocation: onCompleteLocation = (): void => undefined;

export function setOnCompleteLocation(newOnCompleteLocation: onCompleteLocation): void {
onCompleteLocation = newOnCompleteLocation;
}

export const locationsState = atom<Locations>({
key: 'Locations',
default: {},
Expand Down Expand Up @@ -55,13 +47,7 @@ export const locationsDataQuery = selectorFamily<LocationResponse[], string | La

return fetch(url)
.then((res) => res.json() as Promise<LocationsResponse>)
.then((res) => {
if (isLatLng) {
onCompleteLocation(res.locations);
}

return res;
})
.then((res) => res)
.then((res) => res.locations)
.catch(() => {
alertError('Something went from looking for a location.');
Expand Down
6 changes: 6 additions & 0 deletions src/store/location/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createSelector } from 'reselect';

import { LocationState, StoreState } from '../types';
import { LocationResponse } from '../../server/types';

const locationState = (state: StoreState): LocationState => state.location;

export const searchSelector = createSelector(locationState, (state) => state.search);
export const locationsSelector = createSelector(locationState, (state) => state.locations);
export const locationsStateSelector = createSelector(locationState, (state) => state.state);
export const locationsBySearchSelector = createSelector(locationState, (state) => state.bySearch);

export const locationByKeySelector = (key: string | undefined) => createSelector(
locationsSelector,
(state): LocationResponse | undefined => (key ? state[key] : undefined),
);
2 changes: 1 addition & 1 deletion src/store/route/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getRouteValueSelector = createSelector(routeSelector, getValue);

const distanceSelector = createSelector(routeState, (state) => state.distance);
const routeTypeSelector = createSelector(routeState, (state) => state.routeType);
const locationSelector = createSelector(routeState, (state) => state.location);
export const locationSelector = createSelector(routeState, (state) => state.location);
const randomSeedSelector = createSelector(routeState, (state) => state.randomSeed);
export const flippedSelector = createSelector(routeState, (state) => state.flipped);

Expand Down

0 comments on commit eb54036

Please sign in to comment.