React CodeLab Exercise: Fetching and Displaying Trip Data
This CodeLab exercise is designed to help you practice creating React applications using Vite, fetching data from REST APIs, state management, conditional rendering, and creating dynamic UIs with interactive components. Collaboration and teamwork are emphasized, so consider pair programming where possible.
Exercise Overview
Instructions
1. Create a React Project
-
Create a new React project using Vite:
npm create vite@latest
- Choose your project name and framework (
React
) and variant (JavaScript
). -
Navigate to your new project folder and install dependencies:
cd [project-name] npm install npm run dev
- Ensure your Vite development server is running successfully.
2. Fetch Data from the Trip API
- Fetch data from the provided API endpoint: https://tripapi.cphbusinessapps.dk/api/trips.
- Structure your
fetch
call to retrieve the list of trips when your React app loads. UseuseEffect
anduseState
hooks for managing state and lifecycle. -
Verify the response data structure:
[ { "id": 11, "starttime": "2025-01-25T12:34:06.865979", "endtime": "2025-01-28T12:34:06.865979", "longitude": 14.94, "latitude": 58.75, "name": "Test", "price": 10.0, "category": "CITY", "guide": { "id": 1, "firstname": "Andreas", "lastname": "Turkey", "email": "andreas@mail.com", "phone": "33293922", "yearsOfExperience": 10, "trips": ["Beach Holiday", "City Tour", "Snow boarding", "Living under a bridge"] } }, ... ]
3. Display All Trips
- Render all trips on the left side of the screen as a list (or a table or as card layout or whatever you fancy).
- Display key information for each trip, such as:
- Trip name
- Start and end dates
- Price
- Duration
4. Category Filter
- Add a dropdown (
select
element) above the trip list that allows users to filter trips based on theircategory
. Get the categories from here: https://packingapi.cphbusinessapps.dk/packinglist/ - When a category is selected, only trips belonging to that category should be displayed.
5. Trip Details View
- When a trip is clicked, display a
Trip Details
view on the right side of the screen. Details about a trip can be found by id like this: https://tripapi.cphbusinessapps.dk/api/trips/11. - This view should display detailed trip information, including:
- Trip name
- Start and end dates
- Price
- Category
- Guide details (name, email, phone, years of experience)
6. Packing Items Table
- If the selected trip includes packing items, render them in a table within the
Trip Details
view. - Display the following columns:
- Item name
- Weight in grams
- Quantity
- Description
- Category
- Available buying options (shop name, price)
7. Calculations
- Below the table, calculate and display:
- The total summed weight of all packing items for the trip.
- The total cheapest price for each packing item, if applicable.
Good luck and have fun! Remember to read instructions carefully, collaborate with your teammates, and ask for help if needed.