Skip to content

Commit 5179cba

Browse files
committed
Add to cart Typescript
1 parent 9128b75 commit 5179cba

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

refactor/src/components/Product/AddToCart.component.tsx

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,70 @@ import { getFormattedCart } from '@/utils/functions/functions';
1616
import { GET_CART } from '@/utils/gql/GQL_QUERIES';
1717
import { ADD_TO_CART } from '@/utils/gql/GQL_MUTATIONS';
1818

19-
interface IAddToCartProps {
20-
product: { databaseId: number };
19+
interface IImage {
20+
__typename: string;
21+
id: string;
22+
uri: string;
23+
title: string;
24+
srcSet: string;
25+
sourceUrl: string;
26+
}
27+
28+
interface IVariationNode {
29+
__typename: string;
30+
name: string;
31+
}
32+
33+
interface IAllPaColors {
34+
__typename: string;
35+
nodes: IVariationNode[];
36+
}
37+
38+
interface IAllPaSizes {
39+
__typename: string;
40+
nodes: IVariationNode[];
41+
}
42+
43+
interface IVariationNodes {
44+
__typename: string;
45+
id: string;
46+
databaseId: number;
47+
name: string;
48+
stockStatus: string;
49+
stockQuantity: number;
50+
purchasable: boolean;
51+
onSale: boolean;
52+
salePrice?: any;
53+
regularPrice: string;
54+
}
55+
56+
interface IVariations {
57+
__typename: string;
58+
nodes: IVariationNodes[];
59+
}
60+
61+
export interface IProduct {
62+
__typename: string;
63+
id: string;
64+
databaseId: number;
65+
averageRating: number;
66+
slug: string;
67+
description: string;
68+
onSale: boolean;
69+
image: IImage;
70+
name: string;
71+
salePrice?: string;
72+
regularPrice: string;
73+
price: string;
74+
stockQuantity: number;
75+
allPaColors?: IAllPaColors;
76+
allPaSizes?: IAllPaSizes;
77+
variations?: IVariations;
78+
}
79+
80+
export interface IProductRootObject {
81+
product?: IProduct;
82+
variationId?: number;
2183
}
2284

2385
/**
@@ -26,13 +88,11 @@ interface IAddToCartProps {
2688
* @param {IAddToCartProps} product // Product data
2789
*/
2890

29-
const AddToCart = (product: IAddToCartProps) => {
91+
const AddToCart = ({ product, variationId }: IProductRootObject) => {
3092
const { setCart } = useContext(CartContext);
3193
const [requestError, setRequestError] = useState<boolean>(false);
3294

33-
const productId = product.product.databaseId
34-
? product.product.databaseId
35-
: product.product;
95+
const productId = product?.databaseId ? product?.databaseId : variationId;
3696

3797
const productQueryInput = {
3898
clientMutationId: uuidv4(), // Generate a unique id.

refactor/src/components/Product/SingleProduct.component.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ import { useState, useEffect } from 'react';
66
import { filteredVariantPrice, paddedPrice } from '@/utils/functions/functions';
77

88
// Components
9-
import AddToCart from './AddToCart.component';
9+
import AddToCart, { IProduct } from './AddToCart.component';
1010
import LoadingSpinner from '@/components/LoadingSpinner/LoadingSpinner.component';
1111

12-
/**
13-
* Shows a single product with an Add To Cart button.
14-
* Uses GraphQL for product data
15-
* @param {Object} product // Product data
16-
*/
17-
const SingleProduct = ({ product }: any) => {
12+
const SingleProduct = (product: IProduct) => {
1813
const [isLoading, setIsLoading] = useState<boolean>(true);
1914
const [selectedVariation, setSelectedVariation] = useState<number>();
2015
let DESCRIPTION_WITHOUT_HTML;
@@ -122,11 +117,11 @@ const SingleProduct = ({ product }: any) => {
122117
name="variant"
123118
className="block w-80 px-6 py-2 bg-white border border-gray-500 rounded-lg focus:outline-none focus:shadow-outline"
124119
onChange={(e) => {
125-
setSelectedVariation(e.target.value);
120+
setSelectedVariation(Number(e.target.value));
126121
}}
127122
>
128123
{product.variations.nodes.map(
129-
({ id, name, databaseId, stockQuantity }: any) => {
124+
({ id, name, databaseId, stockQuantity }) => {
130125
// Remove product name from variation name
131126
const filteredName = name.split('- ').pop();
132127
return (
@@ -145,7 +140,7 @@ const SingleProduct = ({ product }: any) => {
145140
// If we do, send the variationId to AddToCart button
146141
}
147142
{product.variations && (
148-
<AddToCart product={selectedVariation} />
143+
<AddToCart variationId={selectedVariation} />
149144
)}
150145
{!product.variations && <AddToCart product={product} />}
151146
</div>

0 commit comments

Comments
 (0)