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',
'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 } }) => ({

View File

@ -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 => {

View File

@ -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(() => {