2020-10-14 13:23:26 +03:00
|
|
|
import {
|
|
|
|
put,
|
|
|
|
select,
|
|
|
|
takeEvery,
|
|
|
|
takeLeading,
|
|
|
|
delay,
|
|
|
|
call
|
|
|
|
} from 'redux-saga/effects';
|
2019-12-02 15:48:53 +03:00
|
|
|
|
|
|
|
import {
|
|
|
|
openDonationModal,
|
2019-12-09 17:30:24 +01:00
|
|
|
preventBlockDonationRequests,
|
|
|
|
shouldRequestDonationSelector,
|
|
|
|
preventProgressDonationRequests,
|
2020-10-14 13:23:26 +03:00
|
|
|
canRequestBlockDonationSelector,
|
|
|
|
addDonationComplete,
|
|
|
|
addDonationError,
|
|
|
|
postChargeStripeComplete,
|
|
|
|
postChargeStripeError
|
2019-12-02 15:48:53 +03:00
|
|
|
} from './';
|
|
|
|
|
2020-10-14 13:23:26 +03:00
|
|
|
import { addDonation, postChargeStripe } from '../utils/ajax';
|
|
|
|
|
|
|
|
const defaultDonationError = `Something is not right. Please contact donors@freecodecamp.org`;
|
|
|
|
|
2019-12-02 15:48:53 +03:00
|
|
|
function* showDonateModalSaga() {
|
|
|
|
let shouldRequestDonation = yield select(shouldRequestDonationSelector);
|
|
|
|
if (shouldRequestDonation) {
|
|
|
|
yield delay(200);
|
2019-12-09 17:30:24 +01:00
|
|
|
const isBlockDonation = yield select(canRequestBlockDonationSelector);
|
|
|
|
yield put(openDonationModal(isBlockDonation));
|
|
|
|
if (isBlockDonation) {
|
|
|
|
yield put(preventBlockDonationRequests());
|
|
|
|
} else {
|
|
|
|
yield put(preventProgressDonationRequests());
|
|
|
|
}
|
2019-12-02 15:48:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:23:26 +03:00
|
|
|
function* addDonationSaga({ payload }) {
|
|
|
|
try {
|
|
|
|
yield call(addDonation, payload);
|
|
|
|
yield put(addDonationComplete());
|
|
|
|
} catch (error) {
|
|
|
|
const data =
|
|
|
|
error.response && error.response.data
|
|
|
|
? error.response.data
|
|
|
|
: {
|
|
|
|
message: defaultDonationError
|
|
|
|
};
|
|
|
|
yield put(addDonationError(data.message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function* postChargeStripeSaga({ payload }) {
|
|
|
|
try {
|
|
|
|
yield call(postChargeStripe, payload);
|
|
|
|
yield put(postChargeStripeComplete());
|
|
|
|
} catch (error) {
|
|
|
|
const err =
|
|
|
|
error.response && error.response.data
|
|
|
|
? error.response.data.error
|
|
|
|
: defaultDonationError;
|
|
|
|
yield put(postChargeStripeError(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:48:53 +03:00
|
|
|
export function createDonationSaga(types) {
|
2020-10-14 13:23:26 +03:00
|
|
|
return [
|
|
|
|
takeEvery(types.tryToShowDonationModal, showDonateModalSaga),
|
|
|
|
takeEvery(types.addDonation, addDonationSaga),
|
|
|
|
takeLeading(types.postChargeStripe, postChargeStripeSaga)
|
|
|
|
];
|
2019-12-02 15:48:53 +03:00
|
|
|
}
|