Skip to content

Commit

Permalink
resolved sonarcloud duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulg1254 committed Aug 1, 2024
1 parent 5768d1a commit 7e6ee94
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/components/AddDistrictBlockModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,39 @@ export const AddDistrictBlockModal: React.FC<AddDistrictBlockModalProps> = ({
initialValues = {},
districtId,
}) => {
const [name, setName] = useState<string>(initialValues.name || "");
const [value, setValue] = useState<string>(initialValues.value || "");
const [controllingField, setControllingField] = useState<string>(
initialValues.controllingField || ""
);
const [formData, setFormData] = useState({
name: initialValues.name || "",
value: initialValues.value || "",
controllingField: initialValues.controllingField || "",
});

const { t } = useTranslation();

useEffect(() => {
setName(initialValues.name || "");
setValue(initialValues.value || "");
setControllingField(initialValues.controllingField || "");
setFormData({
name: initialValues.name || "",
value: initialValues.value || "",
controllingField: initialValues.controllingField || "",
});
}, [initialValues]);

const handleChange =
(field: string) => (event: React.ChangeEvent<HTMLInputElement>) => {
setFormData((prev) => ({ ...prev, [field]: event.target.value }));
};

const handleSubmit = () => {
const { name, value, controllingField } = formData;
onSubmit(name, value, controllingField, fieldId, districtId);
console.log(name, value, controllingField, fieldId, districtId);
onClose();
};

const buttonStyles = {
fontSize: "14px",
fontWeight: "500",
};

return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>{t("COMMON.ADD_DISTRICT")}</DialogTitle>
Expand All @@ -68,26 +82,26 @@ export const AddDistrictBlockModal: React.FC<AddDistrictBlockModalProps> = ({
type="text"
fullWidth
variant="outlined"
value={controllingField}
onChange={(e) => setControllingField(e.target.value)}
value={formData.controllingField}
onChange={handleChange("controllingField")}
/>
<TextField
margin="dense"
label="Name"
label="Name"
type="text"
fullWidth
variant="outlined"
value={name}
onChange={(e) => setName(e.target.value)}
value={formData.name}
onChange={handleChange("name")}
/>
<TextField
margin="dense"
label="Value"
type="text"
fullWidth
variant="outlined"
value={value}
onChange={(e) => setValue(e.target.value)}
value={formData.value}
onChange={handleChange("value")}
/>
<Box display="flex" alignItems="center" mt={2}>
<InfoOutlinedIcon color="primary" sx={{ mr: 1 }} />
Expand All @@ -100,12 +114,9 @@ export const AddDistrictBlockModal: React.FC<AddDistrictBlockModalProps> = ({
<Button
onClick={onClose}
sx={{
border: "none",
...buttonStyles,
color: "secondary",
fontSize: "14px",
fontWeight: "500",
"&:hover": {
border: "none",
backgroundColor: "transparent",
},
}}
Expand All @@ -115,12 +126,7 @@ export const AddDistrictBlockModal: React.FC<AddDistrictBlockModalProps> = ({
</Button>
<Button
onClick={handleSubmit}
sx={{
width: "auto",
height: "40px",
fontSize: "14px",
fontWeight: "500",
}}
sx={{ ...buttonStyles, width: "auto", height: "40px" }}
variant="contained"
color="primary"
>
Expand Down

0 comments on commit 7e6ee94

Please sign in to comment.