Skip to content
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

data fetching with the delay message & Feature Validate AddBooking #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 80 additions & 30 deletions src/components/Bookings/Bookings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ const Bookings = () => {
setNewBooking({ ...newBooking, [fieldName]: updatedValue });
};

const [isLoading, setLoading] = useState(true); // New state for loading indicator

useEffect(() => {
setBookings(FakeBookings);
// Fetch data when the component mounts
// fetch("https://cyf-react.glitch.me").then((res)=>{res.json()}).then((data)=>{
// console.log(data);
// setBookings(data)
// })
// const fetchData = async () => {
// try {
// const response = await fetch("https://cyf-react.glitch.me");//,{ mode: 'no-cors'});
// const data = await response.json();
// setBookings(data);
// console.log("Fetched bookings data:", data);
// } catch (error) {
// console.error("Error fetching bookings data:", error);
// }
// };
// fetchData();
const fetchData = async () => {
try {
const response = await fetch(
"https://phrygian-cheddar-antler.glitch.me/delayed"
);
const data = await response.json();
setBookings(data);
setLoading(false); // Set loading to false once data is fetched
console.log("Fetched bookings data:", data);
} catch (error) {
console.error("Error fetching bookings data:", error);
setLoading(false); // Set loading to false in case of an error
}
};
fetchData();
// Log text when the component first renders
console.log("Component has rendered!");
}, []); // Empty dependency array means this effect runs only once on mount
Expand All @@ -63,31 +64,71 @@ const Bookings = () => {
)
);
};
//error handling for the form inputs
const [error, setError] = useState(null);

// Function to check the email format
const isValidEmail = (email) => {
Copy link
Collaborator

@adniyaYousaf adniyaYousaf Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you use the pattern attribute to add validation on email input indead of creating a new funtion? Like here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email

const emailParts = email.split("@");
if (emailParts.length !== 2) {
return false;
}

const [localPart, domainPart] = emailParts;
if (localPart.length === 0 || domainPart.length === 0) {
return false;
}

const domainParts = domainPart.split(".");
if (domainParts.length < 2) {
return false;
}

return true;
};

const bookingSubmit = (e) => {
e.preventDefault();

// Check for empty values in all fields
for (const field in newBooking) {
if (!newBooking[field]) {
setError("All fields must be filled in.");
return;
}
}
// Check if the room ID is already booked
const isRoomAlreadyBooked = bookings.some(
(booking) => booking.roomId === newBooking.roomId
);

if (isRoomAlreadyBooked) {
alert("This room is already booked. Please choose another room.");
setError("This room is already booked. Please choose another room.");
return; // Prevent further execution if the room is already booked
}
// Check if any of the required fields are empty
const requiredFields = [
"firstName",
"surname",
"email",
"roomId",
"checkInDate",
"checkOutDate",
];
if (requiredFields.some((field) => !newBooking[field])) {
alert("Please fill in all required fields.");

// Validation for first name, last name, email, and room ID
if (
Copy link
Collaborator

@adniyaYousaf adniyaYousaf Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Input element have attribute required to validate that input id filled or not.
look into this https://www.w3schools.com/js/js_validation.asp

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That because it's only works on the HTML on react I'm trying to check if any of the fields are empty, that's what I understood when I was testing the validation function. It would make an extra line if I added the required attribute on the input fields

!newBooking.firstName ||
!newBooking.surname ||
!isValidEmail(newBooking.email) ||
!newBooking.roomId.match(/^\d+$/)
) {
setError(
"Invalid input. Please check your entries, and ensure the email format is valid."
);
return;
}

// Additional validation for room ID range
const roomId = parseInt(newBooking.roomId, 10);
if (roomId < 0 || roomId > 100) {
setError("Room ID must be a number between 0 and 100.");
return;
}

// Clear any existing error message
setError(null);
// Generate a unique id for the new booking
const newId = Math.max(...bookings.map((booking) => booking.id), 0) + 1;

Expand Down Expand Up @@ -115,7 +156,11 @@ const Bookings = () => {
<main className="bookings">
<Search search={search} />
<section className="content">
<SearchResults results={bookings} />
{isLoading ? (
<p>Loading data, please wait...</p>
) : (
<SearchResults results={bookings} />
)}
</section>
<button className="open-modal-button" onClick={openModal}>
Book new costumer
Expand Down Expand Up @@ -200,8 +245,13 @@ const Bookings = () => {
<button className="submit_button" type="submit">
Confirm booking
</button>
{error && <div className="error">{error}</div>}
</form>
<img class="form_img" src="/src/assets/spa-logo.png"></img>
<img
className="form_img"
src="/src/assets/spa-logo.png"
alt="SPA Logo"
/>
</div>
</div>

Expand Down
130 changes: 65 additions & 65 deletions src/components/Bookings/Bookings.scss
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
// Bookings.scss

.bookings {
padding: 20px;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The styles looks good.

padding: 20px;
}
.container{
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 5px 35px;
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 5px 35px;
}
.add_customer{
position: fixed;
top: 80px;
left: 410px;
.add_customer {
position: relative;
bottom: 260px;
right: -175px;
}

form {

grid-gap: 5px 35px;
max-width: 500px;
position: relative;
grid-gap: 5px 35px;
max-width: 500px;
position: relative;
}
label {
margin-bottom: 8px;
margin-bottom: 8px;
}

input {
padding: 8px;
width: 100%;
padding: 8px;
width: 100%;
}

button {

padding: 10px;
background-color: #007bff;
color: #fff;
cursor: pointer;
border: none;
border-radius: 4px;
font-size: 16px;
transition: background-color 0.3s;
padding: 10px;
background-color: #007bff;
color: #fff;
cursor: pointer;
border: none;
border-radius: 4px;
font-size: 16px;
transition: background-color 0.3s;
}

button:hover {
background-color: #0056b3;
background-color: #0056b3;
}
.submit_button{
margin-left: 30px;
text-align: center;
width: 15rem;


.submit_button {
margin-left: 30px;
text-align: center;
width: 15rem;
}

.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}

.modal-content {
background: #5f74eb;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
max-width: 600px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;

background: #5f74eb;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
max-width: 600px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.close-modal-button {
position: relative;
bottom: 260px;
left: -50px;
position: relative;
bottom: 260px;
left: -50px;
background: none;
border: none;
cursor: pointer;
font-size: 25px;
color: #ff0000;
transition: transform 0.3s ease-in-out;
outline: none;

&:hover,
&:focus {
transform: scale(1.75);
background: none;
border: none;
cursor: pointer;
font-size: 25px;
color: #ff0000;
transition: transform 0.3s ease-in-out;
outline: none;
}
}

&:hover,
&:focus{
transform: scale(1.75);
background: none;
outline: none;
}
.error {
color: #ff0000;
font-size: clamp(0.5rem, 0.85vw, 1.5rem);
}