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

Feature implement search functionality #63

Merged
merged 2 commits into from
May 21, 2024
Merged
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
26 changes: 16 additions & 10 deletions src/components/Bookings/Bookings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,47 @@ import { useState, useEffect } from "react";

const Bookings = () => {
const [bookings, setBookings] = useState([]);
const [fetchError, setFetchError] = useState(null);

useEffect(() => {
fetch("https://nw6-cyf-hotel.glitch.me/fakebookings")
fetch("https://cyf-hotel-api.netlify.app/")
.then((res) => {
if (!res.ok) {
throw new Error(`${res.status}: ${getReasonPhrase(res.status)}`);
throw new Error(`${res.status}: ${res.statusText}`);
}
return res.json();
})
.then((data) => setBookings(data))
.catch((error) => {
setFetchError(error);
console.log(error);
console.error(error);
});
}),
[];
}, []);

const search = (searchVal) => {
console.info("TO DO!", searchVal);
const searchValueCase = searchVal.toLowerCase();
const fils = FakeBookings.filter((book) => {
const firstName = book.firstName.toLowerCase();
const lastName = book.surname.toLowerCase();
const filteredBookings = bookings.filter((booking) => {
const firstName = booking.firstName.toLowerCase();
const lastName = booking.surname.toLowerCase();
return (
firstName.includes(searchValueCase) ||
lastName.includes(searchValueCase)
);
});
setBookings(fils);
setBookings(filteredBookings);
};

return (
<main className="bookings">
<Search search={search} />
<SearchResult results={bookings} />
{fetchError ? (
<div>Error: {fetchError.message}</div>
) : (
<SearchResult results={bookings} />
)}
</main>
);
};

export default Bookings;
1 change: 1 addition & 0 deletions src/components/Search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Search = (props) => {
event.preventDefault();
props.search(searchInput);
};

return (
<section className="search">
<header className="search__header">
Expand Down
8 changes: 2 additions & 6 deletions src/components/SearchResult/SearchResult.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import "./SearchResult.scss";
import CustomerProfile from "../CustomerProfile/CustomerProfile";
import Table from "./Table";



function SearchResult() {


function SearchResult({ results }) {
return (
<>
<Table />
<Table bookings={results} />
<CustomerProfile />
</>
);
Expand Down
8 changes: 4 additions & 4 deletions src/components/SearchResult/Table.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import TableBody from "./TableBody";
import TableHead from "./TableHead";
import dayjs from "dayjs";
import FakeBookings from "@/data/fakeBookings.json";
// import FakeBookings from "@/data/fakeBookings.json";

function Table() {
const Bookings = FakeBookings;
function Table({ bookings }) {
// const Bookings = bookings;
return (
<>
<table>
<thead>
<TableHead />
</thead>
<tbody>
{Bookings.map((book) => {
{bookings.map((book) => {
const stayNightsTotal = dayjs(book.checkOutDate).diff(
dayjs(book.checkInDate),
"day"
Expand Down