2020-10-14 13:23:26 +03:00
|
|
|
import {
|
|
|
|
put,
|
|
|
|
select,
|
|
|
|
takeEvery,
|
|
|
|
takeLeading,
|
|
|
|
delay,
|
2021-02-08 10:28:36 +03:00
|
|
|
call,
|
|
|
|
take
|
2020-10-14 13:23:26 +03:00
|
|
|
} 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,
|
2021-02-08 10:28:36 +03:00
|
|
|
recentlyClaimedBlockSelector,
|
2020-10-14 13:23:26 +03:00
|
|
|
addDonationComplete,
|
|
|
|
addDonationError,
|
|
|
|
postChargeStripeComplete,
|
2021-02-08 10:28:36 +03:00
|
|
|
postChargeStripeError,
|
|
|
|
types as appTypes
|
2019-12-02 15:48:53 +03:00
|
|
|
} from './';
|
|
|
|
|
2021-04-02 09:33:34 +03:00
|
|
|
import {
|
|
|
|
addDonation,
|
|
|
|
postChargeStripe,
|
|
|
|
postCreateStripeSession
|
|
|
|
} from '../utils/ajax';
|
2020-10-14 13:23:26 +03:00
|
|
|
|
|
|
|
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);
|
2021-02-08 10:28:36 +03:00
|
|
|
const recentlyClaimedBlock = yield select(recentlyClaimedBlockSelector);
|
|
|
|
yield put(openDonationModal());
|
|
|
|
yield take(appTypes.closeDonationModal);
|
|
|
|
if (recentlyClaimedBlock) {
|
2019-12-09 17:30:24 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:33:34 +03:00
|
|
|
function* createStripeSessionSaga({ payload: { stripe, data } }) {
|
|
|
|
try {
|
|
|
|
const session = yield call(postCreateStripeSession, {
|
|
|
|
...data,
|
|
|
|
location: window.location.href
|
|
|
|
});
|
|
|
|
stripe.redirectToCheckout({
|
|
|
|
sessionId: session.data.id
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
const err =
|
|
|
|
error.response && error.response.data
|
|
|
|
? error.response.data.message
|
|
|
|
: defaultDonationError;
|
|
|
|
yield put(addDonationError(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:23:26 +03:00
|
|
|
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),
|
2021-04-02 09:33:34 +03:00
|
|
|
takeLeading(types.postChargeStripe, postChargeStripeSaga),
|
|
|
|
takeLeading(types.createStripeSession, createStripeSessionSaga)
|
2020-10-14 13:23:26 +03:00
|
|
|
];
|
2019-12-02 15:48:53 +03:00
|
|
|
}
|