Guest User

index.js

a guest
Aug 21st, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import {
  3. Grid,
  4. Typography,
  5. Paper,
  6. TextField,
  7. Button,
  8. } from '@material-ui/core';
  9. import Header from '../../Header';
  10. import Footer from '../../Footer';
  11. import LeftMenu from '../LeftMenu';
  12. import TopSnackbar from 'components/TopSnackBar';
  13. import { connect } from 'react-redux'
  14. //styles
  15. import './styles.scss'
  16.  
  17. //service
  18. import userService from 'services/userService'
  19.  
  20. //formulario
  21. import { Formik } from 'formik';
  22. import * as yup from 'yup';
  23. import MaskedInput from 'react-text-mask';
  24. import { cpfRegex, phonRegex, nameRegex } from 'components/FormattedInput/Regex'
  25.  
  26.  
  27. let schema = yup.object().shape({
  28. name: yup
  29. .string()
  30. .matches(nameRegex, 'Nome inválido, digite apenas letras')
  31. .required('Nome é obrigatório')
  32. .min(5, 'Digite o seu nome completo'),
  33. email: yup
  34. .string()
  35. .email('Digite um email válido')
  36. .required('E-mail é obrigatório'),
  37. cpf: yup
  38. .string()
  39. .required('CPF é obrigatório')
  40. .matches(cpfRegex, 'CPF incompleto'),
  41. phone: yup
  42. .string()
  43. .required('Celular é obrigatório')
  44. .matches(phonRegex, 'Telefone incompleto'),
  45. });
  46.  
  47. class PersonalData extends Component {
  48. constructor(props){
  49. super(props)
  50. this.state = {
  51. openSnackbar: false
  52. }
  53. }
  54.  
  55. TextMaskCustom = props => {
  56. const { inputRef, ...other } = props;
  57.  
  58. return (
  59. <MaskedInput
  60. {...other}
  61. mask={[
  62. /[0-9]/,
  63. /\d/,
  64. /\d/,
  65. '.',
  66. /\d/,
  67. /\d/,
  68. /\d/,
  69. '.',
  70. /\d/,
  71. /\d/,
  72. /\d/,
  73. '-',
  74. /\d/,
  75. /\d/
  76. ]}
  77. placeholderChar={'\u2000'}
  78. showMask={false}
  79. />
  80. );
  81. };
  82.  
  83. NumberMaskCustom = props => {
  84. const { inputRef, ...other } = props;
  85.  
  86. return (
  87. <MaskedInput
  88. {...other}
  89. ref={ref => {
  90. inputRef(ref ? ref.inputElement : null);
  91. }}
  92. mask={[
  93. '(',
  94. /[0-9]/,
  95. /\d/,
  96. ')',
  97. ' ',
  98. /\d/,
  99. /\d/,
  100. /\d/,
  101. /\d/,
  102. /\d/,
  103. '-',
  104. /\d/,
  105. /\d/,
  106. /\d/,
  107. /\d/
  108. ]}
  109. placeholderChar={'\u2000'}
  110. showMask={false}
  111. />
  112. );
  113. };
  114.  
  115. sendform = async (user) => {
  116. let response = await userService.userUpdate('5d2f38bd9786551ce58ef8ff', user)
  117. return response
  118.  
  119. }
  120. renderSnackBar = () => {
  121. if(this.state.openSnackbar === true) {
  122. return (
  123. <TopSnackbar
  124. message="Dados trocados com sucesso!"
  125. variant="sucess"
  126. />
  127. )
  128. }
  129. return (<div/>)
  130. }
  131. render() {
  132. const {auth} = this.props.authState
  133. return (
  134. <Grid>
  135. <Header/>
  136. <div className="PersonalData">
  137. <Grid className="content" container spacing={2}>
  138. {this.renderSnackBar()}
  139. <LeftMenu/>
  140. <Grid item xs={9} md={9} lg={9} >
  141. <Formik
  142. initialValues={{ name: '', email: auth.email, cpf: auth.cpf, phone: auth.phone }}
  143. onSubmit={(values) => {
  144. setTimeout(() => {
  145.  
  146. console.log(values)
  147. this.sendform(values)
  148. this.setState({ openSnackbar: true })
  149. }, 400);
  150. }}
  151. validationSchema={schema}
  152. >
  153. {({
  154. values,
  155. errors,
  156. touched,
  157. handleChange,
  158. handleBlur,
  159. handleSubmit,
  160. isSubmitting
  161. /* and other goodies */
  162. }) => (
  163. <form autoComplete="off" onSubmit={handleSubmit} style={{ height: '100%' }}>
  164. <Paper className="paper" elevation={2} style={{borderRadius:10}}>
  165. <Grid container>
  166. <Grid className="personal-data-header" container>
  167. <Grid item xs={12}>
  168. <Typography className="subtitle">Dados pessoais</Typography>
  169. </Grid>
  170. <Grid item xs={12}>
  171. <Typography paragraph className="title">
  172. Edite ou complete os seus dados pessoais
  173. </Typography>
  174. </Grid>
  175. </Grid>
  176.  
  177. <Grid className="form-personal-data-container" container>
  178. <Grid className="input-grid-right" item xs={6}>
  179. <TextField
  180. error = {errors.name && touched.name && errors.name !== '' ? true : false}
  181. fullWidth
  182. label="Como gostaria de ser chamad o ou chamada?"
  183. name="name"
  184. onBlur={handleBlur}
  185. onChange={handleChange}
  186. value={values.name}
  187. variant="outlined"
  188. />
  189. <Typography variant="subtitle2">
  190. {errors.name && touched.name && errors.name}
  191. </Typography>
  192. </Grid>
  193. <Grid className="input-grid-left" item xs={6}>
  194. <TextField
  195. error = {errors.cpf && touched.cpf && errors.cpf !== '' ? true : false}
  196. fullWidth
  197. InputLabelProps={{ className: 'input-label' }}
  198. InputProps={{
  199. inputComponent: this.TextMaskCustom
  200. }}
  201. label="CPF"
  202. name="cpf"
  203. onBlur={handleBlur}
  204. onChange={handleChange}
  205. value={values.cpf}
  206. variant="outlined"
  207. />
  208. <Typography variant="subtitle2">
  209. {errors.cpf && touched.cpf && errors.cpf}
  210. </Typography>
  211. </Grid>
  212. <Grid className="input-grid-right" item xs={6}>
  213. <TextField
  214. error = {errors.email && touched.email && errors.email !== '' ? true : false}
  215. fullWidth
  216. label="E-mail"
  217. name="email"
  218. onBlur={handleBlur}
  219. onChange={handleChange}
  220. value={values.email}
  221. variant="outlined"
  222. />
  223. <Typography variant="subtitle2">
  224. {errors.email && touched.email && errors.email}
  225. </Typography>
  226. </Grid>
  227.  
  228. <Grid className="input-grid-left" item xs={6}>
  229. <TextField
  230. error = {errors.phone && touched.phone && errors.phone !== '' ? true : false}
  231. fullWidth
  232. InputProps={{
  233. inputComponent: this.NumberMaskCustom
  234. }}
  235. label="Celular"
  236. name="phone"
  237. onBlur={handleBlur}
  238. onChange={handleChange}
  239. value={values.phone}
  240. variant="outlined"
  241. />
  242. <Typography variant="subtitle2">
  243. {errors.phone && touched.phone && errors.phone}
  244. </Typography>
  245. </Grid>
  246. <Grid className="send-grid" item xs={12}>
  247. <Button
  248. variant="outlined"
  249. size="large"
  250. type="submit"
  251. >
  252. Salvar
  253. </Button>
  254. </Grid>
  255. </Grid>
  256. </Grid>
  257.  
  258. </Paper>
  259. </form>
  260. )}
  261. </Formik>
  262. </Grid>
  263.  
  264. </Grid>
  265. </div>
  266. <Footer/>
  267. </Grid>
  268. )
  269. }
  270. }
  271. const mapStateToProps = state => {
  272. console.log(state)
  273. return {
  274. authState: state,
  275. }
  276. }
  277. export default connect(mapStateToProps)(PersonalData)
Advertisement
Add Comment
Please, Sign In to add comment