Skip to content

Commit 572959e

Browse files
feat: port shopping-cart-context from JS to TS
1 parent 0bbfe7d commit 572959e

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Header from './components/Header.jsx';
22
import Shop from './components/Shop.jsx';
33
import Product from './components/Product.jsx';
44
import { DUMMY_PRODUCTS } from './dummy-products.js';
5-
import { ShoppingCartProvider } from './store/shopping-cart-context.jsx';
5+
import { ShoppingCartProvider } from './store/shopping-cart-context';
66

77
function App() {
88
return (

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Cart.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
1+
import { useShoppingCart } from '../store/shopping-cart-context';
22

33
export default function Cart() {
44
const { state: { items }, dispatch } = useShoppingCart();

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Header.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useRef } from 'react';
22

33
import CartModal from './CartModal.jsx';
4-
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
4+
import { useShoppingCart } from '../store/shopping-cart-context';
55

66
export default function Header() {
77
const modal = useRef();

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Product.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
1+
import { useShoppingCart } from '../store/shopping-cart-context';
22

33
export default function Product({ id, image, title, price, description }) {
44
const { shoppingCartDispatch } = useShoppingCart();
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
import { createContext, useReducer, useContext } from 'react';
1+
import * as React from 'react';
22

33
import { DUMMY_PRODUCTS } from '../dummy-products.js';
44

5-
export const ShoppingCartContext = createContext();
5+
type State = {
6+
items: {
7+
id: string;
8+
name: string;
9+
price: number;
10+
quantity: number;
11+
}[];
12+
}
13+
14+
type Action =
15+
| { type: 'ADD_ITEM'; payload: string; }
16+
| { type: 'UPDATE_ITEM'; payload: { productId: string; amount: number; }; };
17+
18+
type Dispatch = (action: Action) => void;
19+
20+
const ShoppingCartContext = React.createContext<
21+
{ state: State; dispatch: Dispatch; } | undefined
22+
>(undefined);
623

7-
function shoppingCartReducer(state, action) {
24+
function shoppingCartReducer(state: State, action: Action): State {
825
switch (action.type) {
926
case 'ADD_ITEM': {
1027
const updatedItems = [...state.items];
@@ -24,6 +41,9 @@ function shoppingCartReducer(state, action) {
2441
const product = DUMMY_PRODUCTS.find(
2542
(product) => product.id === action.payload
2643
);
44+
if (!product) {
45+
throw new Error(`Product not found: ${action.payload}`);
46+
}
2747
updatedItems.push({
2848
id: action.payload,
2949
name: product.title,
@@ -62,13 +82,17 @@ function shoppingCartReducer(state, action) {
6282
};
6383
}
6484
default: {
65-
throw new Error(`Unhandled action type: ${action.type}`);
85+
throw new Error(`Unhandled action type: ${action}`);
6686
}
6787
}
6888
}
6989

70-
function ShoppingCartProvider({ children }) {
71-
const [state, dispatch] = useReducer(shoppingCartReducer, { items: [] }
90+
interface ShoppingCartProviderProps {
91+
children: React.ReactNode;
92+
}
93+
94+
function ShoppingCartProvider({ children }: ShoppingCartProviderProps) {
95+
const [state, dispatch] = React.useReducer(shoppingCartReducer, { items: [] }
7296
);
7397

7498
const value = {
@@ -82,10 +106,11 @@ function ShoppingCartProvider({ children }) {
82106
}
83107

84108
function useShoppingCart() {
85-
const context = useContext(ShoppingCartContext);
109+
const context = React.useContext(ShoppingCartContext);
86110
if (!context) {
87111
throw new Error('useShoppingCart must be used within a ShoppingCartProvider');
88112
}
113+
89114
return context;
90115
}
91116

0 commit comments

Comments
 (0)