AddressForm.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { useContext, useEffect, useState } from "react";
  2. import { Grid, Typography } from "@material-ui/core";
  3. import { InputField, SelectField } from "../../FormFields";
  4. import { AuthContext } from "../../../context/Auth/AuthContext";
  5. const countries = [
  6. {
  7. value: "BR",
  8. label: "Brasil",
  9. },
  10. {
  11. value: "usa",
  12. label: "United States",
  13. },
  14. ];
  15. export default function AddressForm(props) {
  16. const { user } = useContext(AuthContext);
  17. const [billingName, setBillingName] = useState(user.company.billingName);
  18. const [addressZipCode, setAddressZipCode] = useState(user.company.addressZipCode);
  19. const [addressStreet, setAddressStreet] = useState(user.company.addressStreet);
  20. const [addressState, setAddressState] = useState(user.company.addressState);
  21. const [addressCity, setAddressCity] = useState(user.company.addressCity);
  22. const [addressDistrict, setAddressDistrict] = useState(user.company.addressDistrict);
  23. const {
  24. formField: {
  25. firstName,
  26. address1,
  27. city,
  28. state,
  29. zipcode,
  30. country,
  31. },
  32. setFieldValue
  33. } = props;
  34. useEffect(() => {
  35. setFieldValue("firstName", billingName)
  36. setFieldValue("zipcode", addressZipCode)
  37. setFieldValue("address2", addressStreet)
  38. setFieldValue("state", addressState)
  39. setFieldValue("city", addressCity)
  40. setFieldValue("country", addressDistrict)
  41. // eslint-disable-next-line react-hooks/exhaustive-deps
  42. }, [])
  43. return (
  44. <React.Fragment>
  45. <Typography variant="h6" gutterBottom>
  46. Vamos precisar de algumas informações
  47. </Typography>
  48. <Grid container spacing={3}>
  49. <Grid item xs={6} sm={6}>
  50. <InputField name={firstName.name} label={firstName.label} fullWidth
  51. value={billingName}
  52. onChange={(e) => {
  53. setBillingName(e.target.value)
  54. setFieldValue("firstName", e.target.value)
  55. }}
  56. />
  57. </Grid>
  58. <Grid item xs={6} sm={6}>
  59. <SelectField
  60. name={country.name}
  61. label={country.label}
  62. data={countries}
  63. fullWidth
  64. value={addressDistrict}
  65. onChange={(e) => {
  66. setAddressDistrict(e.target.value)
  67. setFieldValue("country", e.target.value)
  68. }
  69. }
  70. />
  71. </Grid>
  72. <Grid item xs={4}>
  73. <InputField
  74. name={zipcode.name}
  75. label={zipcode.label}
  76. fullWidth
  77. value={addressZipCode}
  78. onChange={(e) => {
  79. setAddressZipCode(e.target.value)
  80. setFieldValue("zipcode", e.target.value)
  81. }}
  82. />
  83. </Grid>
  84. <Grid item xs={8}>
  85. <InputField
  86. name={address1.name}
  87. label={address1.label}
  88. fullWidth
  89. value={addressStreet}
  90. onChange={(e) => {
  91. setAddressStreet(e.target.value)
  92. setFieldValue("address2", e.target.value)
  93. }}
  94. />
  95. </Grid>
  96. <Grid item xs={4}>
  97. <InputField
  98. name={state.name}
  99. label={state.label}
  100. fullWidth
  101. value={addressState}
  102. onChange={(e) => {
  103. setAddressState(e.target.value)
  104. setFieldValue("state", e.target.value)
  105. }}
  106. />
  107. </Grid>
  108. <Grid item xs={8}>
  109. <InputField
  110. name={city.name}
  111. label={city.label}
  112. fullWidth
  113. value={addressCity}
  114. onChange={(e) => {
  115. setAddressCity(e.target.value)
  116. setFieldValue("city", e.target.value)
  117. }}
  118. />
  119. </Grid>
  120. </Grid>
  121. </React.Fragment>
  122. );
  123. }