Skip to content

Commit

Permalink
Merge pull request #7 from lksnjw/this-is-the-eding-branch
Browse files Browse the repository at this point in the history
This is the eding branch
  • Loading branch information
lksnjw authored Sep 30, 2024
2 parents 71c23e9 + 1103622 commit b3edb8b
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions frontend/src/Pages/delivery/DLeditdriver.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DLViewDriver = () => {
const [driverDetails, setDriverDetails] = useState(null)
const [selectedImage, setSelectedImage] = useState(null) // For image pop-up
const [isEditing, setIsEditing] = useState(false)
const [errors, setErrors] = useState({}) // Error state for validation
const navigate = useNavigate()


Expand Down Expand Up @@ -119,9 +120,22 @@ const DLViewDriver = () => {
})
}

// Handlers to update driver details

// Function to calculate age from birth date
const calculateAge = (birthDate) => {
const today = new Date()
const birthDateObj = new Date(birthDate)
let age = today.getFullYear() - birthDateObj.getFullYear()
const monthDifference = today.getMonth() - birthDateObj.getMonth()
if (
monthDifference < 0 ||
(monthDifference === 0 && today.getDate() < birthDateObj.getDate())
) {
age--
}
return age
}

// Handler for input changes with validation
const handleInputChange = (e) => {
const { name, value } = e.target;
let errorMessage = '';
Expand All @@ -130,41 +144,17 @@ const DLViewDriver = () => {
if (name === 'fullName') {
if (!/^[A-Za-z\s]*$/.test(value)) {
errorMessage = 'Name should only contain alphabets and spaces.';
return;
}
}

// Validate Contact Number
if (name === 'contactNumber') {
if (name === 'phone') {
if (!/^0\d{9}$/.test(value)) {
errorMessage = 'Contact number must be 10 digits and start with 0.';
}
}

// Validate Birthday and Age
if (name === 'dateOfBirth') {
const age = calculateAge(value);
if (age < 18) {
errorMessage = 'You must be at least 18 years old to register.';
}

// Ensure NIC year matches Birthday year
const birthYear = new Date(value).getFullYear();
if (driverDetails.NIC && !driverDetails.NIC.startsWith(birthYear.toString())) {
errorMessage = 'Please Enter valid NIC';
}
}

// Validate NIC
if (name === 'NIC') {
const nicRegex = /^\d{11}[0-9Vv]$/;
const birthYear = new Date(driverDetails.dateOfBirth).getFullYear().toString();

if (!nicRegex.test(value)) {
errorMessage = 'Please Enter valid NIC';
} else if (!value.startsWith(birthYear)) {
errorMessage = 'Please Enter valid NIC';
}
}

setDriverDetails((prevDetails) => ({
...prevDetails,
Expand Down Expand Up @@ -237,6 +227,9 @@ const DLViewDriver = () => {
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
/>
{errors.fullName && (
<p className="text-red-500">{errors.fullName}</p>
)}
</div>

{/* Email */}
Expand Down Expand Up @@ -265,6 +258,9 @@ const DLViewDriver = () => {
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
/>
{errors.phone && (
<p className="text-red-500">{errors.phone}</p>
)}
</div>

{/* Date of Birth */}
Expand All @@ -279,9 +275,11 @@ const DLViewDriver = () => {
0,
10
)}
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
readOnly // Make email field non-editable
className="p-2 border border-gray-300 rounded-md bg-gray-100 cursor-not-allowed"

/>

</div>

{/* ID Card Number */}
Expand All @@ -293,9 +291,11 @@ const DLViewDriver = () => {
type="text"
name="idCardNumber"
value={driverDetails.idCardNumber}
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
readOnly // Make email field non-editable
className="p-2 border border-gray-300 rounded-md bg-gray-100 cursor-not-allowed"

/>

</div>

{/* License Card Number */}
Expand All @@ -307,9 +307,11 @@ const DLViewDriver = () => {
type="text"
name="licenseCardNumber"
value={driverDetails.licenseCardNumber}
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
readOnly // Make email field non-editable
className="p-2 border border-gray-300 rounded-md bg-gray-100 cursor-not-allowed"

/>

</div>

{/* Address */}
Expand All @@ -324,6 +326,9 @@ const DLViewDriver = () => {
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
/>
{errors.address && (
<p className="text-red-500">{errors.address}</p>
)}
</div>

{/* Vehicle Number */}
Expand All @@ -338,6 +343,9 @@ const DLViewDriver = () => {
onChange={handleInputChange}
className="p-2 border border-gray-300 rounded-md"
/>
{errors.vehicleNumber && (
<p className="text-red-500">{errors.vehicleNumber}</p>
)}
</div>

{/* Vehicle Type */}
Expand All @@ -357,6 +365,9 @@ const DLViewDriver = () => {
</option>
<option value="Lorry">Lorry</option>
</select>
{errors.vehicleType && (
<p className="text-red-500">{errors.vehicleType}</p>
)}
</div>
</div>
</div>
Expand Down

0 comments on commit b3edb8b

Please sign in to comment.