redux: router fixed

This commit is contained in:
muerwre 2018-11-26 15:50:36 +07:00
parent 5d2c534aa9
commit eb5b0e5d28
6 changed files with 98 additions and 39 deletions

View file

@ -8,6 +8,10 @@ import { LogoDialog } from '$components/logo/LogoDialog';
import { SaveDialog } from '$components/save/SaveDialog'; import { SaveDialog } from '$components/save/SaveDialog';
import { CancelDialog } from '$components/save/CancelDialog'; import { CancelDialog } from '$components/save/CancelDialog';
import type { UserType } from '$constants/types'; import type { UserType } from '$constants/types';
import { setLogo, routerCancel, routerSubmit } from '$redux/user/actions';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { editor } from '$modules/Editor';
type Props = { type Props = {
mode: String, mode: String,
@ -20,10 +24,15 @@ type Props = {
address: String, address: String,
setLogo: Function, setLogo: Function,
routerSubmit: Function,
routerCancel: Function,
} }
export const EditorDialog = ({
mode, routerPoints, editor, activeSticker, logo, user, title, address, setLogo export const Component = (props: Props) => {
}: Props) => { const {
mode, activeSticker, logo, user, title, address
} = props;
const showDialog = ( const showDialog = (
mode === MODES.ROUTER mode === MODES.ROUTER
|| (mode === MODES.STICKERS && !activeSticker) || (mode === MODES.STICKERS && !activeSticker)
@ -36,7 +45,7 @@ export const EditorDialog = ({
return ( return (
showDialog && showDialog &&
<div id="control-dialog"> <div id="control-dialog">
{ mode === MODES.ROUTER && <RouterDialog routerPoints={routerPoints} editor={editor} /> } { mode === MODES.ROUTER && <RouterDialog {...props} /> }
{ mode === MODES.STICKERS && <StickersDialog editor={editor} /> } { mode === MODES.STICKERS && <StickersDialog editor={editor} /> }
{ mode === MODES.TRASH && <TrashDialog editor={editor} /> } { mode === MODES.TRASH && <TrashDialog editor={editor} /> }
{ mode === MODES.LOGO && <LogoDialog editor={editor} logo={logo} setLogo={setLogo} /> } { mode === MODES.LOGO && <LogoDialog editor={editor} logo={logo} setLogo={setLogo} /> }
@ -45,3 +54,34 @@ export const EditorDialog = ({
</div> </div>
); );
}; };
function mapStateToProps(state) {
const {
user: {
mode, routerPoints, activeSticker, logo, user, title, address,
},
} = state;
return {
mode,
routerPoints,
activeSticker,
logo,
user,
title,
address,
editor,
};
}
const mapDispatchToProps = dispatch => bindActionCreators({
routerCancel,
routerSubmit,
setLogo,
}, dispatch);
export const EditorDialog = connect(
mapStateToProps,
mapDispatchToProps
)(Component);

View file

@ -1,68 +1,60 @@
import React from 'react'; import React from 'react';
const noPoints = ({ cancelDrawing }) => ( type Props = {
routerCancel: Function,
routerSubmit: Function,
routerPoints: Number,
}
const noPoints = ({ routerCancel }: Props) => (
<div className="helper router-helper"> <div className="helper router-helper">
<div className="helper__text"> <div className="helper__text">
<div className="big white">Укажите на карте первую точку маршрута</div> <div className="big white">Укажите на карте первую точку маршрута</div>
<div className="small gray">Путь прокладывается по улицам, тротуарам и тропинкам</div> <div className="small gray">Путь прокладывается по улицам, тротуарам и тропинкам</div>
</div> </div>
<div className="helper__buttons"> <div className="helper__buttons">
<div className="button router-helper__button" onClick={cancelDrawing}> <div className="button router-helper__button" onClick={routerCancel}>
Отмена Отмена
</div> </div>
</div> </div>
</div> </div>
); );
const firstPoint = ({ cancelDrawing }) => ( const firstPoint = ({ routerCancel }: Props) => (
<div className="helper router-helper"> <div className="helper router-helper">
<div className="helper__text"> <div className="helper__text">
<div className="big white">Укажите на карте конечную точку маршрута</div> <div className="big white">Укажите на карте конечную точку маршрута</div>
<div className="small gray"> Вы сможете добавить уточняющие точки</div> <div className="small gray"> Вы сможете добавить уточняющие точки</div>
</div> </div>
<div className="helper__buttons"> <div className="helper__buttons">
<div className="button router-helper__button" onClick={cancelDrawing}> <div className="button router-helper__button" onClick={routerCancel}>
Отмена Отмена
</div> </div>
</div> </div>
</div> </div>
); );
const draggablePoints = ({ cancelDrawing, submitDrawing }) => ( const draggablePoints = ({ routerCancel, routerSubmit }: Props) => (
<div className="helper router-helper"> <div className="helper router-helper">
<div className="helper__text"> <div className="helper__text">
<div className="big white">Продолжите маршрут, щелкая по карте</div> <div className="big white">Продолжите маршрут, щелкая по карте</div>
<div className="small gray">Потяните линию, чтобы указать промежуточные точки</div> <div className="small gray">Потяните линию, чтобы указать промежуточные точки</div>
</div> </div>
<div className="helper__buttons button-group"> <div className="helper__buttons button-group">
<div className="button button_red router-helper__button" onClick={cancelDrawing}> <div className="button button_red router-helper__button" onClick={routerCancel}>
Отмена Отмена
</div> </div>
<div className="button primary router-helper__button" onClick={submitDrawing}> <div className="button primary router-helper__button" onClick={routerSubmit}>
Применить Применить
</div> </div>
</div> </div>
</div> </div>
); );
export class RouterDialog extends React.Component { export const RouterDialog = ({ routerPoints, routerCancel, routerSubmit }: Props) => (
cancelDrawing = () => { <div>
this.props.editor.router.cancelDrawing(); {!routerPoints && noPoints({ routerCancel })}
}; {routerPoints === 1 && firstPoint({ routerCancel })}
{routerPoints >= 2 && draggablePoints({ routerCancel, routerSubmit })}
submitDrawing = () => { </div>
this.props.editor.router.submitDrawing(); );
};
render() {
const { routerPoints, editor } = this.props;
const { cancelDrawing, submitDrawing } = this;
return (
<div>
{!routerPoints && noPoints({ cancelDrawing })}
{routerPoints === 1 && firstPoint({ cancelDrawing })}
{routerPoints >= 2 && draggablePoints({ cancelDrawing, submitDrawing })}
</div>
);
}
}

View file

@ -2,14 +2,13 @@ import L from 'leaflet';
import Routing from 'leaflet-routing-machine/src/index'; import Routing from 'leaflet-routing-machine/src/index';
import { CONFIG } from '$config'; import { CONFIG } from '$config';
import { DomMarker } from '$utils/DomMarker'; import { DomMarker } from '$utils/DomMarker';
import { MODES } from '$constants/modes';
export class Router { export class Router {
constructor({ map, lockMapClicks, setRouterPoints, changeMode, pushPolyPoints }) { constructor({ map, lockMapClicks, setRouterPoints, pushPolyPoints }) {
this.waypoints = []; this.waypoints = [];
this.lockMapClicks = lockMapClicks; this.lockMapClicks = lockMapClicks;
this.setRouterPoints = setRouterPoints; this.setRouterPoints = setRouterPoints;
this.changeMode = changeMode; // this.changeMode = changeMode;
this.pushPolyPoints = pushPolyPoints; this.pushPolyPoints = pushPolyPoints;
const routeLine = r => Routing.line(r, { const routeLine = r => Routing.line(r, {
@ -45,6 +44,8 @@ export class Router {
// this.router._line.on('mousedown', console.log); // this.router._line.on('mousedown', console.log);
} }
// changeMode = value => store.dispatch(setMode(value));
// //
pushWaypointOnClick = ({ latlng: { lat, lng } }) => { pushWaypointOnClick = ({ latlng: { lat, lng } }) => {
const waypoints = this.router.getWaypoints().filter(({ latLng }) => !!latLng); const waypoints = this.router.getWaypoints().filter(({ latLng }) => !!latLng);
@ -116,7 +117,8 @@ export class Router {
cancelDrawing = () => { cancelDrawing = () => {
this.router.setWaypoints([]); this.router.setWaypoints([]);
this.changeMode(MODES.NONE); // this.router.
// this.changeMode(MODES.NONE);
}; };
submitDrawing = () => { submitDrawing = () => {
@ -125,10 +127,12 @@ export class Router {
const { coordinates } = route; const { coordinates } = route;
this.pushPolyPoints(coordinates); this.pushPolyPoints(coordinates);
const waypoints = this.router.getWaypoints().filter(({ latLng }) => !!latLng);
this.router.setWaypoints(waypoints[waypoints.length - 1]);
// this.changeMode(MODES.POLY); this.router.setWaypoints([]);
// UNCOMMENT THIS TO CONTINUE DRAWING
// const waypoints = this.router.getWaypoints().filter(({ latLng }) => !!latLng);
// this.router.setWaypoints(waypoints[waypoints.length - 1]);
}; };
clearAll = () => { clearAll = () => {

View file

@ -16,3 +16,6 @@ export const setAddress = address => ({ type: ACTIONS.SET_ADDRESS, address });
export const startEditing = () => ({ type: ACTIONS.START_EDITING }); export const startEditing = () => ({ type: ACTIONS.START_EDITING });
export const stopEditing = () => ({ type: ACTIONS.STOP_EDITING }); export const stopEditing = () => ({ type: ACTIONS.STOP_EDITING });
export const routerCancel = () => ({ type: ACTIONS.ROUTER_CANCEL });
export const routerSubmit = () => ({ type: ACTIONS.ROUTER_SUBMIT });

View file

@ -14,4 +14,7 @@ export const ACTIONS = {
START_EDITING: 'START_EDITING', START_EDITING: 'START_EDITING',
STOP_EDITING: 'STOP_EDITING', STOP_EDITING: 'STOP_EDITING',
ROUTER_CANCEL: 'ROUTER_CANCEL',
ROUTER_SUBMIT: 'ROUTER_SUBMIT',
}; };

View file

@ -127,6 +127,20 @@ function* setLogoSaga({ logo }) {
} }
} }
function* routerCancelSaga() {
yield call(editor.router.cancelDrawing);
yield put(setMode(MODES.NONE));
return true;
}
function* routerSubmitSaga() {
yield call(editor.router.submitDrawing);
yield put(setMode(MODES.NONE));
return true;
}
export function* userSaga() { export function* userSaga() {
// ASYNCHRONOUS!!! :-) // ASYNCHRONOUS!!! :-)
@ -139,4 +153,7 @@ export function* userSaga() {
yield takeEvery(ACTIONS.USER_LOGOUT, userLogoutSaga); yield takeEvery(ACTIONS.USER_LOGOUT, userLogoutSaga);
yield takeEvery(ACTIONS.SET_ACTIVE_STICKER, setActiveStickerSaga); yield takeEvery(ACTIONS.SET_ACTIVE_STICKER, setActiveStickerSaga);
yield takeEvery(ACTIONS.SET_LOGO, setLogoSaga); yield takeEvery(ACTIONS.SET_LOGO, setLogoSaga);
yield takeEvery(ACTIONS.ROUTER_CANCEL, routerCancelSaga);
yield takeEvery(ACTIONS.ROUTER_SUBMIT, routerSubmitSaga);
} }