From bd62835db7dc521a17d6b0aea81badaae6ade95c Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 2 Aug 2024 16:47:42 +0700 Subject: update template switch --- .../product-card/components/ProductCard.tsx | 150 ++++++++++++++++++--- src-migrate/services/product.ts | 37 ++++- src-migrate/types/product.ts | 6 +- 3 files changed, 171 insertions(+), 22 deletions(-) (limited to 'src-migrate') diff --git a/src-migrate/modules/product-card/components/ProductCard.tsx b/src-migrate/modules/product-card/components/ProductCard.tsx index 0febfadb..3e34d5c3 100644 --- a/src-migrate/modules/product-card/components/ProductCard.tsx +++ b/src-migrate/modules/product-card/components/ProductCard.tsx @@ -10,6 +10,10 @@ import formatCurrency from '~/libs/formatCurrency' import { formatToShortText } from '~/libs/formatNumber' import { createSlug } from '~/libs/slug' import { IProduct } from '~/types/product' +import { useRouter } from 'next/router'; +import currencyFormat from '@/core/utils/currencyFormat'; +import whatsappUrl from '@/core/utils/whatsappUrl'; +import { sellingProductFormat } from '@/core/utils/formatValue'; type Props = { product: IProduct @@ -18,7 +22,14 @@ type Props = { const ProductCard = ({ product, layout = 'vertical' }: Props) => { const utmSource = useUtmSource() - + const router = useRouter(); + + const id = product.id.toString () + const callForPriceWhatsapp = whatsappUrl('product', { + name: product.name, + manufacture: product.manufacture?.name, + url: createSlug('/shop/product/', product.name, id, true), + }); const URL = { product: createSlug('/shop/product/', product.name, product.id.toString()) + `?utm_source=${utmSource}`, @@ -75,6 +86,42 @@ const ProductCard = ({ product, layout = 'vertical' }: Props) => { + {layout === 'vertical' && ( +
+ {router.pathname != '/' && product?.flash_sale.id > 0 && ( +
+
+ +
+
+
+
+ + {Math.floor(product?.lowest_price.discount_percentage ? product?.lowest_price.discount_percentage : product?.lowest_price.discountPercentage)}% + +
+
+ + + {product?.flash_sale?.tag != 'false' || + product?.flash_sale?.tag + ? product?.flash_sale?.tag + : 'FLASH SALE'} + +
+
+
+
+ )} +
+ )} {product.variant_total > 1 && (
{product.variant_total} Varian
@@ -86,6 +133,24 @@ const ProductCard = ({ product, layout = 'vertical' }: Props) => { [style['content-v']]: layout === 'vertical', [style['content-h']]: layout === 'horizontal', })}> + {layout === 'horizontal' && ( +
+ {product.flash_sale.id > 0 && ( +
+ + + {' '} + {product?.flash_sale?.tag != 'false' || product?.flash_sale?.tag + ? product?.flash_sale?.tag + : 'FLASH SALE'} + +
+ )} +
+ )} { {product.name}
- -
+ {product?.flash_sale?.id > 0 ? (
) : + ( +
Rp {formatCurrency(product.lowest_price.price)}
+ ) + } + +
-
- Inc PPN: - Rp {formatCurrency(Math.round(product.lowest_price.price * 1.11))} -
+ {product?.flash_sale?.id > 0 && + product?.lowest_price?.discountPercentage > 0 ? ( + <> + {product?.lowest_price.discountPercentage > 0 && ( +
+
+ {Math.floor(product?.lowest_price.discount_percentage ? product?.lowest_price.discount_percentage : product?.lowest_price.discountPercentage)}% +
+
+ {currencyFormat(product?.lowest_price?.price)} +
+
+ )} -
+
+ {product?.lowest_price?.price_discount ? product?.lowest_price?.price_discount : product?.lowest_price?.priceDiscount > 0 ? ( + currencyFormat(product?.lowest_price?.price_discount ? product?.lowest_price?.price_discount : product?.lowest_price?.priceDiscount) + ) : ( + + Call for Inquiry + + )} +
+
+ {product?.stockTotal > 0 && ( +
Ready Stock
+ )} + {/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/} + {product?.qtySold > 0 && ( +
+ {sellingProductFormat(product?.qtySold) + ' Terjual'} +
+ )} +
+ + ) : ( +
+
+ Inc PPN: + Rp {formatCurrency(Math.round(product.lowest_price.price * 1.11))} +
-
- {product.stock_total > 0 && ( -
- Ready Stock -
- )} - {product.qty_sold > 0 && ( -
- {formatToShortText(product.qty_sold)} Terjual +
+ +
+ {product.stock_total > 0 && ( +
+ Ready Stock +
+ )} + {product.qty_sold > 0 && ( +
+ {formatToShortText(product.qty_sold)} Terjual +
+ )} +
)} -
+
diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts index fe415d11..51667eb9 100644 --- a/src-migrate/services/product.ts +++ b/src-migrate/services/product.ts @@ -2,6 +2,7 @@ import { IProduct, IProductDetail } from '~/types/product'; import snakeCase from 'snakecase-keys'; import odooApi from '~/libs/odooApi'; import { ICategoryBreadcrumb } from '~/types/category'; +import productSearchApi from '../../src/lib/product/api/productSearchApi' const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST; @@ -40,6 +41,27 @@ export const getProductSimilar = async ({ except, limit = 30, }: GetProductSimilarProps): Promise => { + let dataflashSale = null + const flashSale = await odooApi('GET', '/api/v1/flashsale/header') + if (flashSale && flashSale.length > 0) { + const dataFlash = await productSearchApi({ + query: `fq=flashsale_id_i:${flashSale[0].pricelist_id}&fq=flashsale_price_f:[1 TO *]&limit=4`, + operation: 'AND' + }) + + dataflashSale = dataFlash.response.products.map((product: { + qtySold: any; + stockTotal: any; + flashSale: any; lowestPrice: any; +}) => ({ + ...product, + lowest_price: product.lowestPrice, + flash_sale: product.flashSale, + stock_total: product.stockTotal, + qty_sold: product.qtySold, + lowestPrice: undefined + })); + } const query = [ `q=${name}`, 'page=1', @@ -47,16 +69,25 @@ export const getProductSimilar = async ({ 'operation=OR', 'priceFrom=1', ]; - if (except?.productId) query.push(`fq=-product_id_i:${except.productId}`); if (except?.manufactureId) query.push(`fq=-manufacture_id_i:${except.manufactureId}`); - const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`; - return await fetch(url) + const fetchedData = await fetch(url) .then((res) => res.json()) .then((res) => snakeCase(res.response)); + + if (dataflashSale) { + fetchedData.products = [ + ...dataflashSale, + ...fetchedData.products + ] + } else { + fetchedData.products = [...fetchedData.products] + } + + return fetchedData; }; export const getProductCategoryBreadcrumb = async ( diff --git a/src-migrate/types/product.ts b/src-migrate/types/product.ts index 681cdc8e..75d55002 100644 --- a/src-migrate/types/product.ts +++ b/src-migrate/types/product.ts @@ -1,6 +1,8 @@ import { IProductVariantDetail } from './productVariant'; export interface IProduct { + qtySold: number; + stockTotal: number; id: number; image: string; code: string; @@ -18,11 +20,13 @@ export interface IProduct { name: string; }[]; flash_sale: { - id: string; + id: number; remaining_time: number; tag: string; }; lowest_price: { + priceDiscount: number; + discountPercentage: number; price: number; price_discount: number; discount_percentage: number; -- cgit v1.2.3