CheckoutPage.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React, { useContext, useState } from "react";
  2. import {
  3. Stepper,
  4. Step,
  5. StepLabel,
  6. Button,
  7. Typography,
  8. CircularProgress,
  9. } from "@material-ui/core";
  10. import { Formik, Form } from "formik";
  11. import AddressForm from "./Forms/AddressForm";
  12. import PaymentForm from "./Forms/PaymentForm";
  13. import ReviewOrder from "./ReviewOrder";
  14. import CheckoutSuccess from "./CheckoutSuccess";
  15. import api from "../../services/api";
  16. import toastError from "../../errors/toastError";
  17. import { toast } from "react-toastify";
  18. import { AuthContext } from "../../context/Auth/AuthContext";
  19. import validationSchema from "./FormModel/validationSchema";
  20. import checkoutFormModel from "./FormModel/checkoutFormModel";
  21. import formInitialValues from "./FormModel/formInitialValues";
  22. import useStyles from "./styles";
  23. import Invoices from "../../pages/Financeiro";
  24. import { i18n } from "../../translate/i18n";
  25. export default function CheckoutPage(props) {
  26. const steps = [i18n.t("checkoutPage.steps.data"), i18n.t("checkoutPage.steps.customize"), i18n.t("checkoutPage.steps.review")];
  27. const { formId, formField } = checkoutFormModel;
  28. const classes = useStyles();
  29. const [activeStep, setActiveStep] = useState(1);
  30. const [datePayment, setDatePayment] = useState(null);
  31. const [invoiceId, setinvoiceId] = useState(props.Invoice.id);
  32. const currentValidationSchema = validationSchema[activeStep];
  33. const isLastStep = activeStep === steps.length - 1;
  34. const { user } = useContext(AuthContext);
  35. function _renderStepContent(step, setFieldValue, setActiveStep, values ) {
  36. switch (step) {
  37. case 0:
  38. return <AddressForm formField={formField} values={values} setFieldValue={setFieldValue} />;
  39. case 1:
  40. return <PaymentForm
  41. formField={formField}
  42. setFieldValue={setFieldValue}
  43. setActiveStep={setActiveStep}
  44. activeStep={step}
  45. invoiceId={invoiceId}
  46. values={values}
  47. />;
  48. case 2:
  49. return <ReviewOrder />;
  50. default:
  51. return <div>Not Found</div>;
  52. }
  53. }
  54. async function _submitForm(values, actions) {
  55. try {
  56. const plan = JSON.parse(values.plan);
  57. const newValues = {
  58. firstName: values.firstName,
  59. lastName: values.lastName,
  60. address2: values.address2,
  61. city: values.city,
  62. state: values.state,
  63. zipcode: values.zipcode,
  64. country: values.country,
  65. useAddressForPaymentDetails: values.useAddressForPaymentDetails,
  66. nameOnCard: values.nameOnCard,
  67. cardNumber: values.cardNumber,
  68. cvv: values.cvv,
  69. plan: values.plan,
  70. price: plan.price,
  71. users: plan.users,
  72. connections: plan.connections,
  73. invoiceId: invoiceId
  74. }
  75. const { data } = await api.post("/subscription", newValues);
  76. setDatePayment(data)
  77. actions.setSubmitting(false);
  78. setActiveStep(activeStep + 1);
  79. toast.success(i18n.t("checkoutPage.success"));
  80. } catch (err) {
  81. toastError(err);
  82. }
  83. }
  84. function _handleSubmit(values, actions) {
  85. if (isLastStep) {
  86. _submitForm(values, actions);
  87. } else {
  88. setActiveStep(activeStep + 1);
  89. actions.setTouched({});
  90. actions.setSubmitting(false);
  91. }
  92. }
  93. function _handleBack() {
  94. setActiveStep(activeStep - 1);
  95. }
  96. return (
  97. <React.Fragment>
  98. <Typography component="h1" variant="h4" align="center">
  99. {i18n.t("checkoutPage.closeToEnd")}
  100. </Typography>
  101. <Stepper activeStep={activeStep} className={classes.stepper}>
  102. {steps.map((label) => (
  103. <Step key={label}>
  104. <StepLabel>{label}</StepLabel>
  105. </Step>
  106. ))}
  107. </Stepper>
  108. <React.Fragment>
  109. {activeStep === steps.length ? (
  110. <CheckoutSuccess pix={datePayment} />
  111. ) : (
  112. <Formik
  113. initialValues={{
  114. ...user,
  115. ...formInitialValues
  116. }}
  117. validationSchema={currentValidationSchema}
  118. onSubmit={_handleSubmit}
  119. >
  120. {({ isSubmitting, setFieldValue, values }) => (
  121. <Form id={formId}>
  122. {_renderStepContent(activeStep, setFieldValue, setActiveStep, values)}
  123. <div className={classes.buttons}>
  124. {activeStep !== 1 && (
  125. <Button onClick={_handleBack} className={classes.button}>
  126. {i18n.t("checkoutPage.BACK")}
  127. </Button>
  128. )}
  129. <div className={classes.wrapper}>
  130. {activeStep !== 1 && (
  131. <Button
  132. disabled={isSubmitting}
  133. type="submit"
  134. variant="contained"
  135. color="primary"
  136. className={classes.button}
  137. >
  138. {isLastStep ? i18n.t("checkoutPage.PAY") : i18n.t("checkoutPage.NEXT")}
  139. </Button>
  140. )}
  141. {isSubmitting && (
  142. <CircularProgress
  143. size={24}
  144. className={classes.buttonProgress}
  145. />
  146. )}
  147. </div>
  148. </div>
  149. </Form>
  150. )}
  151. </Formik>
  152. )}
  153. </React.Fragment>
  154. </React.Fragment>
  155. );
  156. }