Skip to content

Commit 33da8bd

Browse files
yasiloghmaniyasiloghmani
andauthored
Product variations and Cart-Expiry (#105)
* Just show parents product in the catagory page (Don't show child products in the catagory) * Update service to get product by Id , Update product details page to get the child data * update SDK version + Add cart expiry date do cart list and v=cart info * Remove useEffect to get outof stock product * Update variant products * Show all childs in catagory * Update CartItemList.scss * Update CartItemList.tsx Co-authored-by: yasiloghmani <yasaman.loghmani@EP-ML-10062.local>
1 parent 9cdcf06 commit 33da8bd

File tree

10 files changed

+75
-23
lines changed

10 files changed

+75
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "3.0.0",
44
"private": true,
55
"dependencies": {
6-
"@moltin/sdk": "6.1.2",
6+
"@moltin/sdk": "6.3.0",
77
"@types/node": "^12.0.0",
88
"@types/react": "^16.9.0",
99
"@types/react-dom": "^16.9.0",

src/CartItemList.scss

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
&__header{
1919
line-height: 34px;
20-
padding-bottom: 50px;
2120
padding-top: 20px;
21+
padding-bottom: 15px;
2222
max-width: 300px;
2323
margin: 0 auto;
2424
display: flex;
@@ -330,6 +330,14 @@
330330
}
331331
}
332332

333+
&__date {
334+
padding-bottom: 50px;
335+
}
336+
337+
&__expirydate {
338+
text-align: center;
339+
}
340+
333341
@media (min-width: $mobileXSmallWidth) {
334342
&__backicon {
335343
display: none;
@@ -452,4 +460,4 @@
452460
justify-content: flex-end;
453461
}
454462
}
455-
}
463+
}

src/CartItemList.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { SettingsCart } from "./SettingsCart";
1212
import { ReactComponent as CloseIcon } from './images/icons/ic_close.svg';
1313
import { ReactComponent as BackArrowIcon } from './images/icons/arrow_back-black-24dp.svg';
1414

15-
1615
import './CartItemList.scss';
1716

