diff --git a/src/components/SearchButton/SearchButton.test.jsx b/src/components/SearchButton/SearchButton.test.jsx index aacf48e..97acb8d 100644 --- a/src/components/SearchButton/SearchButton.test.jsx +++ b/src/components/SearchButton/SearchButton.test.jsx @@ -13,3 +13,5 @@ describe("Search Button", () => { // expect(searchButton).toBeInTheDocument(); // }); }); + + diff --git a/src/components/SearchResult/SearchResult.jsx b/src/components/SearchResult/SearchResult.jsx index 66b9d6c..b058e89 100644 --- a/src/components/SearchResult/SearchResult.jsx +++ b/src/components/SearchResult/SearchResult.jsx @@ -1,3 +1,4 @@ +import React, { useState } from 'react'; import "./SearchResult.scss"; import FakeBookings from "@/data/fakeBookings.json"; import TableBody from "./TableBody"; @@ -5,36 +6,76 @@ import TableHead from "./TableHead"; import dayjs from "dayjs"; const Bookings = FakeBookings; + function SearchResult() { + const [sortConfig, setSortConfig] = useState({ key: null, direction: 'ascending' }); + + // Function to handle sorting when a column header is clicked + const handleSort = (key) => { + let direction = 'ascending'; + if (sortConfig.key === key && sortConfig.direction === 'ascending') { + direction = 'descending'; + } + setSortConfig({ key, direction }); + }; + + + // Sorting function based on the current sort configuration + const sortedBookings = [...Bookings].sort((a, b) => { + if (sortConfig.key === null) return 0; + + let aValue = a[sortConfig.key]; + let bValue = b[sortConfig.key]; + + // Special handling for 'roomId' sorting + if (sortConfig.key === 'roomId') { + // Convert string room IDs to numbers for proper sorting + aValue = parseInt(aValue); + bValue = parseInt(bValue); + } + + const sortOrder = sortConfig.direction === 'ascending' ? 1 : -1; + return aValue - bValue * sortOrder; + }); + return ( - + + + + + + + + + + - {Bookings.map((book) => { + {sortedBookings.map((book) => { const stayNightsTotal = dayjs(book.checkOutDate).diff( dayjs(book.checkInDate), "day" ); return ( - + ); })}
handleSort('id')}>ID handleSort('title')}>Title handleSort('firstName')}>First Name handleSort('surname')}>Surname handleSort('email')}>Email handleSort('roomId')}>Room ID handleSort('checkInDate')}>Check In Date handleSort('checkOutDate')}>Check Out Date
); } -export default SearchResult; +export default SearchResult; diff --git a/src/components/SearchResult/SearchResult.scss b/src/components/SearchResult/SearchResult.scss index 4a2e035..49f4be7 100644 --- a/src/components/SearchResult/SearchResult.scss +++ b/src/components/SearchResult/SearchResult.scss @@ -4,8 +4,8 @@ table { } thead { - background-color: rgb(31, 11, 31); - color: white; + background-color: rgb(37, 12, 37); + color: rgb(248, 230, 230); } th, @@ -22,5 +22,5 @@ th { cursor: pointer; } .selected { - background-color: skyblue; + background-color: rgb(241, 176, 249); } diff --git a/src/components/SearchResult/TableHead.jsx b/src/components/SearchResult/TableHead.jsx index 9afda64..9de1575 100644 --- a/src/components/SearchResult/TableHead.jsx +++ b/src/components/SearchResult/TableHead.jsx @@ -2,6 +2,7 @@ function TableHead() { return ( <> + ID Title First name @@ -11,6 +12,7 @@ function TableHead() { Check in date Check out date Total Nights to Stay + );