diff options
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/api/shop/variant.js | 157 | ||||
| -rw-r--r-- | src/pages/google_merchant/products/[page].js | 3 | ||||
| -rw-r--r-- | src/pages/google_merchant/products/index.js | 3 | ||||
| -rw-r--r-- | src/pages/shop/product/variant/[slug].jsx | 75 |
4 files changed, 236 insertions, 2 deletions
diff --git a/src/pages/api/shop/variant.js b/src/pages/api/shop/variant.js new file mode 100644 index 00000000..30213cc3 --- /dev/null +++ b/src/pages/api/shop/variant.js @@ -0,0 +1,157 @@ +import axios from 'axios' +import camelcaseObjectDeep from 'camelcase-object-deep' + +export default async function handler(req, res) { + const { + q = '*', + page = 1, + brand = '', + category = '', + priceFrom = 0, + priceTo = 0, + orderBy = '', + operation = 'AND', + fq = '', + limit = 30 + } = req.query + + let paramOrderBy = '' + switch (orderBy) { + case 'price-asc': + paramOrderBy += 'price_discount_f ASC' + break + case 'price-desc': + paramOrderBy += 'price_discount_f DESC' + break + case 'popular': + paramOrderBy += 'search_rank_i DESC' + break + case 'popular-weekly': + paramOrderBy += 'search_rank_weekly_i DESC' + break + case 'stock': + paramOrderBy += 'stock_total_f DESC' + break + default: + paramOrderBy += 'product_rating_f DESC, price_discount_f DESC' + break + } + + let offset = (page - 1) * limit + let parameter = [ + 'facet.field=manufacture_name', + 'facet.field=category_name', + 'facet=true', + 'indent=true', + `facet.query=${escapeSolrQuery(q)}`, + `q.op=${operation}`, + `q=${escapeSolrQuery(q)}`, + 'qf=name_s', + `start=${offset}`, + `rows=${limit}`, + `sort=${paramOrderBy}` + ] + + if (priceFrom > 0 || priceTo > 0) { + parameter.push( + `fq=price_discount_f:[${priceFrom == '' ? '*' : priceFrom} TO ${ + priceTo == '' ? '*' : priceTo + }]` + ) + } + + if (brand) parameter.push(`fq=manufacture_name:${brand}`) + if (category) parameter.push(`fq=category_name:${category}`) + + // Single fq in url params + if (typeof fq === 'string') parameter.push(`fq=${fq}`) + // Multi fq in url params + if (Array.isArray(fq)) parameter = parameter.concat(fq.map((val) => `fq=${val}`)) + + let result = await axios(process.env.SOLR_HOST + '/solr/variants/select?' + parameter.join('&')) + try { + let { auth } = req.cookies + if (auth) auth = JSON.parse(auth) + result.data.response.products = productResponseMap( + result.data.response.docs, + auth?.pricelist || false + ) + result.data.responseHeader.params.start = parseInt(result.data.responseHeader.params.start) + result.data.responseHeader.params.rows = parseInt(result.data.responseHeader.params.rows) + delete result.data.response.docs + result.data = camelcaseObjectDeep(result.data) + res.status(200).json(result.data) + } catch (error) { + res.status(400).json({ error: error.message }) + } +} + +const escapeSolrQuery = (query) => { + if (query == '*') return query + + const specialChars = /([\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/])/g + const words = query.split(/\s+/) + const escapedWords = words.map((word) => { + if (specialChars.test(word)) { + return `"${word.replace(specialChars, '\\$1')}"` + } + return word + }) + + return escapedWords.join(' ') +} + +const productResponseMap = (products, pricelist) => { + return products.map((product) => { + let price = product.price_f || 0 + let priceDiscount = product.price_discount_f || 0 + let discountPercentage = product.discount_f || 0 + + if (pricelist) { + const pricelistDiscount = product?.[`price_${pricelist}_f`] || false + const pricelistDiscountPerc = product?.[`discount_${pricelist}_f`] || false + + if (pricelistDiscount && pricelistDiscount > 0) priceDiscount = pricelistDiscount + if (pricelistDiscountPerc && pricelistDiscountPerc > 0) + discountPercentage = pricelistDiscountPerc + } + + if (product?.flashsale_id_i > 0) { + price = product?.flashsale_base_price_f || 0 + priceDiscount = product?.flashsale_price_f || 0 + discountPercentage = product?.flashsale_discount_f || 0 + } + + let productMapped = { + id: product.product_id_i || '', + image: product.image_s || '', + code: product.default_code_s || '', + name: product.name_s || '', + lowestPrice: { price, priceDiscount, discountPercentage }, + variantTotal: product.variant_total_i || 0, + stockTotal: product.stock_total_f || 0, + weight: product.weight_f || 0, + manufacture: {}, + categories: [], + flashSale: { + id: product?.flashsale_id_i, + name: product?.product?.flashsale_name_s + } + } + + if (product.manufacture_id_i && product.manufacture_name_s) { + productMapped.manufacture = { + id: product.manufacture_id_i || '', + name: product.manufacture_name_s || '' + } + } + + productMapped.categories = [ + { + id: product.category_id_i || '', + name: product.category_name_s || '' + } + ] + return productMapped + }) +} diff --git a/src/pages/google_merchant/products/[page].js b/src/pages/google_merchant/products/[page].js index 4a7d95c6..f9eac45d 100644 --- a/src/pages/google_merchant/products/[page].js +++ b/src/pages/google_merchant/products/[page].js @@ -1,6 +1,7 @@ import { createSlug } from '@/core/utils/slug' import toTitleCase from '@/core/utils/toTitleCase' import productSearchApi from '@/lib/product/api/productSearchApi' +import variantSearchApi from '@/lib/product/api/variantSearchApi' import _ from 'lodash-contrib' import { create } from 'xmlbuilder' @@ -18,7 +19,7 @@ export async function getServerSideProps({ res, query }) { orderBy: 'popular', fq: 'image_s:["" TO *]' } - const products = await productSearchApi({ query: _.toQuery(queries) }) + const products = await variantSearchApi({ query: _.toQuery(queries) }) const productItems = [] products.response.products.forEach((product) => { diff --git a/src/pages/google_merchant/products/index.js b/src/pages/google_merchant/products/index.js index a1f59d39..d3cdc514 100644 --- a/src/pages/google_merchant/products/index.js +++ b/src/pages/google_merchant/products/index.js @@ -1,4 +1,5 @@ import productSearchApi from '@/lib/product/api/productSearchApi' +import variantSearchApi from '@/lib/product/api/variantSearchApi' import _ from 'lodash-contrib' const limit = 5000 @@ -9,7 +10,7 @@ export async function getServerSideProps() { priceFrom: 1, fq: 'image_s:["" TO *]' } - const products = await productSearchApi({ query: _.toQuery(queries) }) + const products = await variantSearchApi({ query: _.toQuery(queries) }) const { numFound } = products.response const pageTotal = Math.ceil(numFound / limit) diff --git a/src/pages/shop/product/variant/[slug].jsx b/src/pages/shop/product/variant/[slug].jsx new file mode 100644 index 00000000..ba2a79d5 --- /dev/null +++ b/src/pages/shop/product/variant/[slug].jsx @@ -0,0 +1,75 @@ +import Seo from '@/core/components/Seo' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import { getIdFromSlug } from '@/core/utils/slug' +import PageNotFound from '@/pages/404' +import dynamic from 'next/dynamic' +import { useRouter } from 'next/router' +import cookie from 'cookie' +import variantApi from '@/lib/product/api/variantApi' + +const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout')) +const Product = dynamic(() => import('@/lib/product/components/Product/Product')) + +export async function getServerSideProps(context) { + const { slug } = context.query + const cookies = context.req.headers.cookie + const cookieObj = cookies ? cookie.parse(cookies) : {} + const auth = cookieObj.auth ? JSON.parse(cookieObj.auth) : {} + const authToken = auth?.token || '' + + let product = await variantApi({ id: getIdFromSlug(slug), headers: { Token: authToken } }) + if (product?.length == 1) { + product = product[0] + /* const regexHtmlTags = /(<([^>]+)>)/gi + const regexHtmlTagsExceptP = /<\/?(?!p\b)[^>]*>/g + product.description = product.description + .replace(regexHtmlTagsExceptP, ' ') + .replace(regexHtmlTags, ' ') + .trim()*/ + } else { + product = null + } + + return { + props: { product } + } +} + +export default function ProductDetail({ product }) { + const router = useRouter() + + if (!product) return <PageNotFound /> + + return ( + <BasicLayout> + <Seo + title={product?.name || '' + ' - Indoteknik.com' || ''} + description='Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.' + openGraph={{ + url: process.env.NEXT_PUBLIC_SELF_HOST + router.asPath, + images: [ + { + url: product?.image, + width: 800, + height: 800, + alt: product?.name + } + ], + type: 'product' + }} + additionalMetaTags={[ + { + name: 'keywords', + content: `${product?.name}, Harga ${product?.name}, Beli ${product?.name}, Spesifikasi ${product?.name}` + } + ]} + /> + {!product && ( + <div className='container mx-auto flex justify-center pt-10'> + <LogoSpinner width={36} height={36} /> + </div> + )} + {product && <Product product={product} isVariant={true} />} + </BasicLayout> + ) +} |
