-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ describe("Search Button", () => { | |
// expect(searchButton).toBeInTheDocument(); | ||
// }); | ||
}); | ||
|
||
|
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 }); | ||
}; | ||
|
||
|
||
// Sorting function based on the current sort configuration | ||
const sortedBookings = [...Bookings].sort((a, b) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice touch. |
||
} |
There was a problem hiding this comment.
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