Delving into layered architecture
Our application has transitioned wonderfully to a more robust state, with clear, understandable, and modifiable logic, which is now also more test-friendly.
A further refinement I envision is relocating the logic present in ShoppingCart
to a custom Hook. We can do this like so:
export const useShoppingCart = (items: IMenuItem[]) => { const totalPrice = useMemo( () => items.reduce((acc, item) => (acc += item.price), 0), [items] ); const totalDiscount = useMemo( () => items.reduce((acc, item) => (acc += item. calculateDiscount()), 0), [items] ); return { totalPrice, totalDiscount, }; };
The useShoppingCart
Hook accepts an array of IMenuItem
objects and computes two values – totalPrice...