@@ -6,7 +6,6 @@ import { Link } from 'react-router-dom';
66import { useForm , Controller } from 'react-hook-form' ;
77import classNames from 'classnames' ;
88import { yupResolver } from '@hookform/resolvers' ;
9- import * as yup from 'yup' ;
109
1110import paths from 'pages/Router/paths' ;
1211import { usersCleanUp } from 'state/actions/users' ;
@@ -16,7 +15,7 @@ import ErrorMessage from 'components/ErrorMessage';
1615
1716import './UserForm.scss' ;
1817
19- const UserForm = ( { isEditing, isProfile, user, setUser , action } ) => {
18+ const UserForm = ( { isEditing, isProfile, user, onSubmitHandler , schema } ) => {
2019 const { loading, success } = useSelector (
2120 ( state ) => ( {
2221 loading : state . users . loading ,
@@ -27,38 +26,18 @@ const UserForm = ({ isEditing, isProfile, user, setUser, action }) => {
2726
2827 const dispatch = useDispatch ( ) ;
2928
30- const schema = yup . object ( ) . shape ( {
31- email : isEditing
32- ? yup . string ( ) . email ( ) . notRequired ( )
33- : yup . string ( ) . email ( ) . required ( ) ,
34- name : yup . string ( ) . required ( ) ,
35- isAdmin : yup . boolean ( ) . notRequired ( ) ,
36- location : yup . string ( ) . notRequired ( ) ,
37- createdAt : yup . string ( ) . required ( ) ,
38- } ) ;
39-
40- const { register, handleSubmit, errors, control, watch } = useForm ( {
29+ const { register, handleSubmit, errors, control, watch, setValue } = useForm ( {
4130 defaultValues : { ...user } ,
4231 resolver : yupResolver ( schema ) ,
4332 } ) ;
4433
4534 useEffect ( ( ) => {
4635 if ( success ) {
47- setUser ( ( prevState ) => ( { ... prevState , file : null } ) ) ;
36+ setValue ( 'file' , null ) ;
4837 }
4938 return ( ) => dispatch ( usersCleanUp ( ) ) ;
5039 } , [ dispatch , success ] ) ;
5140
52- const onSubmitHandler = ( value ) => {
53- const newUser = {
54- ...value ,
55- file : value ?. file [ 0 ] || null ,
56- isEditing,
57- isProfile,
58- } ;
59- dispatch ( action ( newUser ) ) ;
60- } ;
61-
6241 const invalidEmailMessage = useFormatMessage ( 'UserForm.invalidEmail' ) ;
6342
6443 const imagePreviewUrl =
@@ -102,7 +81,8 @@ const UserForm = ({ isEditing, isProfile, user, setUser, action }) => {
10281 type = "text"
10382 readOnly = "readOnly"
10483 className = "input is-static"
105- value = { user . email }
84+ name = "email"
85+ ref = { register }
10686 />
10787 </ div >
10888 </ div >
@@ -421,7 +401,9 @@ UserForm.propTypes = {
421401 createdAt : PropTypes . string . isRequired ,
422402 email : PropTypes . string . isRequired ,
423403 } ) . isRequired ,
424- action : PropTypes . func . isRequired ,
404+ onSubmitHandler : PropTypes . func . isRequired ,
405+ // eslint-disable-next-line react/forbid-prop-types
406+ schema : PropTypes . object . isRequired ,
425407 isEditing : PropTypes . bool ,
426408 isProfile : PropTypes . bool ,
427409} ;
0 commit comments