-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NW6 | Fikret Ellek | React-Module-Project | Week-4 | Feature Validate AddBooking #63
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React, { useState } from "react"; | ||
import "./AddBookingForm.scss"; | ||
import dayjs, { Dayjs } from "dayjs"; | ||
|
||
const AddBookingForm = ({ bookings, setBookings }) => { | ||
const [formData, setFormData] = useState({ | ||
|
@@ -14,6 +15,20 @@ const AddBookingForm = ({ bookings, setBookings }) => { | |
}); | ||
const [nextId, setNextId] = useState(bookings.length + 1); | ||
|
||
const todaysDate = | ||
dayjs().get("year") + | ||
"-" + | ||
(dayjs().get("month") + 1).toString().padStart(2, "0") + | ||
"-" + | ||
dayjs().get("date"); | ||
const maxDate = | ||
dayjs().get("year") + | ||
1 + | ||
"-" + | ||
(dayjs().get("month") + 1).toString().padStart(2, "0") + | ||
"-" + | ||
dayjs().get("date"); | ||
|
||
const addBooking = (newBooking) => { | ||
setBookings([...bookings, newBooking]); | ||
}; | ||
|
@@ -23,21 +38,46 @@ const AddBookingForm = ({ bookings, setBookings }) => { | |
setFormData((prevData) => ({ ...prevData, [name]: value })); | ||
}; | ||
|
||
const validateBooking = () => { | ||
if (!isNaN(parseInt(formData.firstName))) { | ||
alert("Enter a valid name"); | ||
} else if (!isNaN(parseInt(formData.surname))) { | ||
alert("Enter a valid surname"); | ||
} else if ( | ||
!( | ||
formData.email.includes("@") && | ||
formData.email.includes(".") && | ||
formData.email.indexOf("@") === formData.email.lastIndexOf("@") && | ||
formData.email.indexOf("@") < formData.email.lastIndexOf(".") | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works but it's fairly hard to read I normally use a regex in these cases but that is a diffrent thing. A fairly simple regex such as formData.email.match(/^.+@.+.$/)) should work |
||
) { | ||
alert("Enter a valid email"); | ||
} else if (!(formData.roomId > 0 && formData.roomId < 100)) { | ||
alert("Enter a valid roon id"); | ||
} else if (!(formData.checkInDate < formData.checkOutDate)) { | ||
alert("Enter valid dates"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good work, a more specfic error message here would be helpful to the user though - doesn't say which date is invalid |
||
} else { | ||
return true; | ||
} | ||
}; | ||
|
||
const handleSubmit = (e) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brilliant calling validateBooking function inside the handleSubmit function. |
||
e.preventDefault(); | ||
const newBooking = { ...formData, id: nextId }; | ||
addBooking(newBooking); | ||
setFormData({ | ||
id: "", | ||
firstName: "", | ||
surname: "", | ||
email: "", | ||
title: "", | ||
roomId: "", | ||
checkInDate: "", | ||
checkOutDate: "", | ||
}); | ||
setNextId(nextId + 1); | ||
if (validateBooking()) { | ||
const newBooking = { ...formData, id: nextId }; | ||
addBooking(newBooking); | ||
setFormData({ | ||
id: "", | ||
firstName: "", | ||
surname: "", | ||
email: "", | ||
title: "", | ||
roomId: "", | ||
checkInDate: "", | ||
checkOutDate: "", | ||
}); | ||
setNextId(nextId + 1); | ||
} | ||
}; | ||
|
||
return ( | ||
|
@@ -74,11 +114,12 @@ const AddBookingForm = ({ bookings, setBookings }) => { | |
value={formData.email} | ||
onChange={handleChange} | ||
/> | ||
|
||
<input | ||
required | ||
type="date" | ||
name="checkInDate" | ||
min={todaysDate} | ||
max={maxDate} | ||
placeholder="Check In Date" | ||
value={formData.checkInDate} | ||
onChange={handleChange} | ||
|
@@ -87,11 +128,14 @@ const AddBookingForm = ({ bookings, setBookings }) => { | |
required | ||
type="date" | ||
name="checkOutDate" | ||
min={todaysDate} | ||
max={maxDate} | ||
placeholder="Check Out Date" | ||
value={formData.checkOutDate} | ||
onChange={handleChange} | ||
/> | ||
<input | ||
required | ||
type="text" | ||
name="roomId" | ||
placeholder="Room ID" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an intresting approach to the problem. I think it mostly works, even for special letters such as Ö which is good as few validation solutions do. Of course it includes ! and other special chars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as you said regex would cover it all. to get a valid name should I use [a-zA-z]?