236 lines
5.2 KiB
TypeScript
236 lines
5.2 KiB
TypeScript
import React, { useEffect } from "react";
|
|
import { Box, Button, Typography, Modal } from "@mui/material";
|
|
import CloseIcon from "@mui/icons-material/Close";
|
|
import { useForm, Controller } from "react-hook-form";
|
|
import { CustomIconButton, CustomTextField } from "../AddUserModal/styled.css";
|
|
|
|
interface EditUserModalProps {
|
|
open: boolean;
|
|
handleClose: () => void;
|
|
handleUpdate: (
|
|
id: number,
|
|
name: string,
|
|
email: string,
|
|
phone: string
|
|
) => void;
|
|
editRow: any;
|
|
}
|
|
|
|
interface FormData {
|
|
name: string;
|
|
email: string;
|
|
phone: string;
|
|
}
|
|
|
|
const EditUserModal: React.FC<EditUserModalProps> = ({
|
|
open,
|
|
handleClose,
|
|
handleUpdate,
|
|
editRow,
|
|
}) => {
|
|
const {
|
|
control,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
setValue,
|
|
reset,
|
|
} = useForm<FormData>({
|
|
defaultValues: {
|
|
name: "",
|
|
email: "",
|
|
phone: "",
|
|
},
|
|
});
|
|
|
|
// Set values if editRow is provided
|
|
useEffect(() => {
|
|
if (editRow) {
|
|
setValue("name", editRow.name);
|
|
setValue("email", editRow.email);
|
|
setValue("phone", editRow.phone);
|
|
} else {
|
|
reset();
|
|
}
|
|
}, [editRow, setValue, reset]);
|
|
|
|
const onSubmit = (data: FormData) => {
|
|
if (editRow) {
|
|
handleUpdate(editRow.id, data.name, data.email, data.phone);
|
|
}
|
|
handleClose(); // Close the modal
|
|
reset(); // Reset the form fields
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
open={open}
|
|
onClose={(e, reason) => {
|
|
if (reason === "backdropClick") {
|
|
return;
|
|
}
|
|
handleClose();
|
|
}}
|
|
aria-labelledby="edit-user-modal"
|
|
>
|
|
<Box
|
|
component="form"
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
sx={{
|
|
position: "absolute",
|
|
top: "50%",
|
|
left: "50%",
|
|
transform: "translate(-50%, -50%)",
|
|
width: 400,
|
|
bgcolor: "background.paper",
|
|
boxShadow: 24,
|
|
p: 3,
|
|
borderRadius: 2,
|
|
}}
|
|
>
|
|
{/* Header */}
|
|
<Box
|
|
sx={{
|
|
display: "flex",
|
|
justifyContent: "space-between",
|
|
alignItems: "center",
|
|
}}
|
|
>
|
|
<Typography variant="h6" fontWeight={600}>
|
|
Edit User
|
|
</Typography>
|
|
<CustomIconButton onClick={handleClose}>
|
|
<CloseIcon />
|
|
</CustomIconButton>
|
|
</Box>
|
|
|
|
{/* Horizontal Line */}
|
|
<Box sx={{ borderBottom: "1px solid #ddd", my: 2 }} />
|
|
|
|
{/* Input Fields */}
|
|
<Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
|
|
{/* Name */}
|
|
<Box sx={{ display: "flex", flexDirection: "column" }}>
|
|
<Typography variant="body2" fontWeight={500} mb={0.5}>
|
|
Full Name
|
|
</Typography>
|
|
<Controller
|
|
name="name"
|
|
control={control}
|
|
rules={{
|
|
required: "User Name is required",
|
|
minLength: {
|
|
value: 3,
|
|
message: "Minimum 3 characters required",
|
|
},
|
|
maxLength: {
|
|
value: 30,
|
|
message: "Maximum 30 characters allowed",
|
|
},
|
|
}}
|
|
render={({ field }) => (
|
|
<CustomTextField
|
|
{...field}
|
|
fullWidth
|
|
placeholder="Enter Full Name"
|
|
size="small"
|
|
sx={{ marginTop: 1 }}
|
|
error={!!errors.name}
|
|
helperText={errors.name?.message}
|
|
/>
|
|
)}
|
|
/>
|
|
</Box>
|
|
|
|
{/* Email */}
|
|
<Box sx={{ display: "flex", flexDirection: "column" }}>
|
|
<Typography variant="body2" fontWeight={500} mb={0.5}>
|
|
Email
|
|
</Typography>
|
|
<Controller
|
|
name="email"
|
|
control={control}
|
|
rules={{
|
|
required: "Email is required",
|
|
pattern: {
|
|
value: /\S+@\S+\.\S+/,
|
|
message:
|
|
"Please enter a valid email address.",
|
|
},
|
|
}}
|
|
render={({ field }) => (
|
|
<CustomTextField
|
|
{...field}
|
|
type="email"
|
|
fullWidth
|
|
placeholder="Enter Email"
|
|
size="small"
|
|
sx={{ marginTop: 1 }}
|
|
error={!!errors.email}
|
|
helperText={errors.email?.message}
|
|
/>
|
|
)}
|
|
/>
|
|
</Box>
|
|
|
|
{/* Phone */}
|
|
<Box sx={{ display: "flex", flexDirection: "column" }}>
|
|
<Typography variant="body2" fontWeight={500} mb={0.5}>
|
|
Phone Number
|
|
</Typography>
|
|
<Controller
|
|
name="phone"
|
|
control={control}
|
|
rules={{
|
|
required: "Phone number is required",
|
|
validate: (value) => {
|
|
if (!/^[0-9]*$/.test(value)) {
|
|
return "Only numbers are allowed";
|
|
}
|
|
if (value.length < 6) {
|
|
return "Phone number must be at least 6 digits";
|
|
}
|
|
if (value.length > 14) {
|
|
return "Phone number must be at most 14 digits";
|
|
}
|
|
return true; // No errors
|
|
},
|
|
}}
|
|
render={({ field }) => (
|
|
<CustomTextField
|
|
{...field}
|
|
fullWidth
|
|
placeholder="Enter Phone Number"
|
|
size="small"
|
|
sx={{ marginTop: 1 }}
|
|
error={!!errors.phone}
|
|
helperText={errors.phone?.message}
|
|
/>
|
|
)}
|
|
/>
|
|
</Box>
|
|
</Box>
|
|
|
|
{/* Submit Button */}
|
|
<Box
|
|
sx={{ display: "flex", justifyContent: "flex-end", mt: 3 }}
|
|
>
|
|
<Button
|
|
type="submit"
|
|
sx={{
|
|
backgroundColor: "#52ACDF",
|
|
color: "white",
|
|
borderRadius: "8px",
|
|
width: "117px",
|
|
"&:hover": { backgroundColor: "#439BC1" },
|
|
}}
|
|
>
|
|
Update User
|
|
</Button>
|
|
</Box>
|
|
</Box>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default EditUserModal;
|