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

NW6 | nazanin_Saedi | Feature-sort-table-column | reactProject | Week4 #51

Open
wants to merge 7 commits 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
2 changes: 2 additions & 0 deletions src/components/SearchButton/SearchButton.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ describe("Search Button", () => {
// expect(searchButton).toBeInTheDocument();
// });
});


71 changes: 56 additions & 15 deletions src/components/SearchResult/SearchResult.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,81 @@
import React, { useState } from 'react';
import "./SearchResult.scss";
import FakeBookings from "@/data/fakeBookings.json";
import TableBody from "./TableBody";
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 });
};
Copy link

Choose a reason for hiding this comment

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

Looking good, like how you support sorting in both directions



// Sorting function based on the current sort configuration
const sortedBookings = [...Bookings].sort((a, b) => {
Copy link

Choose a reason for hiding this comment

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

You should be able to do Bookings.sort here. Any particular reason you're done it this way?

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);
}
Copy link

Choose a reason for hiding this comment

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

THis is a good touch 👍


const sortOrder = sortConfig.direction === 'ascending' ? 1 : -1;
return aValue - bValue * sortOrder;
});
Copy link

Choose a reason for hiding this comment

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

The code could use a comment here - not easy to tell what it does


return (
<table>
<thead>
<TableHead />
<tr>
<th onClick={() => handleSort('id')}>ID</th>
<th onClick={() => handleSort('title')}>Title</th>
<th onClick={() => handleSort('firstName')}>First Name</th>
<th onClick={() => handleSort('surname')}>Surname</th>
<th onClick={() => handleSort('email')}>Email</th>
<th onClick={() => handleSort('roomId')}>Room ID</th>
<th onClick={() => handleSort('checkInDate')}>Check In Date</th>
<th onClick={() => handleSort('checkOutDate')}>Check Out Date</th>
</tr>
Copy link

Choose a reason for hiding this comment

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

This works however and idea for next time: it's worth thinking about using .map() on an array for these sorts of things - can make it tider thouht this is up to your prefrence

</thead>
<tbody>
{Bookings.map((book) => {
{sortedBookings.map((book) => {
const stayNightsTotal = dayjs(book.checkOutDate).diff(
dayjs(book.checkInDate),
"day"
);
return (
<TableBody
key={book.id}
id={book.id}
title={book.title}
firstName={book.firstName}
surName={book.surname}
email={book.email}
roomId={book.roomId}
checkInDate={book.checkInDate}
checkOutDate={book.checkOutDate}
stayNights={stayNightsTotal}
/>
<TableBody
key={book.id}
id={book.id}
title={book.title}
firstName={book.firstName}
surName={book.surname}
email={book.email}
roomId={book.roomId}
checkInDate={book.checkInDate}
checkOutDate={book.checkOutDate}
stayNights={stayNightsTotal}
/>
);
})}
</tbody>
</table>
);
}
export default SearchResult;

export default SearchResult;
6 changes: 3 additions & 3 deletions src/components/SearchResult/SearchResult.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,5 +22,5 @@ th {
cursor: pointer;
}
.selected {
background-color: skyblue;
background-color: rgb(241, 176, 249);
Copy link

Choose a reason for hiding this comment

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

Nice touch.

}
2 changes: 2 additions & 0 deletions src/components/SearchResult/TableHead.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function TableHead() {
return (
<>
<tr>

<th>ID</th>
<th>Title</th>
<th>First name</th>
Expand All @@ -11,6 +12,7 @@ function TableHead() {
<th>Check in date</th>
<th>Check out date</th>
<th>Total Nights to Stay</th>

</tr>
</>
);
Expand Down