1- import React , { useState , useCallback } from 'react' ;
2- import { message } from 'antd' ;
1+ import React , { useRef , useState , useCallback } from 'react' ;
2+ import * as Yup from 'yup' ;
3+ import { Form } from '@unform/web' ;
4+ import { FormHandles } from '@unform/core' ;
35
46import { useAuth } from 'contexts/auth' ;
7+ import getValidationErrors from 'utils/getValidationErrors' ;
58
6- import Input from 'components/Input/Input ' ;
9+ import InputForm from 'components/InputForm ' ;
710import Button from 'components/Button/Button' ;
811import Select from './components/Select' ;
912
@@ -14,31 +17,40 @@ import InviteConfirmationModal from './components/InviteConfirmationModal';
1417function InviteUsers ( ) {
1518 const { inviteUser } = useAuth ( ) ;
1619
20+ const formRef = useRef < FormHandles > ( null ) ;
21+
1722 const [ openInvitationModal , setOpenInvitationModal ] = useState < boolean > (
1823 false ,
1924 ) ;
2025
2126 const [ userPermission , setUserPermission ] = useState < string > ( 'Usuário' ) ;
22- const [ userName , setUserName ] = useState < string > ( '' ) ;
23- const [ userEmail , setUserEmail ] = useState < string > ( '' ) ;
2427
2528 const handleInviteUser = useCallback (
26- e => {
27- e . preventDefault ( ) ;
28-
29- if ( ! userName || ! userEmail ) {
30- message . error ( 'Todos os campos devem estar preenchidos' ) ;
31- return ;
29+ async ( data : { name : string ; email : string } ) => {
30+ try {
31+ formRef . current ?. setErrors ( { } ) ;
32+
33+ const schema = Yup . object ( ) . shape ( {
34+ email : Yup . string ( )
35+ . required ( 'O email é obrigatório' )
36+ . email ( 'Digite um email valído' ) ,
37+ name : Yup . string ( ) . required ( 'O nome é obrigatório' ) ,
38+ } ) ;
39+
40+ await schema . validate ( data , { abortEarly : false } ) ;
41+
42+ setOpenInvitationModal ( true ) ;
43+ inviteUser ( {
44+ name : data . name ,
45+ email : data . email ,
46+ type : userPermission ,
47+ } ) ;
48+ } catch ( err ) {
49+ const errors = getValidationErrors ( err ) ;
50+ formRef . current ?. setErrors ( errors ) ;
3251 }
33-
34- setOpenInvitationModal ( true ) ;
35- inviteUser ( {
36- name : userName ,
37- email : userEmail ,
38- type : userPermission ,
39- } ) ;
4052 } ,
41- [ inviteUser , userName , userEmail , userPermission ] ,
53+ [ inviteUser , userPermission ] ,
4254 ) ;
4355
4456 return (
@@ -50,22 +62,14 @@ function InviteUsers() {
5062 < StyleInviteUsers >
5163 < h1 > Convidar Participante</ h1 >
5264
53- < form className = "emailAndPermission" onSubmit = { handleInviteUser } >
54- < Input
55- title = "Nome"
56- color = "black"
57- Size = "large"
58- styleWidth = "41.875rem"
59- onChange = { event => setUserName ( event . target . value ) }
60- />
65+ < Form
66+ ref = { formRef }
67+ className = "emailAndPermission"
68+ onSubmit = { handleInviteUser }
69+ >
70+ < InputForm title = "Nome" name = "name" color = "black" />
6171
62- < Input
63- title = "E-mail"
64- color = "black"
65- Size = "large"
66- styleWidth = "41.875rem"
67- onChange = { event => setUserEmail ( event . target . value ) }
68- />
72+ < InputForm title = "E-mail" name = "email" color = "black" />
6973
7074 < Select
7175 title = "Nível de permissão"
@@ -81,7 +85,7 @@ function InviteUsers() {
8185 >
8286 Convidar
8387 </ Button >
84- </ form >
88+ </ Form >
8589 </ StyleInviteUsers >
8690 </ >
8791 ) ;
0 commit comments