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