PaymentForm.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, { useState, useEffect, useReducer } from 'react';
  2. import Button from '@material-ui/core/Button';
  3. import Card from '@material-ui/core/Card';
  4. import CardActions from '@material-ui/core/CardActions';
  5. import CardContent from '@material-ui/core/CardContent';
  6. import CardHeader from '@material-ui/core/CardHeader';
  7. import Grid from '@material-ui/core/Grid';
  8. import StarIcon from '@material-ui/icons/StarBorder';
  9. import Typography from '@material-ui/core/Typography';
  10. import { makeStyles } from '@material-ui/core/styles';
  11. import IconButton from '@material-ui/core/IconButton';
  12. import MinimizeIcon from '@material-ui/icons/Minimize';
  13. import AddIcon from '@material-ui/icons/Add';
  14. import usePlans from "../../../hooks/usePlans";
  15. import useCompanies from "../../../hooks/useCompanies";
  16. import { i18n } from '../../../translate/i18n';
  17. const useStyles = makeStyles((theme) => ({
  18. '@global': {
  19. ul: {
  20. margin: 0,
  21. padding: 0,
  22. listStyle: 'none',
  23. },
  24. },
  25. margin: {
  26. margin: theme.spacing(1),
  27. },
  28. cardHeader: {
  29. backgroundColor:
  30. theme.palette.type === 'light' ? theme.palette.grey[200] : theme.palette.grey[700],
  31. },
  32. cardPricing: {
  33. display: 'flex',
  34. justifyContent: 'center',
  35. alignItems: 'baseline',
  36. marginBottom: theme.spacing(2),
  37. },
  38. footer: {
  39. borderTop: `1px solid ${theme.palette.divider}`,
  40. marginTop: theme.spacing(8),
  41. paddingTop: theme.spacing(3),
  42. paddingBottom: theme.spacing(3),
  43. [theme.breakpoints.up('sm')]: {
  44. paddingTop: theme.spacing(6),
  45. paddingBottom: theme.spacing(6),
  46. },
  47. },
  48. customCard: {
  49. display: "flex",
  50. marginTop: "16px",
  51. alignItems: "center",
  52. flexDirection: "column",
  53. },
  54. custom: {
  55. display: "flex",
  56. alignItems: "center",
  57. justifyContent: "space-between",
  58. }
  59. }));
  60. export default function Pricing(props) {
  61. const {
  62. setFieldValue,
  63. setActiveStep,
  64. activeStep,
  65. } = props;
  66. const handleChangeAdd = (event, newValue) => {
  67. if (newValue < 3) return
  68. const newPrice = 11.00;
  69. setUsersPlans(newValue);
  70. setCustomValuePlans(customValuePlans + newPrice);
  71. }
  72. const handleChangeMin = (event, newValue) => {
  73. if (newValue < 3) return
  74. const newPrice = 11;
  75. setUsersPlans(newValue);
  76. setCustomValuePlans(customValuePlans - newPrice);
  77. }
  78. const handleChangeConnectionsAdd = (event, newValue) => {
  79. if (newValue < 3) return
  80. const newPrice = 20.00;
  81. setConnectionsPlans(newValue);
  82. setCustomValuePlans(customValuePlans + newPrice);
  83. }
  84. const handleChangeConnectionsMin = (event, newValue) => {
  85. if (newValue < 3) return
  86. const newPrice = 20;
  87. setConnectionsPlans(newValue);
  88. setCustomValuePlans(customValuePlans - newPrice);
  89. }
  90. const { list, finder } = usePlans();
  91. const { find } = useCompanies();
  92. const classes = useStyles();
  93. const [usersPlans, setUsersPlans] = React.useState(3);
  94. const [companiesPlans, setCompaniesPlans] = useState(0);
  95. const [connectionsPlans, setConnectionsPlans] = React.useState(3);
  96. const [storagePlans, setStoragePlans] = React.useState([]);
  97. const [customValuePlans, setCustomValuePlans] = React.useState(49.00);
  98. const [loading, setLoading] = React.useState(false);
  99. const companyId = localStorage.getItem("companyId");
  100. useEffect(() => {
  101. async function fetchData() {
  102. await loadCompanies();
  103. }
  104. fetchData();
  105. }, [])
  106. const loadCompanies = async () => {
  107. setLoading(true);
  108. try {
  109. const companiesList = await find(companyId);
  110. setCompaniesPlans(companiesList.planId);
  111. await loadPlans(companiesList.planId);
  112. } catch (e) {
  113. console.log(e);
  114. // toast.error("Não foi possível carregar a lista de registros");
  115. }
  116. setLoading(false);
  117. };
  118. const loadPlans = async (companiesPlans) => {
  119. setLoading(true);
  120. try {
  121. const plansCompanies = await finder(companiesPlans);
  122. const plans = []
  123. //plansCompanies.forEach((plan) => {
  124. plans.push({
  125. title: plansCompanies.name,
  126. planId: plansCompanies.id,
  127. price: plansCompanies.value,
  128. description: [
  129. `${plansCompanies.users} ${i18n.t("checkoutPage.pricing.users")}`,
  130. `${plansCompanies.connections} ${i18n.t("checkoutPage.pricing.connection")}`,
  131. `${plansCompanies.queues} ${i18n.t("checkoutPage.pricing.queues")}`
  132. ],
  133. users: plansCompanies.users,
  134. connections: plansCompanies.connections,
  135. queues: plansCompanies.queues,
  136. buttonText: i18n.t("checkoutPage.pricing.SELECT"),
  137. buttonVariant: 'outlined',
  138. })
  139. // setStoragePlans(data);
  140. //});
  141. setStoragePlans(plans);
  142. } catch (e) {
  143. console.log(e);
  144. // toast.error("Não foi possível carregar a lista de registros");
  145. }
  146. setLoading(false);
  147. };
  148. const tiers = storagePlans
  149. return (
  150. <React.Fragment>
  151. <Grid container spacing={3}>
  152. {tiers.map((tier) => (
  153. // Enterprise card is full width at sm breakpoint
  154. <Grid item key={tier.title} xs={12} sm={tier.title === 'Enterprise' ? 12 : 12} md={12}>
  155. <Card>
  156. <CardHeader
  157. title={tier.title}
  158. subheader={tier.subheader}
  159. titleTypographyProps={{ align: 'center' }}
  160. subheaderTypographyProps={{ align: 'center' }}
  161. action={tier.title === 'Pro' ? <StarIcon /> : null}
  162. className={classes.cardHeader}
  163. />
  164. <CardContent>
  165. <div className={classes.cardPricing}>
  166. <Typography component="h2" variant="h3" color="textPrimary">
  167. {
  168. <React.Fragment>
  169. R${tier.price.toLocaleString('pt-br', { minimumFractionDigits: 2 })}
  170. </React.Fragment>
  171. }
  172. </Typography>
  173. <Typography variant="h6" color="textSecondary">
  174. /{i18n.t("checkoutPage.pricing.month")}
  175. </Typography>
  176. </div>
  177. <ul>
  178. {tier.description.map((line) => (
  179. <Typography component="li" variant="subtitle1" align="center" key={line}>
  180. {line}
  181. </Typography>
  182. ))}
  183. </ul>
  184. </CardContent>
  185. <CardActions>
  186. <Button
  187. fullWidth
  188. variant={tier.buttonVariant}
  189. color="primary"
  190. onClick={() => {
  191. if (tier.custom) {
  192. setFieldValue("plan", JSON.stringify({
  193. users: usersPlans,
  194. connections: connectionsPlans,
  195. price: customValuePlans,
  196. }));
  197. } else {
  198. setFieldValue("plan", JSON.stringify(tier));
  199. }
  200. setActiveStep(activeStep + 1);
  201. }
  202. }
  203. >
  204. {tier.buttonText}
  205. </Button>
  206. </CardActions>
  207. </Card>
  208. </Grid>
  209. ))}
  210. </Grid>
  211. </React.Fragment>
  212. );
  213. }