fix(client): call donate api withCredentials

This commit is contained in:
Mrugesh Mohapatra
2019-10-20 16:10:38 +05:30
committed by mrugesh
parent 2a449d03a3
commit 368aa688fa
2 changed files with 31 additions and 14 deletions

View File

@ -14,11 +14,10 @@ import {
} from '@freecodecamp/react-bootstrap'; } from '@freecodecamp/react-bootstrap';
import { injectStripe } from 'react-stripe-elements'; import { injectStripe } from 'react-stripe-elements';
import { apiLocation } from '../../../../config/env.json';
import Spacer from '../../../components/helpers/Spacer'; import Spacer from '../../../components/helpers/Spacer';
import StripeCardForm from './StripeCardForm'; import StripeCardForm from './StripeCardForm';
import DonateCompletion from './DonateCompletion'; import DonateCompletion from './DonateCompletion';
import { postJSON$ } from '../../../templates/Challenges/utils/ajax-stream.js'; import { postChargeStripe } from '../../../utils/ajax';
import { userSelector, isSignedInSelector } from '../../../redux'; import { userSelector, isSignedInSelector } from '../../../redux';
const propTypes = { const propTypes = {
@ -123,34 +122,40 @@ class DonateForm extends Component {
} }
})); }));
const chargeStripePath = isSignedIn return postChargeStripe(isSignedIn, {
? `${apiLocation}/internal/donate/charge-stripe`
: `${apiLocation}/unauthenticated/donate/charge-stripe`;
return postJSON$(chargeStripePath, {
token, token,
amount amount
}).subscribe( })
res => .then(response => {
const data = response && response.data;
this.setState(state => ({ this.setState(state => ({
...state, ...state,
donationState: { donationState: {
...state.donationState, ...state.donationState,
processing: false, processing: false,
success: true, success: true,
error: res.error error: data.error ? data.error : null
} }
})), }));
err => })
.catch(error => {
const data =
error.response && error.response.data
? error.response.data
: {
error:
'Something is not right. Please contact team@freecodecamp.org'
};
this.setState(state => ({ this.setState(state => ({
...state, ...state,
donationState: { donationState: {
...state.donationState, ...state.donationState,
processing: false, processing: false,
success: false, success: false,
error: err.error error: data.error
} }
})) }));
); });
} }
resetDonation() { resetDonation() {

View File

@ -3,9 +3,14 @@ import { apiLocation } from '../../config/env.json';
import axios from 'axios'; import axios from 'axios';
const base = apiLocation + '/internal'; const base = apiLocation + '/internal';
const baseUnauthenticated = apiLocation + '/unauthenticated';
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
export function postUnauthenticated(path, body) {
return axios.post(`${baseUnauthenticated}${path}`, body);
}
function get(path) { function get(path) {
return axios.get(`${base}${path}`); return axios.get(`${base}${path}`);
} }
@ -45,6 +50,13 @@ export function getArticleById(shortId) {
} }
/** POST **/ /** POST **/
export function postChargeStripe(isSignedIn, body) {
const donatePath = '/donate/charge-stripe';
return isSignedIn
? post(donatePath, body)
: postUnauthenticated(donatePath, body);
}
export function putUpdateLegacyCert(body) { export function putUpdateLegacyCert(body) {
return post('/update-my-projects', body); return post('/update-my-projects', body);
} }