From 6d57dbfd4e27eacfd1604b8d8326ee75326c0dd5 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 17 May 2023 15:49:50 +0700 Subject: fix search product not found --- src/lib/checkout/components/Checkout.jsx | 12 +++++++----- src/lib/product/components/ProductSearch.jsx | 29 ++++++++++++++++------------ src/lib/product/hooks/useProductSearch.js | 4 ++-- src/lib/quotation/components/Quotation.jsx | 4 ++-- 4 files changed, 28 insertions(+), 21 deletions(-) (limited to 'src/lib') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 40f5d8e5..088b641b 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -340,7 +340,7 @@ const Checkout = () => { {/*

*) Belum termasuk biaya pengiriman

*/}

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} - + Syarat & Ketentuan {' '} yang berlaku @@ -564,7 +564,7 @@ const Checkout = () => {

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} - + Syarat & Ketentuan {' '} yang berlaku @@ -755,9 +755,11 @@ function formatDate(date) { function calculateEstimatedArrival(duration) { if (duration) { - const estimationDate = duration.split('-') - const from = addDays(new Date(), parseInt(estimationDate[0]) + 3) - const to = addDays(new Date(), parseInt(estimationDate[1]) + 3) + let estimationDate = duration.split('-') + estimationDate[0] = parseInt(estimationDate[0]) + estimationDate[1] = parseInt(estimationDate[1]) + const from = addDays(new Date(), estimationDate[0] + 3) + const to = addDays(new Date(), estimationDate[1] + 3) let etdText = `*Estimasi tiba ${formatDate(from)}` diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index 30722c82..ae9618d2 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -19,8 +19,9 @@ import whatsappUrl from '@/core/utils/whatsappUrl' const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const router = useRouter() const { page = 1 } = query + const [q, setQ] = useState(query?.q || '*') if (defaultBrand) query.brand = defaultBrand.toLowerCase() - const { productSearch } = useProductSearch({ query }) + const { productSearch } = useProductSearch({ query: { ...query, q } }) const [products, setProducts] = useState(null) const [spellings, setSpellings] = useState(null) const popup = useActive() @@ -33,7 +34,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const productFound = productSearch.data?.response.numFound useEffect(() => { - if (productFound == 0 && query.q) { + if (productFound == 0 && query.q && !spellings) { searchSpellApi({ query: query.q }).then((response) => { const oddIndexSuggestions = response.data.spellcheck.suggestions.filter( (_, index) => index % 2 === 1 @@ -51,10 +52,12 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { return acc }, []) + setQ(dataSpellings[0]) + setSpellings(dataSpellings) }) } - }, [productFound, query]) + }, [productFound, query, spellings]) const brands = productSearch.data?.facetCounts?.facetFields?.manufactureName?.filter( (value, index) => { @@ -89,10 +92,8 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { } useEffect(() => { - if (!products) { - setProducts(productSearch.data?.response?.products) - } - }, [query, products, productSearch]) + setProducts(productSearch.data?.response?.products) + }, [productSearch]) const SpellingComponent = useMemo(() => { return ( @@ -116,7 +117,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => {

Produk

- {productFound > 0 ? ( + {!spellings ? ( <> Menampilkan  {pageCount > 1 ? ( @@ -186,7 +187,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => {

Hasil Pencarian

- {productFound > 0 ? ( + {!spellings ? ( <> Menampilkan  {pageCount > 1 ? ( @@ -248,9 +249,13 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { Barang yang anda cari tidak ada?{' '} Hubungi Kami diff --git a/src/lib/product/hooks/useProductSearch.js b/src/lib/product/hooks/useProductSearch.js index 0396caec..4c9272f1 100644 --- a/src/lib/product/hooks/useProductSearch.js +++ b/src/lib/product/hooks/useProductSearch.js @@ -5,10 +5,10 @@ import _ from 'lodash-contrib' const useProductSearch = ({ query }) => { const queryString = _.toQuery(query) const fetchProductSearch = async () => await productSearchApi({ query: queryString }) - const { data, isLoading } = useQuery(`productSearch-${queryString}`, fetchProductSearch) + const productSearch = useQuery(`productSearch-${queryString}`, fetchProductSearch) return { - productSearch: { data, isLoading } + productSearch } } diff --git a/src/lib/quotation/components/Quotation.jsx b/src/lib/quotation/components/Quotation.jsx index 6ff40327..9ffc2fbb 100644 --- a/src/lib/quotation/components/Quotation.jsx +++ b/src/lib/quotation/components/Quotation.jsx @@ -143,7 +143,7 @@ const Quotation = () => {

*) Belum termasuk biaya pengiriman

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} - + Syarat & Ketentuan {' '} yang berlaku @@ -270,7 +270,7 @@ const Quotation = () => {

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} - + Syarat & Ketentuan {' '} yang berlaku -- cgit v1.2.3