Below is how the code works and any assumptions made:
The Basket component is a functional component that takes three props: catalog
, deliveryRules
, and offers
. These props represent the product catalog, delivery charge rules, and special offers, respectively.
State Management:
It uses the useState hook to manage the state of the items in the basket (items).
addItem Function:
The addItem
function is responsible for adding a product to the basket. It takes a productCode as a parameter.
It uses the catalog prop to find the product information corresponding to the given productCode.
If a product with the provided code is found, it updates the items state to include the new product.
calculateTotal Function:
The calculateTotal
function computes the total cost of the items in the basket, taking into consideration delivery charges and special offers.
It uses the items state to calculate the subtotal by summing up the prices of all items.
It then iterates over the deliveryRules
prop to determine the appropriate delivery charge based on the subtotal.