From b7def06cf32bf86036056718ece2350b4da72d23 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Fri, 5 Apr 2024 15:43:24 +0700 Subject: recomendation --- src/pages/api/shop/generate-recomendation.js | 6 +- .../components/products-recomendatison.jsx | 141 ++++++++++++++++++--- 2 files changed, 126 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/pages/api/shop/generate-recomendation.js b/src/pages/api/shop/generate-recomendation.js index a2a9ee40..11dd153e 100644 --- a/src/pages/api/shop/generate-recomendation.js +++ b/src/pages/api/shop/generate-recomendation.js @@ -3,7 +3,7 @@ import axios from 'axios'; import camelcaseObjectDeep from 'camelcase-object-deep'; export default async function handler(req, res) { - const { q = null } = req.query + const { q = null, op = 'AND' } = req.query if (!q) { return res.status(422).json({ error: 'parameter missing' }) @@ -11,13 +11,13 @@ export default async function handler(req, res) { let parameter = [ `q=${escapeSolrQuery(q)}`, - `q.op=AND`, + `q.op=${op}`, `indent=true`, `fq=-publish_b:false`, `qf=name_s^2 description_s`, `facetch=true`, `fq=price_tier1_v2_f:[1 TO *]`, - `rows=1`, + `rows=10`, `sort=product_rating_f DESC, price_discount_f DESC`, ]; let result = await axios( diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx index 72b858e8..bb5950c8 100644 --- a/src/pages/my/recomendation/components/products-recomendatison.jsx +++ b/src/pages/my/recomendation/components/products-recomendatison.jsx @@ -14,10 +14,11 @@ const ProductsRecomendation = ({ id }) => { const [isLoading, setIsLoading] = useState(false); const [isOpen, setIsOpen] = useState(false); const [variantsOpen, setVariantsOpen] = useState([]); + const [otherRec, setOtherRec] = useState(false); const mappingProducts = async ({ index, product, result, variants }) => { const resultMapping = { - index : index, + index: index, product: product, result: { id: result?.id || '-', @@ -34,25 +35,51 @@ const ProductsRecomendation = ({ id }) => { return resultMapping; }; - const searchRecomendation = async ({ product, index }) => { + const searchRecomendation = async ({ product, index, operator = 'AND' }) => { let variants = []; + let resultMapping = {}; const searchProduct = await axios.post( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=${operator}` ); - const result = - searchProduct.data.response.numFound > 0 - ? searchProduct.data.response.products[0] - : null; + if (operator === 'AND') { + const result = + searchProduct.data.response.numFound > 0 + ? searchProduct.data.response.products[0] + : null; - if (result?.variantTotal > 1) { - const searchVariants = await axios.post( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${result.id}` - ); - variants = searchVariants.data[0].variants; - } + if (result?.variantTotal > 1) { + const searchVariants = await axios.post( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${result.id}` + ); + variants = searchVariants.data[0].variants; + } + + resultMapping = await mappingProducts({ + index, + product, + result, + variants, + }); + } else { + const result = + searchProduct.data.response.numFound > 0 + ? searchProduct.data.response.products + : null; + + result.map((item) => { + if (item.variantTotal > 1) { + const searchVariants = axios.post( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${item.id}` + ); + variants = searchVariants.data[0].variants; + } + }); - const resultMapping = await mappingProducts({ index, product, result, variants }); + console.log('ini result', searchProduct.data.response); + + + } return resultMapping; }; @@ -102,6 +129,14 @@ const ProductsRecomendation = ({ id }) => { } setIsOpen(false); }; + + const handlingOtherRec = ({ product }) => { + console.log('ini product', product); + const result = async () => + await searchRecomendation({ product, index: 0, operator: 'OR' }); + + result(); + }; return ( <> { + setOtherRec(false)} + className='w-full md:!w-[60%]' + title='Other Recomendations' + > +
+ + + + + + + + + + + + + {variantsOpen?.map((variant, index) => ( + + + + + + + ))} + +
ProductItem Code DescriptionBrandPriceImage
{variant.code}{variant.attributes.join(', ') || '-'} + {variant.price.discountPercentage > 0 && ( +
+
+ {Math.floor(variant.price.discountPercentage)}% +
+
+ Rp {formatCurrency(variant.price.price)} +
+
+ )} + {variant.price.priceDiscount > 0 && + `Rp ${formatCurrency(variant.price.priceDiscount)}`} + {variant.price.priceDiscount === 0 && '-'} +
+ +
+
+
@@ -216,10 +309,10 @@ const ProductsRecomendation = ({ id }) => { Product - Item code - Keterangan + Item Code + Description Brand - Harga + Price Image lainnya @@ -284,7 +377,19 @@ const ProductsRecomendation = ({ id }) => { '-' )} - + + {' '} + + ))} -- cgit v1.2.3