I have a problem resetting the inputs from “AddressForm” component and only when they are containing data from “deviceAddress”, others are working fine when handleReset() is triggered.
Here are my components:
The parent component:
const styles = (theme: Theme) =>
createStyles({
root: {
margin: 0,
padding: 16,
},
closeButton: {
position: 'absolute',
color: '#6b718b',
right: 16,
top: 24,
},
});
export interface ModalFormResponseModel {
success: boolean;
content: string;
}
export interface DialogTitleProps extends WithStyles<typeof styles> {
id: string;
onClose: () => void;
}
const DialogTitle = withStyles(styles)((props: DialogTitleProps) => {
const { classes, onClose, ...other } = props;
return (
<MuiDialogTitle disableTypography className={classes.root} {...other}>
{onClose ? (
<IconButton aria-label="close" className={classes.closeButton} onClick={onClose}>
<span className="uk-icon" uk-icon="close" style={{ width: 18, color: '#212d40' }}></span>
</IconButton>
) : null}
</MuiDialogTitle>
);
});
const SaveButton = withStyles((theme: Theme) => ({
root: {
color: 'white',
backgroundColor: '#212d40',
borderRadius: 0,
margin: 16,
textTransform: 'none',
fontFamily: 'Inter',
fontSize: 16,
fontWeight: 500,
padding: '10px 32px',
'&:hover': {
backgroundColor: '#212d40',
},
},
}))(Button);
const ResetButton = withStyles((theme: Theme) => ({
root: {
color: '#6b718b',
backgroundColor: 'transparent',
borderRadius: 0,
margin: 16,
textTransform: 'none',
fontFamily: 'Inter',
fontSize: 16,
fontWeight: 500,
padding: '10px 16px',
boxShadow: 'none',
'&:hover': {
backgroundColor: 'transparent',
boxShadow: 'none',
},
},
}))(Button);
const DialogActions = withStyles((theme: Theme) => ({
root: {
margin: 0,
padding: theme.spacing(1),
backgroundColor: '#f8f8fa',
},
}))(MuiDialogActions);
function WidgetCustomerData(props): JSX.Element {
const { customer, open, deviceAddress } = props;
const [customerData, setCustomerData] = useState(customer);
const [deviceLocationAddress, setdeviceLocationAddress] = useState(deviceAddress)
const { t } = useTranslation();
const handleClose = () => {
props.handleClose?.();
};
const handleSubmit = async (e) => {
e && e.preventDefault();
const formUrl = 'https://fetch.mock/m-widget-customer-data/addCustomerData';
const formData = new FormData(e.target);
const response = await fetch(formUrl.toString(), {
method: 'POST',
body: formData,
credentials: 'same-origin',
});
const data = await response.json();
};
const handleReset = () => {
setCustomerData(customer);
setdeviceLocationAddress(deviceAddress);
};
const handleChange = (e) => {
const fieldName = e.target.name.split(".");
const value = e.target.value;
console.log('fieldname', fieldName)
if (fieldName.length === 2) {
setCustomerData((prevState) => ({
...prevState,
[fieldName[0]]: {
...prevState[fieldName[0]],
[fieldName[1]]: value,
},
}));
} else if (fieldName[0] === 'address' && deviceAddress && deviceAddress.hasOwnProperty(fieldName[1])) {
setCustomerData((prevState) => ({
...prevState,
address: {
...prevState.address,
[fieldName[1]]: deviceAddress[fieldName[1]],
},
}));
} else {
setCustomerData((prevState) => ({
...prevState,
[fieldName[0]]: value,
}));
}
};
return (
<div className="m-widget-customer-data">
<Dialog
onClose={handleClose}
open={open}
aria-labelledby="customized-dialog-title"
PaperProps={{
style: { borderRadius: 20, minWidth: '80%', maxHeight: 'fit-content' },
}}>
<DialogTitle id="customized-dialog-title" onClose={handleClose} />
<Typography style={{ marginTop: 20, paddingLeft: 48, fontSize: 32, fontWeight: 600 }}>{t('Edit Customer Data')}</Typography>
<form onSubmit={handleSubmit}>
<div style={{ paddingLeft: 48, paddingRight: 48 }} onChange={handleChange}>
<CustomerData customer={customerData} deviceAddress={deviceLocationAddress} />
</div>
<DialogActions>
<ResetButton onClick={handleReset} variant="contained" color="primary">
{t('Reset')}
</ResetButton>
<SaveButton type="submit" variant="contained" color="primary">
{t('Save')}
</SaveButton>
</DialogActions>
</form>
</Dialog>
</div>
);
}
export default WidgetCustomerData;
CustomerData component:
export default ({nextStepAction, customer, handleChange, deviceAddress }: MoleculeSystemManagementCustomerDataProps): JSX.Element => {
const [addressIsDifferent, setAddressIsDifferent] = useState(true);
const { t } = useTranslation();
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if(nextStepAction) nextStepAction(new FormData(e.target as HTMLFormElement));
}
return (
<div className='m-customer-data'>
<CustomerForm customer={customer} handleChange={handleChange} />
<AddressForm formId='customer' formGroupHeader='Residence' customer={customer} handleChange={handleChange} />
<Checkbox label={t("Device address is different than customer address")} id="addressIsDifferent" checked={addressIsDifferent} onChange={(e) => {
setAddressIsDifferent(e.target.checked)
}} />
{addressIsDifferent && <AddressForm formId='device' formGroupHeader='Customer Data System Location' customer={customer} deviceAddress={deviceAddress} handleChange={handleChange} />}
</div>
);
}
AddressForm component:
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Grid } from '@material-ui/core';
import InputField from '/components/atoms/a-input/react/components/a-input.component';
import { DeviceOwner } from '/global/js/modules/SystemManagement/types/DeviceOwner';
import { useStyles } from '../hooks/useStyles';
import { LocationAddress } from '/global/js/modules/SystemManagement/types/LocationAddress';
export interface AddressFormProps {
formId: string;
formGroupHeader: string;
customer: DeviceOwner;
deviceAddress?: LocationAddress;
handleChange?: () => void;
}
function AddressForm({ formId, formGroupHeader, customer, deviceAddress, handleChange }: AddressFormProps): JSX.Element {
const classes = useStyles();
const { t } = useTranslation();
const {
address: { street, house_number, city, zip_code },
} = customer ?? {};
const { street: device_street, house_number: device_house_number, city: device_city, zip_code: device_zip_code } = deviceAddress ?? {};
console.log('street', street);
console.log('deviceAddress addressForm', deviceAddress);
return (
<Grid container className={classes.borderedRow}>
<Grid item xs={12} md={12} className={classes.rowTitle}>
<span>{t(formGroupHeader)}</span>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<div>
<InputField
type="label"
id="address"
name={formId === 'customer' ? 'address.street' : 'deviceAddress.street'}
value={formId === 'customer' ? street : device_street}
onChange={handleChange}
label={t('Street').toString()}
/>
</div>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="addressNr"
name={formId === 'customer' ? 'address.house_number' : 'deviceAddress.house_number'}
value={formId === 'customer' ? house_number : device_house_number}
onChange={handleChange}
label={t('Number').toString()}
/>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="zip"
name={formId === 'customer' ? 'address.zip_code' : 'deviceAddress.zip_code'}
value={formId === 'customer' ? zip_code : device_zip_code}
onChange={handleChange}
label={t('ZIP').toString()}
/>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="city"
name={formId === 'customer' ? 'address.city' : 'deviceAddress.city'}
value={formId === 'customer' ? city : device_city}
onChange={handleChange}
label={t('ORT').toString()}
/>
</Grid>
</Grid>
);
}
export default AddressForm;
CustomerForm component:
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Grid } from '@material-ui/core';
import InputField from '/components/atoms/a-input/react/components/a-input.component';
import { DeviceOwner } from '/global/js/modules/SystemManagement/types/DeviceOwner';
import { useStyles } from '../hooks/useStyles';
export interface CustomerForm {
customer: DeviceOwner;
handleChange?: () => void;
}
function CustomerForm({ customer, handleChange }: CustomerForm): JSX.Element {
const classes = useStyles();
const { t, i18n } = useTranslation();
const { owner_firstname, owner_lastname, owner_phone, owner_email } = customer ?? {};
console.log('owner first name', owner_firstname);
return (
<Grid container className={classes.borderedRow}>
<Grid item xs={12} md={12} className={classes.rowTitle}>
<span>{t('Customer Data')}</span>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<div>
<InputField
type="label"
id="firstName"
name="owner_firstname"
value={customer && owner_firstname}
onChange={handleChange}
label={t('First Name').toString()}
/>
</div>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="lastName"
name="owner_lastname"
value={customer && owner_lastname}
onChange={handleChange}
label={t('Last Name').toString()}
/>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="phone"
name="owner_phone"
value={customer && owner_phone}
onChange={handleChange}
label={t('Phone').toString()}
/>
</Grid>
<Grid item xs={12} sm={6} md={3} className={classes.formColumn}>
<InputField
type="label"
id="email"
name="owner_email"
value={customer && owner_email}
onChange={handleChange}
label={t('Email').toString()}
/>
</Grid>
</Grid>
);
}
export default CustomerForm;
I’ve tried different approaches in handleChange(), the data from deviceAddress is updated, but when I try to reset the data, the inputs are not reset, only those that are containing data from deviceAddress, others are working fine. I don’t catch the issue here, someone cand help me solve this?