Skip to content

Commit 25827e1

Browse files
fix CartPage thumbnail link problem, change from using id to slug
1 parent 33de145 commit 25827e1

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

src/components/AddToCart/AddToCart.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const AddToCart: React.FC<{ singleProduct: productDataType | {} }> = ({
1010
}) => {
1111
const { addToCart } = useCartContext()
1212
// need the number of stock here as well after setting up in productData array
13-
const { id } = { ...singleProduct }
13+
const { id, slug } = { ...singleProduct }
1414
const [amount, setAmount] = useState(1)
1515

1616
// if there's stock variable, add logic to allow adding the amount === stock
@@ -33,7 +33,7 @@ const AddToCart: React.FC<{ singleProduct: productDataType | {} }> = ({
3333
<Link
3434
to='/cart'
3535
className='btn'
36-
onClick={() => addToCart(id, amount, singleProduct)}
36+
onClick={() => addToCart(id, slug, amount, singleProduct)}
3737
>
3838
add to cart
3939
</Link>

src/components/CartItem.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cartType, useCartContext } from '../context/cart_context'
77
import { Link } from 'react-router-dom'
88

99
const CartItem: React.FC<{ cartItem: cartType }> = ({ cartItem }) => {
10-
const { id, image, name, price, amount } = cartItem
10+
const { id, image, name, price, amount, slug } = cartItem
1111

1212
const { removeItem, toggleAmount } = useCartContext()
1313

@@ -22,7 +22,7 @@ const CartItem: React.FC<{ cartItem: cartType }> = ({ cartItem }) => {
2222
<Wrapper>
2323
{/* item column */}
2424
<div className='title'>
25-
<Link to={`/products/${id}`}>
25+
<Link to={`/products/${slug}`}>
2626
<img src={image} alt={name} />
2727
</Link>
2828
<div>

src/context/cart_context.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
export type cartType = {
1313
id: string
14+
slug: string
1415
name: string
1516
amount: number
1617
image: string
@@ -24,6 +25,7 @@ export type initialStateType = {
2425
totalAmount: number
2526
addToCart: (
2627
id: string | undefined,
28+
slug: string | undefined,
2729
amount: number,
2830
singleProduct: productDataType | {}
2931
) => void
@@ -58,12 +60,13 @@ export const CartProvider: React.FC = ({ children }) => {
5860

5961
const addToCart = (
6062
id: string | undefined,
63+
slug: string | undefined,
6164
amount: number,
6265
singleProduct: productDataType | {}
6366
) => {
6467
dispatch({
6568
type: ADD_TO_CART,
66-
payload: { id, amount, singleProduct },
69+
payload: { id, slug, amount, singleProduct },
6770
})
6871
}
6972

src/reducers/cart_reducer.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const cart_reducer = (
1212
action: { type: any; payload?: any }
1313
) => {
1414
if (action.type === ADD_TO_CART) {
15-
const { id, amount, singleProduct } = action.payload
15+
const { id, slug, amount, singleProduct } = action.payload
1616
const tempItem = state.cart.find(item => item.id === id)
1717

1818
if (tempItem) {
@@ -29,6 +29,7 @@ const cart_reducer = (
2929
} else {
3030
const newItem: cartType = {
3131
id,
32+
slug,
3233
name: singleProduct.name,
3334
amount,
3435
image: singleProduct.images[0],

0 commit comments

Comments
 (0)