From 844bad2a346c89f915efec0f2f609a5ef6adf2c4 Mon Sep 17 00:00:00 2001 From: Stuart Taylor Date: Fri, 23 Mar 2018 19:42:33 +0000 Subject: [PATCH] fix(lang): Refetch mapUi on language change (#16844) * fix(lang): Refetch mapUi on language change * chore(redux): Remove redundent action * fix(blockForLang): Refecth challenge block for new lang --- common/app/entities/index.js | 10 +++++++-- common/app/redux/index.js | 5 +---- .../routes/Settings/redux/update-user-epic.js | 21 ++++++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/common/app/entities/index.js b/common/app/entities/index.js index 2e506edffd..f9a80f6d5c 100644 --- a/common/app/entities/index.js +++ b/common/app/entities/index.js @@ -21,6 +21,7 @@ export const types = createTypes([ 'addPortfolioItem', 'optoUpdatePortfolio', 'regresPortfolio', + 'resetFullBlocks', 'updateMultipleUserFlags', 'updateTheme', 'updateUserFlag', @@ -57,6 +58,8 @@ export const updateUserLang = createAction( (username, lang) => ({ username, languageTag: lang }) ); +export const resetFullBlocks = createAction(types.resetFullBlocks); + export const updateUserCurrentChallenge = createAction( types.updateUserCurrentChallenge ); @@ -207,11 +210,14 @@ export default composeReducers( map.fetchMapUi.complete ) ]: (state, { payload: { entities } }) => merge({}, state, entities), - [app.fetchNewBlock.complete]: - (state, { payload: { entities: { block }}}) => ({ + [app.fetchNewBlock.complete]: ( + state, + { payload: { entities: { block } } } + ) => ({ ...state, fullBlocks: union(state.fullBlocks, [ Object.keys(block)[0] ]) }), + [types.resetFullBlocks]: state => ({ ...state, fullBlocks: [] }), [ challenges.submitChallenge.complete ]: (state, { payload: { username, points, challengeInfo } }) => ({ diff --git a/common/app/redux/index.js b/common/app/redux/index.js index 8e8ad5e73e..fce54dfa29 100644 --- a/common/app/redux/index.js +++ b/common/app/redux/index.js @@ -55,7 +55,6 @@ export const types = createTypes([ createAsyncTypes('fetchChallenge'), createAsyncTypes('fetchChallenges'), createAsyncTypes('fetchNewBlock'), - 'updateChallenges', createAsyncTypes('fetchOtherUser'), createAsyncTypes('fetchUser'), 'showSignIn', @@ -132,8 +131,6 @@ export const fetchNewBlockComplete = createAction( ({ meta: { challenge } }) => ({ ...createCurrentChallengeMeta(challenge) }) ); -export const updateChallenges = createAction(types.updateChallenges); - // updateTitle(title: String) => Action export const updateTitle = createAction(types.updateTitle); @@ -241,7 +238,7 @@ export const isSignedInSelector = state => !!userSelector(state).username; export const challengeSelector = state => { const challengeName = currentChallengeSelector(state); const challengeMap = entitiesSelector(state).challenge || {}; - return challengeMap[challengeName] || {}; + return challengeMap[challengeName] || firstChallengeSelector(state); }; export const isCurrentBlockCompleteSelector = state => { diff --git a/common/app/routes/Settings/redux/update-user-epic.js b/common/app/routes/Settings/redux/update-user-epic.js index d95779801f..8604e3d1b4 100644 --- a/common/app/routes/Settings/redux/update-user-epic.js +++ b/common/app/routes/Settings/redux/update-user-epic.js @@ -9,19 +9,22 @@ import { updateMyPortfolioComplete } from './'; import { makeToast } from '../../../Toasts/redux'; +import { fetchMapUi } from '../../../Map/redux'; import { - updateChallenges, doActionOnError, usernameSelector, userSelector, - createErrorObservable + createErrorObservable, + challengeSelector, + fetchNewBlock } from '../../../redux'; import { updateUserEmail, updateUserLang, updateMultipleUserFlags, regresPortfolio, - optoUpdatePortfolio + optoUpdatePortfolio, + resetFullBlocks } from '../../../entities'; import { postJSON$ } from '../../../../utils/ajax-stream'; @@ -198,8 +201,7 @@ export function updateUserLangEpic(actions, { getState }) { type === types.updateMyLang && !!langs[payload] )) .map(({ payload }) => { - const state = getState(); - const { languageTag } = userSelector(state); + const { languageTag } = userSelector(getState()); return { lang: payload, oldLang: languageTag }; }); const ajaxUpdate = updateLang @@ -209,13 +211,18 @@ export function updateUserLangEpic(actions, { getState }) { const body = { _csrf, lang }; return postJSON$('/update-my-lang', body) .flatMap(({ message }) => { + const { block } = challengeSelector(getState()); return Observable.of( // show user that we have updated their lang makeToast({ message }), // update url to reflect change onRouteSettings({ lang }), - // refetch challenges in new language - updateChallenges() + // clear fullBlocks so challenges are fetched in correct language + resetFullBlocks(), + // refetch current challenge block updated for new lang + fetchNewBlock(block), + // refetch mapUi in new language + fetchMapUi() ); }) .catch(doActionOnError(() => {