From 1d45e65434cf3ea0318a3267d1547c711f4e0efc Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Tue, 11 Feb 2020 11:04:57 +0700 Subject: [PATCH] replacing gpx tracks --- src/components/dialogs/GpxDialog.tsx | 43 ++++++++++-------- src/components/gpx/GpxConfirm.tsx | 21 +++++++++ src/components/gpx/GpxDialogRow.tsx | 68 +++++++++++++++------------- src/redux/editor/sagas.ts | 2 +- src/utils/simplify.ts | 8 ++-- 5 files changed, 86 insertions(+), 56 deletions(-) create mode 100644 src/components/gpx/GpxConfirm.tsx diff --git a/src/components/dialogs/GpxDialog.tsx b/src/components/dialogs/GpxDialog.tsx index 3b080db..7940fbd 100644 --- a/src/components/dialogs/GpxDialog.tsx +++ b/src/components/dialogs/GpxDialog.tsx @@ -1,9 +1,9 @@ import React, { FC, useCallback, ChangeEvent } from 'react'; import { connect } from 'react-redux'; -import * as EDITOR_ACTIONS from '~/redux/editor/actions'; import { IState } from '~/redux/store'; import { selectEditorGpx } from '~/redux/editor/selectors'; import { GpxDialogRow } from '~/components/gpx/GpxDialogRow'; +import { GpxConfirm } from '~/components/gpx/GpxConfirm'; import { MainMap } from '~/constants/map'; import { latLngBounds } from 'leaflet'; import { Switch } from '../Switch'; @@ -13,6 +13,10 @@ import uuid from 'uuid'; import { getUrlData } from '~/utils/history'; import { getRandomColor } from '~/utils/dom'; +import * as EDITOR_ACTIONS from '~/redux/editor/actions'; +import * as MAP_ACTIONS from '~/redux/map/actions'; +import { simplify } from '~/utils/simplify'; + const mapStateToProps = (state: IState) => ({ gpx: selectEditorGpx(state), route: selectMapRoute(state), @@ -25,6 +29,7 @@ const mapDispatchToProps = { editorUploadGpx: EDITOR_ACTIONS.editorUploadGpx, editorSetGpx: EDITOR_ACTIONS.editorSetGpx, editorGetGPXTrack: EDITOR_ACTIONS.editorGetGPXTrack, + mapSetRoute: MAP_ACTIONS.mapSetRoute, }; type Props = ReturnType & typeof mapDispatchToProps & {}; @@ -37,7 +42,12 @@ const GpxDialogUnconnected: FC = ({ editorGetGPXTrack, editorSetGpx, editorUploadGpx, + mapSetRoute, }) => { + const toggleGpx = useCallback(() => { + editorSetGpx({ enabled: !gpx.enabled }); + }, [gpx, editorSetGpx]); + const onGpxUpload = useCallback( (event: ChangeEvent) => { if (!event.target || !event.target.files || event.target.files.length === 0) { @@ -76,13 +86,10 @@ const GpxDialogUnconnected: FC = ({ [gpx, editorSetGpx] ); - const toggleGpx = useCallback(() => { - editorSetGpx({ enabled: !gpx.enabled }); - }, [gpx, editorSetGpx]); - const onRouteToggle = useCallback( index => { if (!gpx.enabled) return; + editorSetGpx({ list: gpx.list.map((el, i) => (i !== index ? el : { ...el, enabled: !el.enabled })), }); @@ -109,22 +116,19 @@ const GpxDialogUnconnected: FC = ({ }); }, [route, gpx, editorSetGpx]); + const onRouteReplace = useCallback( + (i: number) => { + mapSetRoute(simplify(gpx.list[i].latlngs)); + + editorSetGpx({ + list: gpx.list.map((el, index) => (i !== index ? el : { ...el, enabled: false })), + }); + }, + [gpx, mapSetRoute, editorSetGpx] + ); + return (
- {false && ( -
-
Маршрут уже нанесен. Что делаем?
- -
-
Соединить
- -
Переписать
- -
Отмена
-
-
- )} -
Треки
@@ -140,6 +144,7 @@ const GpxDialogUnconnected: FC = ({ onFocusRoute={onFocusRoute} onRouteToggle={onRouteToggle} onRouteColor={onRouteColor} + onRouteReplace={onRouteReplace} /> ))} diff --git a/src/components/gpx/GpxConfirm.tsx b/src/components/gpx/GpxConfirm.tsx new file mode 100644 index 0000000..d3eec4f --- /dev/null +++ b/src/components/gpx/GpxConfirm.tsx @@ -0,0 +1,21 @@ +import React, { FC } from 'react'; + +interface IProps {} + +const GpxConfirm: FC = ({}) => { + return ( +
+
Маршрут уже нанесен. Что делаем?
+ +
+
Соединить
+ +
Переписать
+ +
Отмена
+
+
+ ); +}; + +export { GpxConfirm }; diff --git a/src/components/gpx/GpxDialogRow.tsx b/src/components/gpx/GpxDialogRow.tsx index 8f7ed3d..e2f5078 100644 --- a/src/components/gpx/GpxDialogRow.tsx +++ b/src/components/gpx/GpxDialogRow.tsx @@ -1,4 +1,4 @@ -import React, { FC } from 'react'; +import React, { FC, memo } from 'react'; import { IGpxRoute } from '~/redux/editor'; import { Switch } from '../Switch'; import { Icon } from '../panels/Icon'; @@ -13,44 +13,48 @@ interface IProps { onRouteDrop: (i: number) => void; onRouteToggle: (i: number) => void; onRouteColor: (i: number) => void; + onRouteReplace: (i: number) => void; } -const GpxDialogRow: FC = ({ - item, - index, - enabled, - onRouteToggle, - onFocusRoute, - onRouteDrop, - onRouteColor, -}) => { - return ( -
-
onRouteColor(index)} - /> +const GpxDialogRow: FC = memo( + ({ + item, + index, + enabled, + onRouteToggle, + onFocusRoute, + onRouteDrop, + onRouteColor, + onRouteReplace, + }) => { + return ( +
+
onRouteColor(index)} + /> -
onFocusRoute(index)}> - {item.name} -
+
onFocusRoute(index)}> + {item.name} +
-
- {false && ( -
onRouteDrop(index)}> +
+
onRouteReplace(index)}>
- )} -
onRouteDrop(index)}> - -
-
- onRouteToggle(index)} /> + +
onRouteDrop(index)}> + +
+ +
+ onRouteToggle(index)} /> +
-
- ); -}; + ); + } +); export { GpxDialogRow }; diff --git a/src/redux/editor/sagas.ts b/src/redux/editor/sagas.ts index 2c85766..057f42d 100644 --- a/src/redux/editor/sagas.ts +++ b/src/redux/editor/sagas.ts @@ -294,7 +294,7 @@ function* routerSubmit() { const route: ReturnType = yield select(selectMapRoute); const latlngs: LatLng[] = path(['_routes', 0, 'coordinates'], OsrmRouter); - const coordinates = simplify({ map: MainMap, latlngs }); + const coordinates = simplify(latlngs); yield put(mapSetRoute([...route, ...coordinates])); OsrmRouter.setWaypoints([]); diff --git a/src/utils/simplify.ts b/src/utils/simplify.ts index 0e7616e..4c20705 100644 --- a/src/utils/simplify.ts +++ b/src/utils/simplify.ts @@ -1,9 +1,9 @@ import { Map, LineUtil, LatLng } from 'leaflet'; -// import { ILatLng } from "~/redux/map/types"; +import { MainMap } from '~/constants/map'; -export const simplify = ({ map, latlngs }: { map: Map, latlngs: LatLng[] }): LatLng[] => { +export const simplify = (latlngs: LatLng[]): LatLng[] => { const zoom = 12; const mul = 0.7; // 0 - not simplifying, 1 - very rude. - const points = latlngs.map(({ lat, lng }) => map.project({ lat, lng }, zoom)); - return LineUtil.simplify(points, mul).map(item => map.unproject(item, zoom)); + const points = latlngs.map(({ lat, lng }) => MainMap.project({ lat, lng }, zoom)); + return LineUtil.simplify(points, mul).map(item => MainMap.unproject(item, zoom)); };