| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- import React, { useContext, useState } from "react";
- import {
- Stepper,
- Step,
- StepLabel,
- Button,
- Typography,
- CircularProgress,
- } from "@material-ui/core";
- import { Formik, Form } from "formik";
- import AddressForm from "./Forms/AddressForm";
- import PaymentForm from "./Forms/PaymentForm";
- import ReviewOrder from "./ReviewOrder";
- import CheckoutSuccess from "./CheckoutSuccess";
- import api from "../../services/api";
- import toastError from "../../errors/toastError";
- import { toast } from "react-toastify";
- import { AuthContext } from "../../context/Auth/AuthContext";
- import validationSchema from "./FormModel/validationSchema";
- import checkoutFormModel from "./FormModel/checkoutFormModel";
- import formInitialValues from "./FormModel/formInitialValues";
- import useStyles from "./styles";
- import Invoices from "../../pages/Financeiro";
- import { i18n } from "../../translate/i18n";
- export default function CheckoutPage(props) {
- const steps = [i18n.t("checkoutPage.steps.data"), i18n.t("checkoutPage.steps.customize"), i18n.t("checkoutPage.steps.review")];
- const { formId, formField } = checkoutFormModel;
-
-
-
- const classes = useStyles();
- const [activeStep, setActiveStep] = useState(1);
- const [datePayment, setDatePayment] = useState(null);
- const [invoiceId, setinvoiceId] = useState(props.Invoice.id);
- const currentValidationSchema = validationSchema[activeStep];
- const isLastStep = activeStep === steps.length - 1;
- const { user } = useContext(AuthContext);
- function _renderStepContent(step, setFieldValue, setActiveStep, values ) {
- switch (step) {
- case 0:
- return <AddressForm formField={formField} values={values} setFieldValue={setFieldValue} />;
- case 1:
- return <PaymentForm
- formField={formField}
- setFieldValue={setFieldValue}
- setActiveStep={setActiveStep}
- activeStep={step}
- invoiceId={invoiceId}
- values={values}
- />;
- case 2:
- return <ReviewOrder />;
- default:
- return <div>Not Found</div>;
- }
- }
- async function _submitForm(values, actions) {
- try {
- const plan = JSON.parse(values.plan);
- const newValues = {
- firstName: values.firstName,
- lastName: values.lastName,
- address2: values.address2,
- city: values.city,
- state: values.state,
- zipcode: values.zipcode,
- country: values.country,
- useAddressForPaymentDetails: values.useAddressForPaymentDetails,
- nameOnCard: values.nameOnCard,
- cardNumber: values.cardNumber,
- cvv: values.cvv,
- plan: values.plan,
- price: plan.price,
- users: plan.users,
- connections: plan.connections,
- invoiceId: invoiceId
- }
- const { data } = await api.post("/subscription", newValues);
- setDatePayment(data)
- actions.setSubmitting(false);
- setActiveStep(activeStep + 1);
- toast.success(i18n.t("checkoutPage.success"));
- } catch (err) {
- toastError(err);
- }
- }
- function _handleSubmit(values, actions) {
- if (isLastStep) {
- _submitForm(values, actions);
- } else {
- setActiveStep(activeStep + 1);
- actions.setTouched({});
- actions.setSubmitting(false);
- }
- }
- function _handleBack() {
- setActiveStep(activeStep - 1);
- }
- return (
- <React.Fragment>
- <Typography component="h1" variant="h4" align="center">
- {i18n.t("checkoutPage.closeToEnd")}
- </Typography>
- <Stepper activeStep={activeStep} className={classes.stepper}>
- {steps.map((label) => (
- <Step key={label}>
- <StepLabel>{label}</StepLabel>
- </Step>
- ))}
- </Stepper>
- <React.Fragment>
- {activeStep === steps.length ? (
- <CheckoutSuccess pix={datePayment} />
- ) : (
- <Formik
- initialValues={{
- ...user,
- ...formInitialValues
- }}
- validationSchema={currentValidationSchema}
- onSubmit={_handleSubmit}
- >
- {({ isSubmitting, setFieldValue, values }) => (
- <Form id={formId}>
- {_renderStepContent(activeStep, setFieldValue, setActiveStep, values)}
- <div className={classes.buttons}>
- {activeStep !== 1 && (
- <Button onClick={_handleBack} className={classes.button}>
- {i18n.t("checkoutPage.BACK")}
- </Button>
- )}
- <div className={classes.wrapper}>
- {activeStep !== 1 && (
- <Button
- disabled={isSubmitting}
- type="submit"
- variant="contained"
- color="primary"
- className={classes.button}
- >
- {isLastStep ? i18n.t("checkoutPage.PAY") : i18n.t("checkoutPage.NEXT")}
- </Button>
- )}
- {isSubmitting && (
- <CircularProgress
- size={24}
- className={classes.buttonProgress}
- />
- )}
- </div>
- </div>
- </Form>
- )}
- </Formik>
- )}
- </React.Fragment>
- </React.Fragment>
- );
- }
|