Merge pull request #16143 from BerkeleyTrue/fix/settings-route

fix(Router): Add lang to payload if payload doesn't exist
This commit is contained in:
mrugesh mohapatra
2017-12-05 11:48:52 +05:30
committed by GitHub
6 changed files with 81 additions and 62 deletions

View File

@ -1,18 +1,26 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { selectLocationState } from 'redux-first-router'; import { createSelector } from 'reselect';
import toUrl from './to-url.js'; import toUrl from './to-url.js';
import createHandler from './handle-press.js'; import createHandler from './handle-press.js';
import { langSelector } from './redux'; import { routesMapSelector, langSelector } from './redux';
const mapStateToProps = createSelector(
langSelector,
routesMapSelector,
(lang, routesMap) => ({ lang, routesMap })
);
const propTypes = { const propTypes = {
children: PropTypes.node, children: PropTypes.node,
dispatch: PropTypes.func, dispatch: PropTypes.func,
lang: PropTypes.string,
onClick: PropTypes.func, onClick: PropTypes.func,
redirect: PropTypes.bool, redirect: PropTypes.bool,
replace: PropTypes.bool, replace: PropTypes.bool,
routesMap: PropTypes.object,
shouldDispatch: PropTypes.bool, shouldDispatch: PropTypes.bool,
target: PropTypes.string, target: PropTypes.string,
to: PropTypes.oneOfType([ PropTypes.object, PropTypes.string ]).isRequired to: PropTypes.oneOfType([ PropTypes.object, PropTypes.string ]).isRequired
@ -22,19 +30,16 @@ export const Link = (
{ {
children, children,
dispatch, dispatch,
lang,
onClick, onClick,
redirect, redirect,
replace, replace,
routesMap,
shouldDispatch = true, shouldDispatch = true,
target, target,
to to
}, }
{ store }
) => { ) => {
const state = store.getState();
const lang = langSelector(state);
const location = selectLocationState(state);
const { routesMap } = location;
const url = toUrl(to, routesMap, lang); const url = toUrl(to, routesMap, lang);
const handler = createHandler( const handler = createHandler(
url, url,
@ -77,4 +82,4 @@ Link.contextTypes = {
}; };
Link.propTypes = propTypes; Link.propTypes = propTypes;
export default connect()(Link); export default connect(mapStateToProps)(Link);

View File

@ -1,7 +1,7 @@
import { langSelector } from './'; import { langSelector } from './';
// This enhancers sole purpose is to add the lang prop to route actions so that // This enhancers sole purpose is to add the lang prop to route actions so that
// they do not need to be explicitally added when using a RFR route action. // they do not need to be explicitly added when using a RFR route action.
export default function addLangToRouteEnhancer(routesMap) { export default function addLangToRouteEnhancer(routesMap) {
return createStore => (...args) => { return createStore => (...args) => {
const store = createStore(...args); const store = createStore(...args);
@ -11,7 +11,7 @@ export default function addLangToRouteEnhancer(routesMap) {
dispatch(action) { dispatch(action) {
if ( if (
routesMap[action.type] && routesMap[action.type] &&
(action && action.payload && !action.payload.lang) (!action.payload || !action.payload.lang)
) { ) {
action = { action = {
...action, ...action,

View File

@ -4,4 +4,5 @@ export const paramsSelector = state => selectLocationState(state).payload || {};
export const locationTypeSelector = export const locationTypeSelector =
state => selectLocationState(state).type || ''; state => selectLocationState(state).type || '';
export const langSelector = state => paramsSelector(state).lang || 'en'; export const langSelector = state => paramsSelector(state).lang || 'en';
export const routesMapSelector = state => paramsSelector(state).routesMap || {}; export const routesMapSelector = state =>
selectLocationState(state).routesMap || {};

View File

@ -12,8 +12,9 @@ import SocialSettings from './Social-Settings.jsx';
import EmailSettings from './Email-Setting.jsx'; import EmailSettings from './Email-Setting.jsx';
import LanguageSettings from './Language-Settings.jsx'; import LanguageSettings from './Language-Settings.jsx';
import SettingsSkeleton from './Settings-Skeleton.jsx'; import SettingsSkeleton from './Settings-Skeleton.jsx';
import UpdateEmail from './routes/update-email';
import { toggleUserFlag } from './redux'; import { toggleUserFlag, showUpdateEmailViewSelector } from './redux';
import { import {
toggleNightMode, toggleNightMode,
updateTitle, updateTitle,
@ -23,20 +24,10 @@ import {
} from '../../redux'; } from '../../redux';
import ChildContainer from '../../Child-Container.jsx'; import ChildContainer from '../../Child-Container.jsx';
const mapDispatchToProps = {
updateTitle,
toggleNightMode,
toggleIsAvailableForHire: () => toggleUserFlag('isAvailableForHire'),
toggleIsLocked: () => toggleUserFlag('isLocked'),
toggleQuincyEmail: () => toggleUserFlag('sendQuincyEmail'),
toggleNotificationEmail: () => toggleUserFlag('sendNotificationEmail'),
toggleMonthlyEmail: () => toggleUserFlag('sendMonthlyEmail')
};
const mapStateToProps = createSelector( const mapStateToProps = createSelector(
userSelector, userSelector,
signInLoadingSelector, signInLoadingSelector,
showUpdateEmailViewSelector,
( (
{ {
username, username,
@ -51,21 +42,33 @@ const mapStateToProps = createSelector(
sendQuincyEmail sendQuincyEmail
}, },
showLoading, showLoading,
showUpdateEmailView
) => ({ ) => ({
showLoading,
username,
email, email,
isAvailableForHire, isAvailableForHire,
isLocked,
isGithubCool, isGithubCool,
isTwitter,
isLinkedIn, isLinkedIn,
isLocked,
isTwitter,
sendMonthlyEmail, sendMonthlyEmail,
sendNotificationEmail, sendNotificationEmail,
sendQuincyEmail sendQuincyEmail,
showLoading,
showUpdateEmailView,
username
}) })
); );
const mapDispatchToProps = {
toggleIsAvailableForHire: () => toggleUserFlag('isAvailableForHire'),
toggleIsLocked: () => toggleUserFlag('isLocked'),
toggleMonthlyEmail: () => toggleUserFlag('sendMonthlyEmail'),
toggleNightMode,
toggleNotificationEmail: () => toggleUserFlag('sendNotificationEmail'),
toggleQuincyEmail: () => toggleUserFlag('sendQuincyEmail'),
updateTitle
};
const propTypes = { const propTypes = {
children: PropTypes.element, children: PropTypes.element,
email: PropTypes.string, email: PropTypes.string,
@ -80,6 +83,7 @@ const propTypes = {
sendNotificationEmail: PropTypes.bool, sendNotificationEmail: PropTypes.bool,
sendQuincyEmail: PropTypes.bool, sendQuincyEmail: PropTypes.bool,
showLoading: PropTypes.bool, showLoading: PropTypes.bool,
showUpdateEmailView: PropTypes.bool,
toggleIsAvailableForHire: PropTypes.func.isRequired, toggleIsAvailableForHire: PropTypes.func.isRequired,
toggleIsLocked: PropTypes.func.isRequired, toggleIsLocked: PropTypes.func.isRequired,
toggleMonthlyEmail: PropTypes.func.isRequired, toggleMonthlyEmail: PropTypes.func.isRequired,
@ -109,34 +113,30 @@ export class Settings extends React.Component {
render() { render() {
const { const {
children,
username,
isAvailableForHire,
isLocked,
isGithubCool,
isTwitter,
isLinkedIn,
showLoading,
email, email,
isAvailableForHire,
isGithubCool,
isLinkedIn,
isLocked,
isTwitter,
sendMonthlyEmail, sendMonthlyEmail,
sendNotificationEmail, sendNotificationEmail,
sendQuincyEmail, sendQuincyEmail,
showLoading,
showUpdateEmailView,
toggleIsAvailableForHire, toggleIsAvailableForHire,
toggleNightMode,
toggleIsLocked, toggleIsLocked,
toggleQuincyEmail,
toggleMonthlyEmail, toggleMonthlyEmail,
toggleNotificationEmail toggleNightMode,
toggleNotificationEmail,
toggleQuincyEmail,
username
} = this.props; } = this.props;
if (!username && !showLoading) { if (!username && !showLoading) {
return <SettingsSkeleton />; return <SettingsSkeleton />;
} }
if (children) { if (showUpdateEmailView) {
return ( return <UpdateEmail />;
<ChildContainer>
{ children }
</ChildContainer>
);
} }
return ( return (
<ChildContainer> <ChildContainer>

View File

@ -1,7 +1,8 @@
import { isLocationAction } from 'redux-first-router';
import { import {
addNS,
createAction, createAction,
createTypes, createTypes
handleActions
} from 'berkeleys-redux-utils'; } from 'berkeleys-redux-utils';
import userUpdateEpic from './update-user-epic.js'; import userUpdateEpic from './update-user-epic.js';
@ -19,6 +20,7 @@ export const types = createTypes([
'onRouteUpdateEmail' 'onRouteUpdateEmail'
], 'settings'); ], 'settings');
export const onRouteSettings = createAction(types.onRouteSettings); export const onRouteSettings = createAction(types.onRouteSettings);
export const onRouteUpdateEmail = createAction(types.onRouteUpdateEmail); export const onRouteUpdateEmail = createAction(types.onRouteUpdateEmail);
export const toggleUserFlag = createAction(types.toggleUserFlag); export const toggleUserFlag = createAction(types.toggleUserFlag);
@ -33,10 +35,28 @@ const defaultState = {
showUpdateEmailView: false showUpdateEmailView: false
}; };
export default handleActions( const getNS = state => state[ns];
() => ({ export const showUpdateEmailViewSelector =
[types.onRouteSettings]: state => ({ ...state, showUpdateEmailView: true }) state => getNS(state).showUpdateEmailView;
}),
defaultState, export default addNS(
ns ns,
function settingsRouteReducer(state = defaultState, action) {
if (isLocationAction(action)) {
const { type } = action;
if (type === types.onRouteUpdateEmail) {
return {
...state,
showUpdateEmailView: true
};
}
if (state.showUpdateEmailView) {
return {
...state,
showUpdateEmailView: false
};
}
}
return state;
}
); );

View File

@ -1,8 +1 @@
import UpdateEmail from './Update-Email.jsx'; export { default } from './Update-Email.jsx';
export default function updateEmailRoute() {
return [{
path: 'update-email',
component: UpdateEmail
}];
}