Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Commit af77f29

Browse files
committed
20. Display a customer profile - step 1
Added a new column in the table of the <SearchResults /> component and displayed a <button> for each row. The text of the button should read "Show profile". Then, created a new <CustomerProfile /> component. This component is rendered next to the table in the <SearchResults /> component. This component receives one prop id. When clicking on a "Show profile" button for a given row, the component <CustomerProfile /> displays the text "Customer Profile", where the id of the selected customer is. Initially, the <CustomerProfile /> component doesn't show anything. Hint: We need to record the selected customer id after clicking on a "Show profile" button. In which component do we think this state should be defined? Tested: When first showing the page, no customer profile is displayed. When clicking the first "Show profile" button of the table, the text "Customer 1 profile" appears. When clicking the second "Show profile" button of the table, the text "Customer 2 profile" appears instead.
1 parent cbec91e commit af77f29

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

src/components/CustomerProfile.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
3+
const CustomerProfile = ({id}) => {
4+
return (
5+
<p>Customer {id} Profile </p>
6+
)
7+
}
8+
9+
export default CustomerProfile;

src/components/SearchResults.jsx

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import React, { useState } from "react";
22
import moment from "moment";
3+
import CustomerProfile from "./CustomerProfile";
34

45
const Row = (props) => {
56
const [highlight, setHighlight] = useState(false);
67
const toggleHighlight = () => {
78
setHighlight(!highlight);
89
};
10+
911
const a = moment(props.result.checkOutDate);
1012
const b = moment(props.result.checkInDate);
1113
const days = a.diff(b, "days"); // 1
14+
15+
1216
return (
1317
<tr
14-
onClick={() => toggleHighlight()}
15-
style={{
16-
backgroundColor: highlight ? "yellow" : "",
17-
}}
18-
key={props.index}
18+
onClick={() => toggleHighlight()}
19+
style={{
20+
backgroundColor: highlight ? "yellow" : "",
21+
}}
22+
key={props.index}
1923
>
2024
<td>{props.result.id}</td>
2125
<td>{props.result.title}</td>
@@ -26,34 +30,49 @@ const Row = (props) => {
2630
<td>{props.result.checkInDate}</td>
2731
<td>{props.result.checkOutDate}</td>
2832
<td>{days}</td>
33+
<td>
34+
<button onClick={() => props.getCustomerId(props.result)}>Show profile</button>
35+
</td>
2936
</tr>
3037
);
3138
};
32-
const SearchResultsOther = (props) => {
39+
const SearchResults= (props) => {
40+
41+
const [customerId, setCustomerId] = useState();
42+
43+
const getCustomerId = (result) => {
44+
setCustomerId(result.id);
45+
}
46+
3347
return (
34-
<table className="table table-bordered">
35-
<thead>
36-
<tr>
37-
<th scope="col">Id</th>
38-
<th scope="col">Title</th>
39-
<th scope="col">First Name</th>
40-
<th scope="col">Surname</th>
41-
<th scope="col">Email</th>
42-
<th scope="col">Room id</th>
43-
<th scope="col">Check in date</th>
44-
<th scope="col">Check out date</th>
45-
<th scope="col">Number of nights</th>
46-
</tr>
47-
</thead>
48-
<tbody>
49-
{props.results.map((result) => {
50-
return (
51-
<Row key={result.id} result={result} />
52-
)
53-
})}
54-
</tbody>
55-
</table>
48+
<div>
49+
<table className="table table-bordered">
50+
<thead>
51+
<tr>
52+
<th scope="col">Id</th>
53+
<th scope="col">Title</th>
54+
<th scope="col">First Name</th>
55+
<th scope="col">Surname</th>
56+
<th scope="col">Email</th>
57+
<th scope="col">Room id</th>
58+
<th scope="col">Check in date</th>
59+
<th scope="col">Check out date</th>
60+
<th scope="col">Number of nights</th>
61+
<th scope="col">Show profile</th>
62+
</tr>
63+
</thead>
64+
<tbody>
65+
{props.results.map((result) => {
66+
return (
67+
<Row key={result.id} getCustomerId={getCustomerId} result={result} />
68+
)
69+
})}
70+
</tbody>
71+
</table>
72+
{customerId ? <CustomerProfile id={customerId}/> : null}
73+
74+
</div>
5675
);
5776
};
5877

59-
export default SearchResultsOther;
78+
export default SearchResults;

0 commit comments

Comments
 (0)