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 (
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 | +
---|