A simple web application that allows users to add food items to a cart, view the total price, and checkout.
- Display a list of available foods from db.json
- Add food items to cart
- Adjust quantity of items in cart
- Remove items from cart
- View total price of items in cart
- Checkout functionality
- Node.js installed on your machine
- json-server installed (will be installed with npx)
index.html- Main HTML file for the applicationstyles.css- CSS styling for the applicationscript.js- JavaScript functionalitydb.json- Database file with food items
- To add more food items, edit the
db.jsonfile - Each food item should have:
- id
- name
- emoji
- stock
- price
-
Open the
index.htmlfile in your browser- If using Visual Studio Code, you can use the Live Server extension
-
Interact with the application:
- Browse available food items
- Add items to your cart
- Adjust quantities or remove items
- Click "Checkout" when ready
Overview: In this exercise, you will transform the application from using local data to fetching data from a JSON server API endpoint.
Current Implementation: The starter code uses a local array for storing food items:
// Initial food data - In memory data source
let foods = [
{
"id": 1,
"name": "Pizza",
"emoji": "🍕",
"stock": 10,
"price": 8.99
},
// More food items...
];Tasks:
- Examine the structure of the
db.jsonfile to understand the data format - Start the JSON Server:
npx json-server --watch db.json
- Implement the
fetchFoods()function to:- Use the Fetch API to get foods from
http://localhost:3000/foods - Parse the JSON response
- Update the
foodsarray with the fetched data - Handle potential errors with appropriate user feedback
- Use the Fetch API to get foods from
- Modify the
initializeFoods()function to call your new fetch implementation - Test that the food items load correctly from the server
Learning Objectives:
- Understanding asynchronous JavaScript with
async/await - Working with the Fetch API
- Handling API responses and errors
- Updating the UI based on fetched data
Overview: When a user checks out their cart, the server should be updated to reflect the new stock levels.
Current Implementation:
The checkout function only updates the local foods array:
cart.forEach(item => {
const currentFood = foods.find(food => food.id === item.id);
if (currentFood) {
// Calculate and update new stock locally
currentFood.stock -= item.quantity;
}
});Tasks:
- Modify the checkout function to use the Fetch API with PATCH requests
- For each item in the cart:
- Calculate the new stock level
- Send a PATCH request to
http://localhost:3000/foods/{id} - Include the appropriate headers and JSON body
- Handle success and error responses
- Wait for all PATCH requests to complete before showing the success message
- Refresh the food data after successful checkout to show updated stock levels
Learning Objectives:
- Understanding HTTP PATCH method
- Creating and sending HTTP requests with headers and body
- Managing multiple asynchronous operations with
Promise.all(optional) - Handling API responses and errors
- Test your API endpoints using
curlor Postman before implementing them in code - Check the browser's developer console for error messages
- Make sure json-server is running before testing your implementation
- Remember to refresh the food list after updating stock levels
- Use try/catch blocks to handle potential errors gracefully
- GET all foods:
GET http://localhost:3000/foods - GET a specific food:
GET http://localhost:3000/foods/{id} - Update a food item:
PATCH http://localhost:3000/foods/{id}- Body:
{ "stock": newStockValue } - Headers:
{ "Content-Type": "application/json" }
- Body: