2016-07-16 10:38:06 -07:00
|
|
|
import { Observable } from 'rx';
|
2016-07-20 15:06:44 -07:00
|
|
|
import { push } from 'react-router-redux';
|
2016-07-19 16:36:34 -07:00
|
|
|
|
2016-07-16 10:38:06 -07:00
|
|
|
import { types } from './actions';
|
2016-07-19 16:36:34 -07:00
|
|
|
import { makeToast } from '../../../toasts/redux/actions';
|
2016-07-20 15:06:44 -07:00
|
|
|
import { fetchChallenges } from '../../challenges/redux/actions';
|
2016-07-19 16:36:34 -07:00
|
|
|
import {
|
|
|
|
updateUserFlag,
|
|
|
|
updateUserEmail,
|
|
|
|
updateUserLang,
|
|
|
|
doActionOnError
|
|
|
|
} from '../../../redux/actions';
|
2016-07-20 15:06:44 -07:00
|
|
|
import { userSelector } from '../../../redux/selectors';
|
2016-07-16 10:38:06 -07:00
|
|
|
import { postJSON$ } from '../../../../utils/ajax-stream';
|
2016-07-19 16:36:34 -07:00
|
|
|
import langs from '../../../../utils/supported-languages';
|
2016-08-13 18:09:32 -07:00
|
|
|
import combineSagas from '../../../../utils/combine-sagas';
|
2016-07-16 10:38:06 -07:00
|
|
|
|
|
|
|
const urlMap = {
|
|
|
|
isLocked: 'lockdown',
|
|
|
|
sendQuincyEmail: 'quincy-email',
|
|
|
|
sendNotificationEmail: 'notification-email',
|
|
|
|
sendMonthlyEmail: 'announcement-email'
|
|
|
|
};
|
2016-07-19 16:36:34 -07:00
|
|
|
|
|
|
|
export function updateUserEmailSaga(actions$, getState) {
|
|
|
|
return actions$
|
|
|
|
.filter(({ type }) => type === types.updateMyEmail)
|
|
|
|
.flatMap(({ payload: email }) => {
|
|
|
|
const {
|
|
|
|
app: { user: username, csrfToken: _csrf },
|
|
|
|
entities: { user: userMap }
|
|
|
|
} = getState();
|
|
|
|
const { email: oldEmail } = userMap[username] || {};
|
|
|
|
const body = { _csrf, email };
|
|
|
|
const optimisticUpdate$ = Observable.just(
|
|
|
|
updateUserEmail(username, email)
|
|
|
|
);
|
|
|
|
const ajaxUpdate$ = postJSON$('/update-my-email', body)
|
|
|
|
.map(({ message }) => makeToast({ message }))
|
|
|
|
.catch(doActionOnError(() => oldEmail ?
|
|
|
|
updateUserFlag(username, oldEmail) :
|
|
|
|
null
|
|
|
|
));
|
|
|
|
return Observable.merge(optimisticUpdate$, ajaxUpdate$);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateUserLangSaga(actions$, getState) {
|
|
|
|
const updateLang$ = actions$
|
|
|
|
.filter(({ type, payload }) => (
|
|
|
|
type === types.updateMyLang && !!langs[payload]
|
|
|
|
))
|
2016-07-20 15:06:44 -07:00
|
|
|
.map(({ payload }) => {
|
|
|
|
const state = getState();
|
|
|
|
const { user: { languageTag } } = userSelector(state);
|
|
|
|
return { lang: payload, oldLang: languageTag };
|
|
|
|
});
|
2016-07-19 16:36:34 -07:00
|
|
|
const ajaxUpdate$ = updateLang$
|
2016-07-20 15:06:44 -07:00
|
|
|
.debounce(250)
|
|
|
|
.flatMap(({ lang, oldLang }) => {
|
|
|
|
const { app: { user: username, csrfToken: _csrf } } = getState();
|
|
|
|
const body = { _csrf, lang };
|
2016-07-19 16:36:34 -07:00
|
|
|
return postJSON$('/update-my-lang', body)
|
2016-07-20 15:06:44 -07:00
|
|
|
.flatMap(({ message }) => {
|
|
|
|
return Observable.of(
|
|
|
|
// show user that we have updated their lang
|
|
|
|
makeToast({ message }),
|
|
|
|
// update url to reflect change
|
|
|
|
push(`/${lang}/settings`),
|
|
|
|
// refetch challenges in new language
|
|
|
|
fetchChallenges()
|
|
|
|
);
|
|
|
|
})
|
2016-07-19 16:36:34 -07:00
|
|
|
.catch(doActionOnError(() => {
|
|
|
|
return updateUserLang(username, oldLang);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
const optimistic$ = updateLang$
|
2016-07-20 15:06:44 -07:00
|
|
|
.map(({ lang }) => {
|
2016-07-19 16:36:34 -07:00
|
|
|
const { app: { user: username } } = getState();
|
|
|
|
return updateUserLang(username, lang);
|
|
|
|
});
|
|
|
|
return Observable.merge(ajaxUpdate$, optimistic$);
|
|
|
|
}
|
|
|
|
export function updateUserFlagSaga(actions$, getState) {
|
2016-07-16 10:38:06 -07:00
|
|
|
const toggleFlag$ = actions$
|
|
|
|
.filter(({ type, payload }) => type === types.toggleUserFlag && payload)
|
|
|
|
.map(({ payload }) => payload);
|
|
|
|
const optimistic$ = toggleFlag$.map(flag => {
|
|
|
|
const { app: { user: username } } = getState();
|
|
|
|
return updateUserFlag(username, flag);
|
|
|
|
});
|
|
|
|
const serverUpdate$ = toggleFlag$
|
|
|
|
.debounce(500)
|
|
|
|
.flatMap(flag => {
|
|
|
|
const url = `/toggle-${urlMap[ flag ]}`;
|
|
|
|
const {
|
|
|
|
app: { user: username, csrfToken: _csrf },
|
|
|
|
entities: { user: userMap }
|
|
|
|
} = getState();
|
|
|
|
const user = userMap[username];
|
|
|
|
const currentValue = user[ flag ];
|
|
|
|
return postJSON$(url, { _csrf })
|
|
|
|
.map(({ flag, value }) => {
|
|
|
|
if (currentValue === value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return updateUserFlag(username, flag);
|
|
|
|
})
|
2016-07-19 16:36:34 -07:00
|
|
|
.catch(doActionOnError(() => {
|
|
|
|
return updateUserFlag(username, currentValue);
|
|
|
|
}));
|
2016-07-16 10:38:06 -07:00
|
|
|
});
|
|
|
|
return Observable.merge(optimistic$, serverUpdate$);
|
|
|
|
}
|
2016-07-19 16:36:34 -07:00
|
|
|
|
|
|
|
export default combineSagas(
|
|
|
|
updateUserFlagSaga,
|
|
|
|
updateUserEmailSaga,
|
|
|
|
updateUserLangSaga
|
|
|
|
);
|