Remove optimistic update

Add multiple toast when saving challenge
and when first completed challenges
This commit is contained in:
Berkeley Martinez
2016-01-09 23:21:19 -08:00
parent e3890785a2
commit d172edecf7

View File

@ -255,26 +255,40 @@ export default Actions({
} }
// challenge completed // challenge completed
const optimisticSave = isSignedIn ? let update$;
this.post$('/completed-challenge', { id, name, challengeType }) : if (isSignedIn) {
Observable.just(true); const body = { id, name, challengeType };
update$ = this.postJSON$('/completed-challenge', body)
const correctAnswer = { // if post fails, will retry once
.retry(3)
.map(({ alreadyCompleted, points }) => ({
transform(state) { transform(state) {
return { return {
...state, ...state,
hikesApp: { points,
...state.hikesApp, toast: {
isCorrect: true, message:
isPressed: false, 'Challenge saved.' +
delta: [0, 0], (alreadyCompleted ? '' : ' First time Completed!'),
mouse: [ userAnswer ? 1000 : -1000, 0] title: 'Saved',
type: 'info',
id: state.toast && state.toast.id ? state.toast.id + 1 : 1
} }
}; };
} }
}))
.catch((errObj => {
const err = new Error(errObj.message);
err.stack = errObj.stack;
return {
transform(state) { return { ...state, err }; }
}; };
}));
} else {
update$ = Observable.just({ transform: (() => {}) });
}
return Observable.just({ const challengeCompleted$ = Observable.just({
transform(state) { transform(state) {
const { hikes, currentHike: { id } } = state.hikesApp; const { hikes, currentHike: { id } } = state.hikesApp;
const currentHike = findNextHike(hikes, id); const currentHike = findNextHike(hikes, id);
@ -291,10 +305,10 @@ export default Actions({
}, },
toast: { toast: {
title: 'Congratulations!', title: 'Congratulations!',
message: 'Hike completed', message: 'Hike completed.' + (isSignedIn ? ' Saving...' : ''),
id: state.toast && typeof state.toast.id === 'number' ? id: state.toast && state.toast.id ?
state.toast.id + 1 : state.toast.id + 1 :
0, 1,
type: 'success' type: 'success'
}, },
location: { location: {
@ -304,9 +318,25 @@ export default Actions({
'/hikes' '/hikes'
} }
}; };
}, }
optimistic: optimisticSave });
})
const correctAnswer = {
transform(state) {
return {
...state,
hikesApp: {
...state.hikesApp,
isCorrect: true,
isPressed: false,
delta: [0, 0],
mouse: [ userAnswer ? 1000 : -1000, 0]
}
};
}
};
return Observable.merge(challengeCompleted$, update$)
.delay(300) .delay(300)
.startWith(correctAnswer) .startWith(correctAnswer)
.catch(err => Observable.just({ .catch(err => Observable.just({