small refactor
This commit is contained in:
@@ -21,7 +21,7 @@ import { signupFields } from "../../Constants/Fields/SignupFields";
|
|||||||
import { paymentfields } from "../../Constants/Fields/PaymentFields";
|
import { paymentfields } from "../../Constants/Fields/PaymentFields";
|
||||||
import PaymentModal from "../Modals/PaymentModal";
|
import PaymentModal from "../Modals/PaymentModal";
|
||||||
import "../../styles/signup.scss";
|
import "../../styles/signup.scss";
|
||||||
import Stripe from 'stripe';
|
import Stripe from "stripe";
|
||||||
|
|
||||||
const SignupPage = () => {
|
const SignupPage = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -36,7 +36,9 @@ const SignupPage = () => {
|
|||||||
const modalText = "Description of monthly and annual subscriptions";
|
const modalText = "Description of monthly and annual subscriptions";
|
||||||
|
|
||||||
// this could also go in App.js if needed in the future
|
// this could also go in App.js if needed in the future
|
||||||
const stripe = new Stripe('pk_test_51NNoasBi8p7FeGFrI5SfpM6HuNMoxwImx6NRKyKbgbt6OPxMxQDiZ9I1GqvDa9qUwB3D3jlJOng6MyjPppWofxzP00Exvr8dBy')
|
const stripe = new Stripe(
|
||||||
|
"pk_test_51NNoasBi8p7FeGFrI5SfpM6HuNMoxwImx6NRKyKbgbt6OPxMxQDiZ9I1GqvDa9qUwB3D3jlJOng6MyjPppWofxzP00Exvr8dBy"
|
||||||
|
);
|
||||||
|
|
||||||
const apiUrl =
|
const apiUrl =
|
||||||
process.env.NODE_ENV === "development"
|
process.env.NODE_ENV === "development"
|
||||||
@@ -139,7 +141,11 @@ const SignupPage = () => {
|
|||||||
setShowPaymentModal(true);
|
setShowPaymentModal(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function handleStripeAuthorization(user, paymentDataValues, customerDataValues) {
|
async function handleStripeAuthorization(
|
||||||
|
user,
|
||||||
|
paymentDataValues,
|
||||||
|
customerDataValues
|
||||||
|
) {
|
||||||
// Do: calls to Stripe API
|
// Do: calls to Stripe API
|
||||||
// if success, return some truthy value or
|
// if success, return some truthy value or
|
||||||
// handle failure in handleSignup
|
// handle failure in handleSignup
|
||||||
@@ -154,50 +160,53 @@ const SignupPage = () => {
|
|||||||
exp_month: paymentDataValues.cardExpirationMonth,
|
exp_month: paymentDataValues.cardExpirationMonth,
|
||||||
exp_year: paymentDataValues.cardExpirationYear,
|
exp_year: paymentDataValues.cardExpirationYear,
|
||||||
cvc: paymentDataValues.cardCvvCode,
|
cvc: paymentDataValues.cardCvvCode,
|
||||||
name: paymentDataValues.cardFirstName + ' ' + paymentDataValues.cardLastName,
|
name:
|
||||||
}
|
paymentDataValues.cardFirstName +
|
||||||
})
|
" " +
|
||||||
|
paymentDataValues.cardLastName,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let response = await fetch(`${apiUrl}/create-subscription`, {
|
let response = await fetch(`${apiUrl}/create-subscription`, {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
type: 'monthly', // TODO: replace this with the user-chosen value later when we have the UI for it
|
type: "monthly", // TODO: replace this with the user-chosen value later when we have the UI for it
|
||||||
customerData: {
|
customerData: {
|
||||||
email: customerDataValues.email,
|
email: customerDataValues.email,
|
||||||
name: customerDataValues.firstName + ' ' + customerDataValues.lastName,
|
name:
|
||||||
|
customerDataValues.firstName + " " + customerDataValues.lastName,
|
||||||
},
|
},
|
||||||
token,
|
token,
|
||||||
}),
|
}),
|
||||||
})
|
});
|
||||||
|
|
||||||
response = await response.json()
|
response = await response.json();
|
||||||
|
|
||||||
subscriptionId = response.subscriptionId;
|
subscriptionId = response.subscriptionId;
|
||||||
customerId = response.customerId;
|
customerId = response.customerId;
|
||||||
|
|
||||||
console.log('response', response);
|
console.log("response", response);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
subscriptionId,
|
subscriptionId,
|
||||||
customerId,
|
customerId,
|
||||||
}
|
};
|
||||||
}
|
} catch (error) {
|
||||||
catch (error) {
|
console.log(error);
|
||||||
console.log(error)
|
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(error) {
|
if (error) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
subscriptionId,
|
subscriptionId,
|
||||||
customerId,
|
customerId,
|
||||||
}
|
};
|
||||||
|
|
||||||
return !error;
|
return !error;
|
||||||
}
|
}
|
||||||
@@ -215,39 +224,44 @@ const SignupPage = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setIsBusy(true);
|
setIsBusy(true);
|
||||||
|
console.log("paymentDataValues, dataValues", paymentDataValues, dataValues);
|
||||||
|
if (paymentDataValues && dataValues) {
|
||||||
|
try {
|
||||||
|
const userCredential = await createUserWithEmailAndPassword(
|
||||||
|
auth,
|
||||||
|
dataValues.email,
|
||||||
|
dataValues.password
|
||||||
|
);
|
||||||
|
const user = userCredential.user;
|
||||||
|
|
||||||
try {
|
const paymentResponse = await handleStripeAuthorization(
|
||||||
const userCredential = await createUserWithEmailAndPassword(
|
user,
|
||||||
auth,
|
paymentDataValues,
|
||||||
dataValues.email,
|
dataValues
|
||||||
dataValues.password
|
);
|
||||||
);
|
|
||||||
const user = userCredential.user;
|
|
||||||
|
|
||||||
const paymentResponse = await handleStripeAuthorization(user, paymentDataValues, dataValues);
|
if (paymentResponse === false) {
|
||||||
|
// handle payment failure
|
||||||
|
setIsBusy(false);
|
||||||
|
setNotice("Sorry, something went wrong. Please try again.");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
dataValues = { ...dataValues, ...paymentResponse };
|
||||||
|
|
||||||
if(paymentResponse === false) {
|
await saveUserData(user.uid, dataValues);
|
||||||
// handle payment failure
|
navigate("/");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
setIsBusy(false);
|
setIsBusy(false);
|
||||||
setNotice(
|
setNotice(
|
||||||
"Sorry, something went wrong. Please try again."
|
error.message || "Sorry, something went wrong. Please try again."
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
dataValues = {...dataValues, ...paymentResponse};
|
|
||||||
|
|
||||||
await saveUserData(user.uid, dataValues);
|
|
||||||
navigate("/");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
setIsBusy(false);
|
|
||||||
setNotice(
|
|
||||||
error.message || "Sorry, something went wrong. Please try again."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const showNoticeOnPage = "" !== notice && !showPaymentModal;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="signup-super-container">
|
<div className="signup-super-container">
|
||||||
<div className="signup-sub-container">
|
<div className="signup-sub-container">
|
||||||
@@ -335,13 +349,15 @@ const SignupPage = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{"" !== notice && (
|
{showNoticeOnPage ? (
|
||||||
<>
|
<>
|
||||||
<br></br>
|
<br></br>
|
||||||
<div className="alert alert-danger" role="alert">
|
<div className="alert alert-danger" role="alert">
|
||||||
{notice}
|
{notice}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
|
) : (
|
||||||
|
<></>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{showPaymentModal ? (
|
{showPaymentModal ? (
|
||||||
@@ -353,6 +369,8 @@ const SignupPage = () => {
|
|||||||
handleChangePaymentInput={handleChangePaymentInput}
|
handleChangePaymentInput={handleChangePaymentInput}
|
||||||
handleSignup={handleSignup}
|
handleSignup={handleSignup}
|
||||||
modalText={modalText}
|
modalText={modalText}
|
||||||
|
notice={notice}
|
||||||
|
setNotice={setNotice}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<></>
|
<></>
|
||||||
|
|||||||
Reference in New Issue
Block a user