redux: setMode sagas

This commit is contained in:
muerwre 2018-11-26 13:33:18 +07:00
parent dde6cf83e9
commit 0d47cd8773
6 changed files with 127 additions and 51 deletions

View file

@ -7,35 +7,62 @@ import { toHours } from '$utils/format';
import { Icon } from '$components/panels/Icon'; import { Icon } from '$components/panels/Icon';
import { EditorDialog } from '$components/panels/EditorDialog'; import { EditorDialog } from '$components/panels/EditorDialog';
import { LogoPreview } from '$components/logo/LogoPreview'; import { LogoPreview } from '$components/logo/LogoPreview';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { setMode } from '$redux/user/actions';
import type { UserType } from '$constants/types';
import { editor } from '$modules/Editor';
export class EditorPanel extends React.PureComponent { type Props = {
startPolyMode = () => this.props.editor.changeMode(MODES.POLY); user: UserType,
editing: false,
mode: String,
changed: Boolean,
distance: Number,
// not implemented
title: String,
address: String,
mode: String,
editing: Boolean,
logo: String,
routerPoints: Number,
estimateTime: Number,
activeSticker: String,
title: String,
address: String,
startStickerMode = () => this.props.editor.changeMode(MODES.STICKERS); setMode: Function,
}
startRouterMode = () => this.props.editor.changeMode(MODES.ROUTER); class Component extends React.PureComponent<Props, void> {
startPolyMode = () => this.props.setMode(MODES.POLY);
startShotterMode = () => this.props.editor.changeMode(MODES.SHOTTER); startStickerMode = () => this.props.setMode(MODES.STICKERS);
startRouterMode = () => this.props.setMode(MODES.ROUTER);
startTrashMode = () => this.props.editor.changeMode(MODES.TRASH); startShotterMode = () => this.props.setMode(MODES.SHOTTER);
startTrashMode = () => this.props.setMode(MODES.TRASH);
startLogoMode = () => this.props.editor.changeMode(MODES.LOGO); startLogoMode = () => this.props.setMode(MODES.LOGO);
startSaveMode = () => this.props.setMode(MODES.SAVE);
startSaveMode = () => this.props.editor.changeMode(MODES.SAVE); // startPolyMode = () => this.props.editor.changeMode(MODES.POLY);
// startStickerMode = () => this.props.editor.changeMode(MODES.STICKERS);
// startRouterMode = () => this.props.editor.changeMode(MODES.ROUTER);
// startShotterMode = () => this.props.editor.changeMode(MODES.SHOTTER);
// startTrashMode = () => this.props.editor.changeMode(MODES.TRASH);
// startLogoMode = () => this.props.editor.changeMode(MODES.LOGO);
// startSaveMode = () => this.props.editor.changeMode(MODES.SAVE);
stopEditing = () => { stopEditing = () => {
if (!this.props.changed){ if (!this.props.changed) {
this.props.editor.cancelEditing(); editor.cancelEditing();
} else { } else {
this.props.editor.changeMode(MODES.CONFIRM_CANCEL); editor.changeMode(MODES.CONFIRM_CANCEL);
} }
}; };
startEditing = () => this.props.editor.startEditing(); startEditing = () => editor.startEditing();
render() { render() {
const { const {
mode, routerPoints, editor, totalDistance, estimateTime, activeSticker, logo, user, editing, title, address, changed, mode, routerPoints, distance, estimateTime, activeSticker, logo, user, editing, title, address, changed,
} = this.props; } = this.props;
return ( return (
@ -56,10 +83,12 @@ export class EditorPanel extends React.PureComponent {
<div className="control-dist"> <div className="control-dist">
{changed && '(ch) '} {changed && '(ch) '}
{totalDistance} км {distance} км
<Icon icon="icon-cycle" size={32} /> <Icon icon="icon-cycle" size={32} />
{ {
<span>{toHours(estimateTime)}</span> <span>{
// toHours(estimateTime)
}HOURS HERE</span>
} }
</div> </div>
@ -146,3 +175,36 @@ export class EditorPanel extends React.PureComponent {
); );
} }
} }
function mapStateToProps(state) {
const {
user: {
user,
editing,
mode, routerPoints, distance, estimateTime, activeSticker, logo, title, address, changed,
},
} = state;
return {
user,
editing,
mode,
routerPoints,
distance,
estimateTime,
activeSticker,
logo,
title,
address,
changed,
};
}
const mapDispatchToProps = dispatch => bindActionCreators({
setMode,
}, dispatch);
export const EditorPanel = connect(
mapStateToProps,
mapDispatchToProps
)(Component);

View file

@ -17,18 +17,14 @@ import { hot } from 'react-hot-loader';
import type { UserType } from '$constants/types'; import type { UserType } from '$constants/types';
type Props = { type Props = {
// todo: clean this!
user: UserType, user: UserType,
editing: false, editing: false,
mode: String, mode: String,
changed: Boolean,
// not implemented distance: Number,
title: String, title: String,
address: String, address: String,
changed: Boolean,
totalDistance: Number,
}
type State = {
mode: String, mode: String,
editing: Boolean, editing: Boolean,
logo: String, logo: String,
@ -37,7 +33,10 @@ type State = {
activeSticker: String, activeSticker: String,
title: String, title: String,
address: String, address: String,
changed: Boolean, }
type State = {
} }
class Component extends React.Component<Props, State> { class Component extends React.Component<Props, State> {
@ -224,12 +223,7 @@ class Component extends React.Component<Props, State> {
render() { render() {
const { const {
state: { props: { user }
mode, routerPoints, totalDistance, estimateTime, activeSticker, logo, title, address, changed,
},
props: {
user, editing,
}
} = this; } = this;
return ( return (
@ -245,20 +239,7 @@ class Component extends React.Component<Props, State> {
userLogout={this.userLogout} userLogout={this.userLogout}
/> />
<EditorPanel <EditorPanel />
editor={editor}
mode={mode}
routerPoints={routerPoints}
totalDistance={totalDistance}
estimateTime={estimateTime}
activeSticker={activeSticker}
logo={logo}
user={user}
editing={editing}
title={title}
address={address}
changed={changed}
/>
</div> </div>
); );
} }
@ -269,12 +250,30 @@ function mapStateToProps(state) {
user: { user: {
user, user,
editing, editing,
mode,
routerPoints,
totalDistance,
estimateTime,
activeSticker,
logo,
title,
address,
changed,
}, },
} = state; } = state;
return { return {
user, user,
editing, editing,
mode,
routerPoints,
totalDistance,
estimateTime,
activeSticker,
logo,
title,
address,
changed,
}; };
} }

View file

@ -118,12 +118,12 @@ export class Editor {
this.switches[mode].toggle(); // if we have special function on mode when it toggles this.switches[mode].toggle(); // if we have special function on mode when it toggles
} else { } else {
this.disableMode(mode); this.disableMode(mode);
this.setMode(MODES.NONE); // this.setMode(MODES.NONE);
this.mode = MODES.NONE; this.mode = MODES.NONE;
} }
} else { } else {
this.disableMode(this.mode); this.disableMode(this.mode);
this.setMode(mode); // this.setMode(mode);
this.mode = mode; this.mode = mode;
this.enableMode(mode); this.enableMode(mode);
} }

View file

@ -12,7 +12,7 @@ import { userSaga } from '$redux/user/sagas';
const userPersistConfig = { const userPersistConfig = {
key: 'user', key: 'user',
blacklist: ['editing'], whitelist: ['user'],
storage, storage,
}; };

View file

@ -2,6 +2,7 @@ import { createReducer } from 'reduxsauce';
import { ACTIONS } from '$redux/user/constants'; import { ACTIONS } from '$redux/user/constants';
import { DEFAULT_USER } from '$constants/auth'; import { DEFAULT_USER } from '$constants/auth';
import { MODES } from '$constants/modes'; import { MODES } from '$constants/modes';
import { DEFAULT_LOGO } from '$constants/logos';
const setUser = (state, { user }) => ({ const setUser = (state, { user }) => ({
...state, ...state,
@ -26,7 +27,14 @@ export const INITIAL_STATE = {
user: { ...DEFAULT_USER }, user: { ...DEFAULT_USER },
editing: false, editing: false,
mode: MODES.NONE, mode: MODES.NONE,
logo: DEFAULT_LOGO,
routerPoints: 0,
distance: 0, distance: 0,
estimateTime: 0,
activeSticker: null,
title: 0,
address: '',
changed: false,
}; };
export const userReducer = createReducer(INITIAL_STATE, HANDLERS); export const userReducer = createReducer(INITIAL_STATE, HANDLERS);

View file

@ -1,9 +1,10 @@
import { REHYDRATE } from 'redux-persist'; import { REHYDRATE } from 'redux-persist';
import { takeLatest, select, call, put } from 'redux-saga/effects'; import { takeLatest, select, call, put, takeEvery } from 'redux-saga/effects';
import { checkUserToken, getGuestToken, getStoredMap } from '$utils/api'; import { checkUserToken, getGuestToken, getStoredMap } from '$utils/api';
import { setUser } from '$redux/user/actions'; import { setUser } from '$redux/user/actions';
import { getUrlData, pushPath } from '$utils/history'; import { getUrlData, pushPath } from '$utils/history';
import { editor } from '$modules/Editor'; import { editor } from '$modules/Editor';
import { ACTIONS } from '$redux/user/constants';
const getUser = state => (state.user.user); const getUser = state => (state.user.user);
const hideLoader = () => { const hideLoader = () => {
@ -73,8 +74,14 @@ function* authChechSaga() {
return yield call(mapInitSaga); return yield call(mapInitSaga);
} }
function* setModeSaga({ mode }) {
return yield editor.changeMode(mode);
// console.log('change', mode);
}
export function* userSaga() { export function* userSaga() {
// Login // Login
// yield takeLatest(AUTH_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga); // yield takeLatest(AUTH_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
yield takeLatest(REHYDRATE, authChechSaga); yield takeLatest(REHYDRATE, authChechSaga);
yield takeEvery(ACTIONS.SET_MODE, setModeSaga);
} }