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 | Fikret Ellek | React-Module-Project | Week-3 | Customer Profile #51

Merged
merged 4 commits into from
Mar 14, 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
3 changes: 1 addition & 2 deletions src/components/Bookings/Bookings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import FakeBookings from "../../data/fakeBookings.json";
import SearchResults from "../SearchResults/SearchResults";

const Bookings = () => {

const [bookings, setBookings] = useState(FakeBookings);

const search = (searchVal) => {
Expand All @@ -15,7 +14,7 @@ const Bookings = () => {
<main className="bookings">
<Search search={search} />

<SearchResults results={bookings} />
<SearchResults bookings={bookings} />
</main>
);
};
Expand Down
12 changes: 12 additions & 0 deletions src/components/CustomerProfile/CustomerProfile.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function CustomerProfile({ id, bookings }) {
const booking = bookings.find((booking) => booking.id === id);

if (booking)
return (
<div data-testid="profileCard" className="card">
<h3 data-testid="profileId">Customer Id: {booking.id}</h3>
<p>Name: {booking.firstName}</p>
<p>Surname: {booking.surname}</p>
</div>
);
}
25 changes: 25 additions & 0 deletions src/components/CustomerProfile/CustomerProfile.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import Bookings from "../Bookings/Bookings";
import { fireEvent, render, screen } from "@testing-library/react";

describe("Customer Profile card", () => {
it("shouldn't be displayed at the beginning", () => {
render(<Bookings />);
const profileCard = screen.queryByTestId("profileCard");
expect(profileCard).toBeNull();
});
it("should be displayed after clicking showProfile button", () => {
render(<Bookings />);
const showProfileButton = screen.getByTestId("showProfileButton1");
fireEvent.click(showProfileButton);
const profileCard = screen.getByTestId("profileId");
expect(profileCard.innerHTML).toBe("Customer Id: 1");
});
it("should be displayed after clicking second showProfile button", () => {
render(<Bookings />);
const showProfileButton = screen.getByTestId("showProfileButton2");
fireEvent.click(showProfileButton);
const profileCard = screen.getByTestId("profileId");
expect(profileCard.innerHTML).toBe("Customer Id: 2");
});
});
45 changes: 21 additions & 24 deletions src/components/SearchResults/SearchResults.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
// SearchResults.jsx
import React from "react";
import React, { useState } from "react";
import TableHead from "./TableHead";
import TableBody from "./TableBody";
import dayjs from "dayjs";
import CustomerProfile from "../CustomerProfile/CustomerProfile";

const SearchResults = ({ results }) => {
const SearchResults = ({ bookings }) => {
const [selectedProfile, setSelectedProfile] = useState(null);

function handleShowProfile(e) {
setSelectedProfile(Number(e.target.value));
}
return (
<table>
<thead>
<TableHead />
</thead>
<tbody>
{results.map((booking) => (
<TableBody
key={booking.id}
id={booking.id}
title={booking.title}
firstName={booking.firstName}
surName={booking.surname}
nights={dayjs(booking.checkOutDate)
.diff(dayjs(booking.checkInDate), "days")
.toString()
.padStart(2, "0")}
email={booking.email}
/>
))}
</tbody>
</table>
<>
<table>
<thead>
<TableHead />
</thead>
<tbody>
{bookings.map((booking) => (
<TableBody key={booking.id} booking={booking} handleShowProfile={handleShowProfile} />
))}
</tbody>
</table>
<CustomerProfile id={selectedProfile} bookings={bookings} />
</>
);
};

Expand Down
26 changes: 19 additions & 7 deletions src/components/SearchResults/TableBody.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import React from "react";
import dayjs from "dayjs";

const TableBody = ({ id, title, firstName, surName, email, nights }) => {
const TableBody = ({ booking, handleShowProfile }) => {
const testid = "showProfileButton" + booking.id;
return (
<tr>
<td>{id}</td>
<td>{title}</td>
<td>{firstName}</td>
<td>{surName}</td>
<td>{nights}</td>
<td>{email}</td>
<td>{booking.id}</td>
<td>{booking.title}</td>
<td>{booking.firstName}</td>
<td>{booking.surname}</td>
<td>
{dayjs(booking.checkOutDate)
.diff(dayjs(booking.checkInDate), "days")
.toString()
.padStart(2, "0")}
</td>
<td>{booking.email}</td>
<td>
<button data-testid={testid} value={booking.id} onClick={handleShowProfile}>
Show Profile
</button>
</td>
</tr>
);
};
Expand Down