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
This commit is contained in:
Stuart Taylor
2018-03-23 19:42:33 +00:00
committed by mrugesh mohapatra
parent 5ad70d90df
commit 844bad2a34
3 changed files with 23 additions and 13 deletions

View File

@ -21,6 +21,7 @@ export const types = createTypes([
'addPortfolioItem', 'addPortfolioItem',
'optoUpdatePortfolio', 'optoUpdatePortfolio',
'regresPortfolio', 'regresPortfolio',
'resetFullBlocks',
'updateMultipleUserFlags', 'updateMultipleUserFlags',
'updateTheme', 'updateTheme',
'updateUserFlag', 'updateUserFlag',
@ -57,6 +58,8 @@ export const updateUserLang = createAction(
(username, lang) => ({ username, languageTag: lang }) (username, lang) => ({ username, languageTag: lang })
); );
export const resetFullBlocks = createAction(types.resetFullBlocks);
export const updateUserCurrentChallenge = createAction( export const updateUserCurrentChallenge = createAction(
types.updateUserCurrentChallenge types.updateUserCurrentChallenge
); );
@ -207,11 +210,14 @@ export default composeReducers(
map.fetchMapUi.complete map.fetchMapUi.complete
) )
]: (state, { payload: { entities } }) => merge({}, state, entities), ]: (state, { payload: { entities } }) => merge({}, state, entities),
[app.fetchNewBlock.complete]: [app.fetchNewBlock.complete]: (
(state, { payload: { entities: { block }}}) => ({ state,
{ payload: { entities: { block } } }
) => ({
...state, ...state,
fullBlocks: union(state.fullBlocks, [ Object.keys(block)[0] ]) fullBlocks: union(state.fullBlocks, [ Object.keys(block)[0] ])
}), }),
[types.resetFullBlocks]: state => ({ ...state, fullBlocks: [] }),
[ [
challenges.submitChallenge.complete challenges.submitChallenge.complete
]: (state, { payload: { username, points, challengeInfo } }) => ({ ]: (state, { payload: { username, points, challengeInfo } }) => ({

View File

@ -55,7 +55,6 @@ export const types = createTypes([
createAsyncTypes('fetchChallenge'), createAsyncTypes('fetchChallenge'),
createAsyncTypes('fetchChallenges'), createAsyncTypes('fetchChallenges'),
createAsyncTypes('fetchNewBlock'), createAsyncTypes('fetchNewBlock'),
'updateChallenges',
createAsyncTypes('fetchOtherUser'), createAsyncTypes('fetchOtherUser'),
createAsyncTypes('fetchUser'), createAsyncTypes('fetchUser'),
'showSignIn', 'showSignIn',
@ -132,8 +131,6 @@ export const fetchNewBlockComplete = createAction(
({ meta: { challenge } }) => ({ ...createCurrentChallengeMeta(challenge) }) ({ meta: { challenge } }) => ({ ...createCurrentChallengeMeta(challenge) })
); );
export const updateChallenges = createAction(types.updateChallenges);
// updateTitle(title: String) => Action // updateTitle(title: String) => Action
export const updateTitle = createAction(types.updateTitle); export const updateTitle = createAction(types.updateTitle);
@ -241,7 +238,7 @@ export const isSignedInSelector = state => !!userSelector(state).username;
export const challengeSelector = state => { export const challengeSelector = state => {
const challengeName = currentChallengeSelector(state); const challengeName = currentChallengeSelector(state);
const challengeMap = entitiesSelector(state).challenge || {}; const challengeMap = entitiesSelector(state).challenge || {};
return challengeMap[challengeName] || {}; return challengeMap[challengeName] || firstChallengeSelector(state);
}; };
export const isCurrentBlockCompleteSelector = state => { export const isCurrentBlockCompleteSelector = state => {

View File

@ -9,19 +9,22 @@ import {
updateMyPortfolioComplete updateMyPortfolioComplete
} from './'; } from './';
import { makeToast } from '../../../Toasts/redux'; import { makeToast } from '../../../Toasts/redux';
import { fetchMapUi } from '../../../Map/redux';
import { import {
updateChallenges,
doActionOnError, doActionOnError,
usernameSelector, usernameSelector,
userSelector, userSelector,
createErrorObservable createErrorObservable,
challengeSelector,
fetchNewBlock
} from '../../../redux'; } from '../../../redux';
import { import {
updateUserEmail, updateUserEmail,
updateUserLang, updateUserLang,
updateMultipleUserFlags, updateMultipleUserFlags,
regresPortfolio, regresPortfolio,
optoUpdatePortfolio optoUpdatePortfolio,
resetFullBlocks
} from '../../../entities'; } from '../../../entities';
import { postJSON$ } from '../../../../utils/ajax-stream'; import { postJSON$ } from '../../../../utils/ajax-stream';
@ -198,8 +201,7 @@ export function updateUserLangEpic(actions, { getState }) {
type === types.updateMyLang && !!langs[payload] type === types.updateMyLang && !!langs[payload]
)) ))
.map(({ payload }) => { .map(({ payload }) => {
const state = getState(); const { languageTag } = userSelector(getState());
const { languageTag } = userSelector(state);
return { lang: payload, oldLang: languageTag }; return { lang: payload, oldLang: languageTag };
}); });
const ajaxUpdate = updateLang const ajaxUpdate = updateLang
@ -209,13 +211,18 @@ export function updateUserLangEpic(actions, { getState }) {
const body = { _csrf, lang }; const body = { _csrf, lang };
return postJSON$('/update-my-lang', body) return postJSON$('/update-my-lang', body)
.flatMap(({ message }) => { .flatMap(({ message }) => {
const { block } = challengeSelector(getState());
return Observable.of( return Observable.of(
// show user that we have updated their lang // show user that we have updated their lang
makeToast({ message }), makeToast({ message }),
// update url to reflect change // update url to reflect change
onRouteSettings({ lang }), onRouteSettings({ lang }),
// refetch challenges in new language // clear fullBlocks so challenges are fetched in correct language
updateChallenges() resetFullBlocks(),
// refetch current challenge block updated for new lang
fetchNewBlock(block),
// refetch mapUi in new language
fetchMapUi()
); );
}) })
.catch(doActionOnError(() => { .catch(doActionOnError(() => {