1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
import { useEffect, useMemo, useState } from 'react'
import useProductSearch from '../hooks/useProductSearch'
import ProductCard from './ProductCard'
import Pagination from '@/core/components/elements/Pagination/Pagination'
import { toQuery } from 'lodash-contrib'
import _ from 'lodash'
import ProductSearchSkeleton from './Skeleton/ProductSearchSkeleton'
import ProductFilter from './ProductFilter'
import useActive from '@/core/hooks/useActive'
import MobileView from '@/core/components/views/MobileView'
import DesktopView from '@/core/components/views/DesktopView'
import NextImage from 'next/image'
import ProductFilterDesktop from './ProductFilterDesktop'
import { useRouter } from 'next/router'
import searchSpellApi from '@/core/api/searchSpellApi'
import Link from '@/core/components/elements/Link/Link'
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: { ...query, q } })
const [products, setProducts] = useState(null)
const [spellings, setSpellings] = useState(null)
const popup = useActive()
const pageCount = Math.ceil(
productSearch.data?.response.numFound / productSearch.data?.responseHeader.params.rows
)
const productStart = productSearch.data?.responseHeader.params.start
const productRows = productSearch.data?.responseHeader.params.rows
const productFound = productSearch.data?.response.numFound
useEffect(() => {
if (productFound == 0 && query.q && !spellings) {
searchSpellApi({ query: query.q }).then((response) => {
const oddIndexSuggestions = response.data.spellcheck.suggestions.filter(
(_, index) => index % 2 === 1
)
const oddIndexCollations = response.data.spellcheck.collations.filter(
(_, index) => index % 2 === 1
)
const dataSpellings = oddIndexSuggestions.reduce((acc, curr) => {
oddIndexCollations.forEach((collation) => {
acc.push(collation.collationQuery)
})
curr.suggestion.forEach((s) => {
if (!acc.includes(s.word)) acc.push(s.word)
})
return acc
}, [])
if (dataSpellings.length > 0) {
setQ(dataSpellings[0])
}
setSpellings(dataSpellings)
})
}
}, [productFound, query, spellings])
const brands = productSearch.data?.facetCounts?.facetFields?.manufactureName?.filter(
(value, index) => {
if (index % 2 === 0) {
return true
}
}
)
const categories = productSearch.data?.facetCounts?.facetFields?.categoryName?.filter(
(value, index) => {
if (index % 2 === 0) {
return true
}
}
)
const orderOptions = [
{ value: 'price-asc', label: 'Harga Terendah' },
{ value: 'price-desc', label: 'Harga Tertinggi' },
{ value: 'popular', label: 'Populer' },
{ value: 'stock', label: 'Ready Stock' }
]
const handleOrderBy = (e) => {
let params = {
...router.query,
orderBy: e.target.value
}
params = _.pickBy(params, _.identity)
params = toQuery(params)
router.push(`${prefixUrl}?${params}`)
}
useEffect(() => {
setProducts(productSearch.data?.response?.products)
}, [productSearch])
const SpellingComponent = useMemo(() => {
return (
<>
Mungkin yang anda cari{' '}
{spellings?.map((spelling, i) => (
<Link href={`/shop/search?q=${spelling}`} key={i} className='inline'>
{spelling}
{i + 1 < spellings.length ? ', ' : ''}
</Link>
))}
</>
)
}, [spellings])
return (
<>
<MobileView>
{productSearch.isLoading && <ProductSearchSkeleton />}
<div className='p-4'>
<h1 className='mb-2 font-semibold text-h-sm'>Produk</h1>
<div className='mb-2 leading-6 text-gray_r-11'>
{!spellings ? (
<>
Menampilkan
{pageCount > 1 ? (
<>
{productStart + 1}-
{productStart + productRows > productFound
? productFound
: productStart + productRows}
dari
</>
) : (
''
)}
{productFound}
produk{' '}
{query.q && (
<>
untuk pencarian <span className='font-semibold'>{query.q}</span>
</>
)}
</>
) : (
SpellingComponent
)}
</div>
{productFound > 0 && (
<button className='btn-light mb-6 py-2 px-5' onClick={popup.activate}>
Filter
</button>
)}
<div className='grid grid-cols-2 gap-3'>
{products &&
products.map((product) => <ProductCard product={product} key={product.id} />)}
</div>
<Pagination
pageCount={pageCount}
currentPage={parseInt(page)}
url={`${prefixUrl}?${toQuery(_.omit(query, ['page']))}`}
className='mt-6 mb-2'
/>
<ProductFilter
active={popup.active}
close={popup.deactivate}
brands={brands || []}
categories={categories || []}
prefixUrl={prefixUrl}
defaultBrand={defaultBrand}
/>
</div>
</MobileView>
<DesktopView>
<div className='container mx-auto mt-10 flex mb-3'>
<div className='w-3/12'>
<ProductFilterDesktop
brands={brands || []}
categories={categories || []}
prefixUrl={prefixUrl}
defaultBrand={defaultBrand}
/>
</div>
<div className='w-9/12 pl-6'>
<h1 className='text-2xl mb-2 font-semibold'>Hasil Pencarian</h1>
<div className='flex justify-between items-center mb-2'>
<div className='mb-2 leading-6 text-gray_r-11'>
{!spellings ? (
<>
Menampilkan
{pageCount > 1 ? (
<>
{productStart + 1}-
{productStart + productRows > productFound
? productFound
: productStart + productRows}
dari
</>
) : (
''
)}
{productFound}
produk{' '}
{query.q && (
<>
untuk pencarian <span className='font-semibold'>{query.q}</span>
</>
)}
</>
) : (
SpellingComponent
)}
</div>
<div className='justify-end flex '>
<div className='ml-3'>
<select
name='urutan'
className='form-input mt-2'
value={router.query?.orderBy || ''}
onChange={(e) => handleOrderBy(e)}
>
<option value=''>Urutkan</option>
{orderOptions.map((option, index) => (
<option key={index} value={option.value}>
{' '}
{option.label}{' '}
</option>
))}
</select>
</div>
</div>
</div>
{productSearch.isLoading && <ProductSearchSkeleton />}
<div className='grid grid-cols-5 gap-x-3 gap-y-6'>
{products &&
products.map((product) => <ProductCard product={product} key={product.id} />)}
</div>
<div className='flex justify-between items-center mt-6 mb-2'>
<div className='pt-2 pb-6 flex items-center gap-x-3'>
<NextImage
src='/images/logo-question.png'
alt='Logo Question Indoteknik'
width={60}
height={60}
/>
<div className='text-gray_r-12/90'>
<span>
Barang yang anda cari tidak ada?{' '}
<a
href={
query?.q
? whatsappUrl('productSearch', {
name: query.q
})
: whatsappUrl()
}
className='text-danger-500'
>
Hubungi Kami
</a>
</span>
</div>
</div>
<Pagination
pageCount={pageCount}
currentPage={parseInt(page)}
url={`${prefixUrl}?${toQuery(_.omit(query, ['page']))}`}
className='!justify-end'
/>
</div>
</div>
</div>
</DesktopView>
</>
)
}
export default ProductSearch
|