2019-02-06 14:30:54 +03:00
|
|
|
import { put, select, call, takeEvery } from 'redux-saga/effects';
|
2019-08-30 19:15:26 +02:00
|
|
|
import store from 'store';
|
2019-02-06 14:30:54 +03:00
|
|
|
|
|
|
|
import {
|
|
|
|
isSignedInSelector,
|
|
|
|
updateComplete,
|
2019-12-02 15:48:53 +03:00
|
|
|
updateFailed,
|
|
|
|
allowDonationRequests
|
2019-02-06 14:30:54 +03:00
|
|
|
} from '../../../redux';
|
|
|
|
|
|
|
|
import { post } from '../../../utils/ajax';
|
|
|
|
|
2019-02-06 14:32:36 +03:00
|
|
|
import { randomCompliment } from '../utils/get-words';
|
|
|
|
import { updateSuccessMessage } from './';
|
|
|
|
|
2019-08-30 19:15:26 +02:00
|
|
|
export const CURRENT_CHALLENGE_KEY = 'currentChallengeId';
|
|
|
|
|
|
|
|
export function* currentChallengeSaga({ payload: id }) {
|
|
|
|
store.set(CURRENT_CHALLENGE_KEY, id);
|
2019-02-06 14:30:54 +03:00
|
|
|
const isSignedIn = yield select(isSignedInSelector);
|
2019-08-30 19:15:26 +02:00
|
|
|
if (isSignedIn) {
|
2019-02-06 14:30:54 +03:00
|
|
|
const update = {
|
|
|
|
endpoint: '/update-my-current-challenge',
|
|
|
|
payload: {
|
2019-08-30 19:15:26 +02:00
|
|
|
currentChallengeId: id
|
2019-02-06 14:30:54 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
yield call(post, update.endpoint, update.payload);
|
|
|
|
yield put(updateComplete());
|
|
|
|
} catch {
|
|
|
|
yield put(updateFailed(update));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:48:53 +03:00
|
|
|
export function* updateSuccessMessageSaga() {
|
2019-02-06 14:32:36 +03:00
|
|
|
yield put(updateSuccessMessage(randomCompliment()));
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:48:53 +03:00
|
|
|
export function* allowDonationRequestsSaga() {
|
|
|
|
yield put(allowDonationRequests());
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:30:54 +03:00
|
|
|
export function createCurrentChallengeSaga(types) {
|
|
|
|
return [
|
2019-02-06 14:32:36 +03:00
|
|
|
takeEvery(types.challengeMounted, currentChallengeSaga),
|
2019-12-02 15:48:53 +03:00
|
|
|
takeEvery(types.challengeMounted, updateSuccessMessageSaga),
|
|
|
|
takeEvery(types.lastBlockChalSubmitted, allowDonationRequestsSaga)
|
2019-02-06 14:30:54 +03:00
|
|
|
];
|
|
|
|
}
|