A hotel booking application in React. Homework for the CodeYourFuture React module
- Follow the instructions to fork & clone the GitHub repo
- Install the dependencies by running
npm install - Launch server using
npm start - It should automatically open http://localhost:3000/
-
Extract the
<button>in thesrc/Search.jscomponent to be its own separate component. -
Extract the
<header>in thesrc/App.jsto be its own separate component calledHeading. Make sure that you import and render the<Heading />component withinsrc/App.js. In theHeadingcomponent, render the hotel's logo in an<img>(you can usehttps://image.flaticon.com/icons/svg/139/139899.svgor find your own image URL). You can adjust the CSS by editingsrc/App.cssto make your Heading looks better if necessary. -
In
src/App.js, above the<Bookings />component add a new component calledTouristInfoCardswhich shows 3 cards. A card is a common user interface pattern with an image at the top and some related text underneath. The cards must link topeoplemakeglasgow.com,visitmanchester.comandvisitlondon.com. The cards should contain the name of the city and an image of the city (use the same className as the example below to benefit from Bootstrap library which is already imported for you in the project). Use the JSX code below as an example of one card (note that in JSX, you'll need to useclassNameinstead ofclass):
<div className="card">
<img src="..." className="card-img-top" />
<div className="card-body">
<a href="#" className="btn btn-primary">Go somewhere</a>
</div>
</div>
-
Add a
<Footer />component at the bottom of the page. Pass the following array as a prop to this component:["123 Fake Street, London, E1 4UD", "[email protected]", "0123 456789"]. Inside the component, use the data you passed as a prop to render a<ul>list with each item of the array displayed as a<li>. Hint: the.map()method will by useful. -
Create a
<SearchResults />component that shows hotel bookings in a<table>element. Each booking will have an id, title, first name, surname, email, room id, check in date and check out date. You can make up data to show in the table. Then show<SearchResults />component within the<Bookings />component that is provided for you. Be sure to split out your components into small well-named components, similar to the method used in exercise 1. Hint: You will find some useful<table>examples in the Bootstrap documentation for tables -
Instead of using your hard-coded data in the
<SearchResults />component, load data from thesrc/data/fakeBookings.jsonfile in the<Bookings />component and pass it as a prop to<SearchResults />. Hint: look in the<Bookings />component for how to import data from a JSON file. -
Add another column to your
<SearchResults />table which shows the number of days each booking is staying. Hint: try installing the moment.js library (you'll need to install it withnpm install moment --save) and using the.diff()method to compare dates
-
Within
src/App.js, render the<Restaurant />component (that is provided for you insrc/Restaurant.js) underneath the<Bookings />component. Then convert the<Restaurant />component to a class component. -
Add a method to the
Restaurantclass namedaddOrder. Useconsole.logto log a "Add order" message. Remember to use theaddOrder = () => {}syntax. Add aonClickhandler to the Add<button>that callsthis.addOrder. Ensure that clicking on the button logs your "Add order" message in the console. -
Extract the
<button>in the<Restaurant />component to a new component namedRestaurantButton. Pass thethis.addOrdermethod as a prop to the<RestaurantButton />component and use this prop in theonClickhandler. Ensure that clicking the button still logs the "Add order" message. -
Within the
<Restaurant />component, initialise state to have a key namedordersand a value of 0 (hint: use theconstructormethod). Then replace theordersvariable within therendermethod withthis.state.ordersthat we just created. -
Within the
addOrdermethod of<Restaurant />, use thethis.setStatemethod to increment theordersstate by 1. Hint: remember that if we are using previous state to calculate the new state, we must use a callback function withthis.setState. -
Extract the
<li>containing "Pizzas" within the<Restaurant />component to a new component namedOrder. Moveordersinitial state set up in theconstructorand theaddOrdermethod from<Restaurant />to the new<Order />component. Make sure that clicking the "Add" button still increments the number of orders. Then replace the hard-coded string "Pizzas" in<Order />with a prop namedorderType. Finally, render another<Order />component but this time with the proporderType="Salads". -
Within the
<SearchResults />component or it's child components, add anonClickhandler to each row in the table (hint: on the<tr>element). When clicked, the row is "selected" and highlighted with a different colour. Hint: use state to add a class to theclassName. When clicking on the row for a second time, "unselect" the row and remove the coloured highlighting.
-
Within your
<Header />component, render the<Clock />component (that is provided for you insrc/Clock.js). Fix the problem where thesetTimeouttimer is not cleared if the component is unmounted. Hint: look at the Clock exercise you did in class. -
Convert the
src/Search.jscomponent into a class component. Add aconstructormethod and initialise a new statesearchInputto an empty string''. Add avalueproperty to the<input>that you set to your newsearchInputstate. Then create a new methodhandleSearchInputtaking aneventparameter. This method should usesetStateto update the statesearchInputwith what the user typed in the input (hint: useevent.target.valueto get the input value). Finally add aonChangeproperty to the<input>set to the methodhandleSearchInput. -
Still in the
<Search />component, add anonSubmithandler to the<form>element. When the form is submitted (try clicking the search button), get the value of the statesearchInputand pass it as a parameter to thethis.props.searchprop function that has been provided for you. Look in the console, you should see the text that is typed in the search box when submitting the form (note: also your submit handler should take aneventparameter and add the lineevent.preventDefault()to prevent the browser to implicitely submit the form). -
In the
<Bookings />component, use state to hold theFakeBookingsdata instead of directly passing it to<SearchResults />. Hint: use aconstructormethod to initialise the state with theFakeBookingsvariable. -
Still in the
<Bookings />component, implement thesearchmethod. It must use thesearchVal(that you just passed from the<Search />component) to filter the search results. The filter function should return results wherefirstNameorsurnamematchsearchVal. Once filtered, usethis.setStateto update the results rendered in<SearchResults />. -
Again in the
<Bookings />component, use thefetch()function to get data fromhttps://cyf-react.glitch.me. Hints:
- Replace
FakeBookingsin the state initialise withnull(because we haven't fetched any results yet!) - Add a
componentDidMount()method that calls thefetch()function and then use.then()to handle the response. Try looking at your Pokemon app that you worked on in class for an example - When the response comes back use
this.setStateto update the results
-
Now show a loading state in
<Bookings />while the data from the server is being fetched. To test this, try loading data fromhttps://cyf-react.glitch.me/delayed, which has a 5 second delay before returning the data. Hint: try looking at your Pokemon app that you worked on in class for an example -
Finally, display an error message in
<Bookings />if there is an HTTP error when fetching data from the server. To test this, try loading data fromhttps://cyf-react.glitch.me/error, which will return a 500 HTTP error. Hint: Try looking at your Pokemon app that you worked on in class for an example
-
Add a form with
<input>s for each of the booking fields (first name, surname, title, room id, check in date, check out date) to the bottom of the page. Submitting the form adds the booking to the result table. Note that the new booking won't persist if you refresh the page. -
Add an
onClickhandler to the columns of the result table, which sorts the results ascending (A -> Z). Clicking the column again will reverse the sort order to descending (Z -> A). Hint: try using the.sort()method with a callback to do custom sorting
