Practicing styling with styled-components and error handling
This CodeLab exercise is designed to help you practice the concepts you have learned in the Frontend Week IV module. You will be working with a partner to complete the tasks in this exercise.
The assignment
Basic setup with pages
- Create a new React application using
npm create vite@latest
and choose the React template. - Install the following dependencies:
styled-components
react-router-dom@6.28.0
- Create 2 new folders called
components
andpages
in thesrc
folder. - In package.json add a new script called
json-server
that runsjson-server --watch db.json --port 3000
. - Get the
db.json
file from here and place it in the root of your project. - Create the following pages:
Home
page that Shows an image of a truck and explains what the app is about (Administering a trucking company).Trucks
a page that displays a list of trucks. Each truck should have a name and a description with weight and capacity.Drivers
a page that displays a list of drivers. Each driver should have a name, and a description (with driver details)RegisterDriver
a page that allows you to register a new driver. Something like this
Components
- Create a
Header
component that displays a navigation bar with links to the different pages. - Create a
Footer
component that displays the text “© 2024 Trucking Company”. - Create a
TruckCard
component that displays a truck with a name, description, and picture. The card should have its own route and be displayed when you click on a truck. - Create a
DriverCard
component that displays a driver with a name, description, and picture. The card should have its own route and be displayed when you click on a driver.
Styling and error handling
- Style everything using
styled-components
. Add a theme object with colors and fonts and use it in your components with theThemeProvider
. - Add error handling to the
TruckCard
andDriverCard
components. If the truck or driver does not exist, display an error message in an error banner in the main Outlet component. - Add a 404 page that displays a message saying “Page not found” and a link to the home page.
- Add a 500 error page that displays a message saying “Server error” when a component can not render due to an error.
- Add try-catch blocks to the fetch requests in the
TruckCard
andDriverCard
components to catch errors and display the error message in the error banner.
Extra
- Implement buttons to delete or edit trucks and drivers. The edit button should take you to a form where you can edit the truck or driver.