1- import React , { useState } from 'react' ;
1+ import React , { useState , useEffect } from 'react' ;
22import { Box , Typography , TextField , Button , Modal , Select , MenuItem } from '@mui/material' ;
33import PropTypes from 'prop-types' ;
44
@@ -15,9 +15,10 @@ const modalStyle = {
1515const EditRecipientModal = ( { open, onClose, profile, onChange, onSubmit } ) => {
1616 const [ error , setError ] = useState ( '' ) ;
1717
18- const handleSubmit = ( ) => {
18+ const handleSubmit = ( event ) => {
19+ event . preventDefault ( ) ;
1920 const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / ;
20- if ( ! profile . firstName || ! profile . lastName || ! profile . email || ! profile . companyName ) {
21+ if ( ! profile . firstName || ! profile . lastName || ! profile . email ) {
2122 setError ( 'All fields must be filled' ) ;
2223 return ;
2324 }
@@ -31,71 +32,70 @@ const EditRecipientModal = ({ open, onClose, profile, onChange, onSubmit }) => {
3132
3233 return (
3334 < >
34- { profile ? (
35+ { profile ? (
3536 < Modal open = { open } onClose = { onClose } >
3637 < Box sx = { { ...modalStyle , width : 400 } } >
3738 < Typography variant = "h6" component = "h2" >
3839 Edit Recipient Profile
3940 </ Typography >
40- < TextField
41- label = "First Name"
42- name = "firstName "
43- value = { profile . firstName }
44- onChange = { onChange }
45- fullWidth
46- margin = "normal"
47- error = { ! ! error && ! profile . firstName }
48- helperText = { ! ! error && ! profile . firstName ? 'First Name is required' : '' }
49- />
50- < TextField
51- label = "Last Name"
52- name = "lastName "
53- value = { profile . lastName }
54- onChange = { onChange }
55- fullWidth
56- margin = "normal"
57- error = { ! ! error && ! profile . lastName }
58- helperText = { ! ! error && ! profile . lastName ? 'Last Name is required' : '' }
59- />
60- < TextField
61- label = "Email"
62- name = "email "
63- value = { profile . email }
64- onChange = { onChange }
65- fullWidth
66- margin = "normal"
67- error = { ! ! error && ( ! profile . email || error === 'Invalid email address' ) }
68- helperText = { ! ! error && ( ! profile . email ? 'Email is required' : error === 'Invalid email address' ? 'Invalid email address' : '' ) }
69- />
70- < TextField
71- label = "Company Name"
72- name = "companyName "
73- value = { profile . companyName }
74- onChange = { onChange }
75- fullWidth
76- margin = "normal"
77- error = { ! ! error && ! profile . companyName }
78- helperText = { ! ! error && ! profile . companyName ? 'Company Name is required' : '' }
79- />
80- < Select
81- label = "Status "
82- name = " inactive"
83- value = { profile . inactive ? 'Inactive' : 'Active' }
84- onChange = { ( e ) => onChange ( { target : { name : 'inactive' , value : e . target . value === 'Inactive' } } ) }
85- fullWidth
86- margin = "normal"
87- >
88- < MenuItem value = "Active" > Active </ MenuItem >
89- < MenuItem value = "Inactive" > Inactive </ MenuItem >
90- </ Select >
91- < Box mt = { 2 } / >
92- < Button onClick = { handleSubmit } variant = "contained" color = "primary" >
93- Save
94- </ Button >
41+ < form onSubmit = { handleSubmit } >
42+ < TextField
43+ label = "Email "
44+ name = "email"
45+ value = { profile . email }
46+ onChange = { onChange }
47+ fullWidth
48+ margin = "normal"
49+ error = { ! ! error && ( ! profile . email || error === 'Invalid email address' ) }
50+ helperText = { ! ! error && ( ! profile . email ? 'Email is required' : error === 'Invalid email address' ? 'Invalid email address' : '' ) }
51+ />
52+ < TextField
53+ label = "First Name "
54+ name = "firstName"
55+ value = { profile . firstName }
56+ onChange = { onChange }
57+ fullWidth
58+ margin = "normal"
59+ error = { ! ! error && ! profile . firstName }
60+ helperText = { ! ! error && ! profile . firstName ? 'First Name is required' : '' }
61+ />
62+ < TextField
63+ label = "Last Name "
64+ name = "lastName"
65+ value = { profile . lastName }
66+ onChange = { onChange }
67+ fullWidth
68+ margin = "normal"
69+ error = { ! ! error && ! profile . lastName }
70+ helperText = { ! ! error && ! profile . lastName ? 'Last Name is required' : '' }
71+ />
72+ < TextField
73+ label = "Company Name "
74+ name = " companyName"
75+ value = { profile . companyName }
76+ onChange = { onChange }
77+ fullWidth
78+ margin = "normal"
79+ />
80+ < Select
81+ label = "Status"
82+ name = "inactive "
83+ value = { profile . inactive ? 'Inactive' : 'Active' }
84+ onChange = { ( e ) => onChange ( { target : { name : 'inactive' , value : e . target . value === 'Inactive' } } ) }
85+ fullWidth
86+ margin = "normal"
87+ >
88+ < MenuItem value = "Active" > Active </ MenuItem >
89+ < MenuItem value = "Inactive" > Inactive </ MenuItem >
90+ </ Select >
91+ < Box mt = { 2 } / >
92+ < Button type = "submit" variant = "contained" color = "primary" >
93+ Save
94+ </ Button >
95+ </ form >
9596 </ Box >
9697 </ Modal >
97- ) : null
98- }
98+ ) : null }
9999 </ >
100100 ) ;
101101} ;
0 commit comments