1817
interface CartItemListParams {
@@ -142,7 +141,6 @@ export const CartItemList: React.FC<CartItemListParams> = (props) => {
142141
<CloseIcon onClick={() => setShowUpdateCartAlert(false)}/>
143142
</div>
144143
)}
145-
146144
{mergedMessage && (
147145
<div className="cartslist__alertMessage">
148146
<p>{mergedMessage}</p>
@@ -163,7 +161,6 @@ export const CartItemList: React.FC<CartItemListParams> = (props) => {
163161
</div>
164162
)}
165163
</div>
166-
167164
<div className="cartitemlist__header">
168165
<h2 className="cartitemlist__title">
169166
{isLoggedIn && selectedCart ? (
@@ -180,6 +177,13 @@ export const CartItemList: React.FC<CartItemListParams> = (props) => {
180177
{isLoggedIn && selectedCart && <SettingsIcon onClick={() => setShowSettings(true)} /> }
181178
</span>
182179
</div>
180+
<div className="cartitemlist__date">
181+
{ isLoggedIn && selectedCart?.meta.timestamps.expires_at &&
182+
<p className="cartitemlist__expirydate">
183+
{t('expires')} - {(selectedCart?.meta.timestamps.expires_at).substring(0, 10)}
184+
</p>
185+
}
186+
</div>
183187
{items && items.length > 0 ? (
184188
<div>
185189
<div className="cartitemlist__wrap">

src/CartsList.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ export const CartsList: React.FC<CartsListParams> = (props) => {
170170
<span className="cartslist__date">
171171
{t('edited')} - {(cart.meta.timestamps.updated_at).substring(0, 10)}
172172
</span>
173+
<br />
174+
<span className="cartslist__date">
175+
{t('expires')} - {(cart.meta.timestamps.expires_at).substring(0, 10)}
176+
</span>
173177
</span>
174178
</div>
175179
<p className="cartslist__quantity">
@@ -220,6 +224,10 @@ export const CartsList: React.FC<CartsListParams> = (props) => {
220224
<span className="cartslist__date">
221225
{t('edited')} - {(cart.meta.timestamps.updated_at).substring(0, 10)}
222226
</span>
227+
<br />
228+
<span className="cartslist__date">
229+
{t('expires')} - {(cart.meta.timestamps.expires_at).substring(0, 10)}
230+
</span>
223231
</span>
224232
</div>
225233
<p className="cartslist__quantity">

src/LoginForm.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export const LoginForm: React.FC<LoginFormProps> = (props) => {
7878
handleShowNewCart(true);
7979
}
8080
if (createCart) {
81-
console.log("hello")
8281
setIsCreateNewCart(true);
8382
}
8483
if (onSubmit) {

src/Product.tsx

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState, useContext } from 'react';
22
import { useParams } from 'react-router-dom';
33
import useOnclickOutside from 'react-cool-onclickoutside';
44
import { useResolve, useProductImages } from './hooks';
5-
import { addToCart, loadProductBySlug } from './service';
5+
import { addToCart, loadProductBySlug, getProductById } from './service';
66
import { CompareCheck } from './CompareCheck';
77
import { SocialShare } from './SocialShare';
88
import {
@@ -23,7 +23,6 @@ import { APIErrorContext } from './APIErrorProvider';
2323

2424
import './Product.scss';
2525

26-
2726
interface ProductParams {
2827
productSlug: string;
2928
}
@@ -61,17 +60,37 @@ export const Product: React.FC = () => {
6160
},
6261
[productSlug, selectedLanguage, selectedCurrency, addError]
6362
);
63+
64+
const [childProduct, setChildProduct] = useState<string>(product?.relationships.children ? product.relationships.children.data[0].id : "")
65+
66+
const handleVariationChange = (childID: string) => {
67+
setProductId(childID);
68+
setChildProduct(childID)
69+
};
70+
71+
const [child] = useResolve(
72+
async () => {
73+
try {
74+
if(childProduct.length > 0)
75+
return getProductById(childProduct)
76+
}
77+
catch (error) {
78+
addError(error.errors);
79+
}
80+
}, [childProduct, addError]
81+
)
82+
6483
const [productId, setProductId] = useState('');
6584

6685
useEffect(() => {
67-
product && setProductId(product.id);
86+
product && setProductId(product.id);
6887
}, [product]);
6988

7089
useEffect(() => {
7190
document.body.style.overflow = modalOpen ? 'hidden' : 'unset';
7291
}, [modalOpen])
7392

74-
const productImageHrefs = useProductImages(product);
93+
const productImageHrefs = useProductImages(child ? child : product);
7594
const [currentImageIndex, setCurrentImageIndex] = useState(0);
7695
const isPrevImageVisible = currentImageIndex > 0;
7796
const isNextImageVisible = currentImageIndex < (productImageHrefs?.length ?? 0) - 1;
@@ -81,6 +100,15 @@ export const Product: React.FC = () => {
81100
setCurrentImageIndex(currentImageIndex - 1);
82101
};
83102

103+
useEffect(() => {
104+
if(product)
105+
if(product.relationships.children) {
106+
setChildProduct(product.relationships.children.data[0].id);
107+
setProductId(product.relationships.children.data[0].id)
108+
109+
}
110+
}, [product])
111+
84112
const handleAddToCart = (cartId?: string) => {
85113
const currentCart = localStorage.getItem("mcart") || "";
86114
const mcart = cartId ? cartId : currentCart;
@@ -105,10 +133,6 @@ export const Product: React.FC = () => {
105133
setCurrentImageIndex(currentImageIndex + 1);
106134
};
107135

108-
const handleVariationChange = (childID: string) => {
109-
setProductId(childID);
110-
};
111-
112136
const handleAddToSelectedCart = (cart:any) => {
113137
updateSelectedCart(cart);
114138
handleAddToCart(cart.id);
@@ -218,14 +242,14 @@ export const Product: React.FC = () => {
218242
{product.name}
219243
</h1>
220244
<div className="product__sku">
221-
{product.sku}
245+
{child ? child.sku : product.sku}
222246
</div>
223247
<div className="product__price">
224-
{product.meta.display_price.without_tax.formatted}
248+
{child ? child.meta.display_price.without_tax.formatted : product.meta.display_price.without_tax.formatted}
225249
</div>
226-
<Availability available={isProductAvailable(product)} />
250+
<Availability available={child ? isProductAvailable(child) : isProductAvailable(product)} />
227251
<div className="product__comparecheck">
228-
<CompareCheck product={product} />
252+
<CompareCheck product={child ? child : product} />
229253
</div>
230254
{
231255
product.meta.variations
@@ -238,10 +262,10 @@ export const Product: React.FC = () => {
238262
</div>
239263
</div>
240264
<div className="product__description">
241-
{product.description}
265+
{child ? child.description : product.description}
242266
</div>
243267
<div className="product__socialshare">
244-
<SocialShare name={product.name} description={product.description} imageHref={productImageHrefs?.[0]} />
268+
<SocialShare name={child ? child.name : product.name} description={child ? child.description : product.description} imageHref={productImageHrefs?.[0]} />
245269
</div>
246270
</div>
247271
</div>

src/VariationsSelector.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface VariationsSelectorParams {
1212
export const VariationsSelector: React.FC<VariationsSelectorParams> = ({ product, onChange }) => {
1313
const { variations, variation_matrix: variationMatrix } = product.meta;
1414

15+
1516
const initialOptions = variations.reduce(
1617
(acum: any, variation: any) => ({ ...acum, [variation.id]: variation.options[0].id }),
1718
{}
@@ -45,6 +46,7 @@ export const VariationsSelector: React.FC<VariationsSelectorParams> = ({ product
4546
processOptions(updatedOptions);
4647
};
4748

49+
4850
return (
4951
<div className="variationsselector">
5052
{variations.map((variation: any) => (

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
"new-cart": "New Cart",
191191
"edited": "Edited",
192192
"created": "Created",
193+
"expires": "Expires",
193194
"selected": "Selected",
194195
"saved-carts": "Saved carts",
195196
"done": "Done",

src/locales/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
"new-cart": "ÑÑéŵ ÇÇåŕţ",
191191
"edited": "ÉÉðîţţéð",
192192
"created": "ÇÇŕéååţéð",
193+
"expires": "ÉÉẋþîîŕéš",
193194
"selected": "ŠŠéļéççţéð",
194195
"saved-carts": "ŠŠåṽṽéð ççåŕŕţš",
195196
"done": "ÐÐöñé",

src/service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ export async function loadProductBySlug(productSlug: string, language: string, c
128128
const productId = resultSlug?.data[0]?.id;
129129
const result = await moltin.Products.Get(productId);
130130
const product = result.data;
131-
132131
setProductCache(product.slug, language, currency, product);
133132

134133
return product;
@@ -212,11 +211,17 @@ export async function getAllOrders(token: string, pageNum:number,dates: number,
212211
return result;
213212
}
214213

214+
export async function getProductById(productId: string): Promise<moltin.Product> {
215+
const moltin = MoltinGateway({ host: config.endpointURL, client_id: config.clientId });
216+
const result = await moltin.Products.Get(productId);
217+
const product = result.data;
218+
return product
219+
}
220+
215221
export async function getProductsByIds(ids: string[]): Promise<any> {
216222
const moltin = MoltinGateway({ host: config.endpointURL, client_id: config.clientId });
217223
const productsRequests = ids.map(id => moltin.Products.Get(id));
218224
const products = await Promise.all(productsRequests);
219-
220225
return products.map(product => product.data)
221226
}
222227

0 commit comments

Comments
 (0)