diff --git a/src/Components/SignupPage/SignupPage.js b/src/Components/SignupPage/SignupPage.js index f72e03a..eb6705b 100644 --- a/src/Components/SignupPage/SignupPage.js +++ b/src/Components/SignupPage/SignupPage.js @@ -21,7 +21,7 @@ import { signupFields } from "../../Constants/Fields/SignupFields"; import { paymentfields } from "../../Constants/Fields/PaymentFields"; import PaymentModal from "../Modals/PaymentModal"; import "../../styles/signup.scss"; -import Stripe from 'stripe'; +import Stripe from "stripe"; const SignupPage = () => { const navigate = useNavigate(); @@ -36,7 +36,9 @@ const SignupPage = () => { const modalText = "Description of monthly and annual subscriptions"; // 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 = process.env.NODE_ENV === "development" @@ -139,7 +141,11 @@ const SignupPage = () => { setShowPaymentModal(true); }; - async function handleStripeAuthorization(user, paymentDataValues, customerDataValues) { + async function handleStripeAuthorization( + user, + paymentDataValues, + customerDataValues + ) { // Do: calls to Stripe API // if success, return some truthy value or // handle failure in handleSignup @@ -154,50 +160,53 @@ const SignupPage = () => { exp_month: paymentDataValues.cardExpirationMonth, exp_year: paymentDataValues.cardExpirationYear, cvc: paymentDataValues.cardCvvCode, - name: paymentDataValues.cardFirstName + ' ' + paymentDataValues.cardLastName, - } - }) + name: + paymentDataValues.cardFirstName + + " " + + paymentDataValues.cardLastName, + }, + }); try { let response = await fetch(`${apiUrl}/create-subscription`, { - method: 'POST', + method: "POST", headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", }, 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: { email: customerDataValues.email, - name: customerDataValues.firstName + ' ' + customerDataValues.lastName, + name: + customerDataValues.firstName + " " + customerDataValues.lastName, }, token, }), - }) + }); - response = await response.json() + response = await response.json(); subscriptionId = response.subscriptionId; customerId = response.customerId; - console.log('response', response); + console.log("response", response); return { subscriptionId, customerId, - } - } - catch (error) { - console.log(error) + }; + } catch (error) { + console.log(error); error = true; } - if(error) { - return false + if (error) { + return false; } return { subscriptionId, customerId, - } + }; return !error; } @@ -215,39 +224,44 @@ const SignupPage = () => { } 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 userCredential = await createUserWithEmailAndPassword( - auth, - dataValues.email, - dataValues.password - ); - const user = userCredential.user; + const paymentResponse = await handleStripeAuthorization( + user, + paymentDataValues, + dataValues + ); - 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) { - // handle payment failure + await saveUserData(user.uid, dataValues); + navigate("/"); + } + } catch (error) { setIsBusy(false); 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 (