forked from neptune-mutual-blue/nft-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketplaceSection.jsx
169 lines (141 loc) · 4.83 KB
/
MarketplaceSection.jsx
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
import {
useCallback,
useContext,
useEffect,
useMemo,
useRef,
useState
} from 'react'
import {
Router,
useRouter
} from 'next/router'
import { NftCard } from '@/components/NftCard/NftCard'
import { LoaderContext } from '@/contexts/LoaderContext'
import { Icon } from '@/elements/Icon'
import { useDebounce } from '@/hooks/useDebounce'
import { useMobileFilter } from '@/hooks/useMobileFilter'
import { imageOrigin } from '@/services/marketplace-api'
import { getMarketplaceUrl } from '@/utils/nft'
import { NftPlaceholder } from '@/views/marketplace/NftPlaceholder'
import { Filter } from './Filter'
import { Pagination } from './Pagination'
const MarketPlaceSection = ({ data = [], filters = [], pageData }) => {
const [searchValue, setSearchValue] = useState(pageData?.search || '')
const [properties, setProperties] = useState(pageData?.filters ?? [])
const initial = useRef(true)
const inputRef = useRef(null)
const router = useRouter()
const debouncedSearchValue = useDebounce(searchValue, 500)
const { showFilter, onFilterOpen, onFilterClose } = useMobileFilter()
const updateUrlPath = useCallback((_page, _search, _filters) => {
if (initial.current) return
const urlObject = getMarketplaceUrl(_page, _search, _filters)
router.push(urlObject, undefined, { scroll: false })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
updateUrlPath(1, debouncedSearchValue, properties)
}, [debouncedSearchValue, properties, updateUrlPath])
useEffect(() => {
/* initial delay of 500ms */
setTimeout(() => {
initial.current = false
}, 500)
}, [])
const [curentPage, totalPages] = useMemo(() => {
if (!data.length) return [1, 1]
return [
data[0].pageNumber,
data[0].totalPages
]
}, [data])
const handleInputChange = (e) => {
setSearchValue(e.target.value)
}
const { loading } = useContext(LoaderContext)
const [isNavigating, setIsNavigating] = useState(false)
useEffect(() => {
if (loading && !initial.current) {
inputRef.current.scrollIntoView({ behavior: 'smooth' })
}
}, [loading])
useEffect(() => {
const handleRouteChangeStart = (url) => {
const urlParts = url.split('/')
const lastPart = urlParts[urlParts.length - 1]
if (url === '/' || (urlParts.length === 3 && (!isNaN(lastPart) || lastPart === 'minting-levels'))) {
setIsNavigating(true)
} else {
setIsNavigating(false)
}
}
Router.events.on('routeChangeStart', handleRouteChangeStart)
return () => {
Router.events.off('routeChangeStart', handleRouteChangeStart)
}
}, [])
return (
<div className='marketplace search container'>
<div className='inner container'>
<div className='filter overlay' data-open={showFilter ? 'true' : 'false'} />
<Filter
setProperties={setProperties}
properties={properties}
filters={filters}
showFilter={showFilter}
onFilterClose={onFilterClose}
/>
<div className='right'>
<div>
<div className='filter marketplace'>
<input
placeholder='Search Marketplace'
className='search input'
value={searchValue}
onChange={handleInputChange}
ref={inputRef}
/>
<button className='button' onClick={() => onFilterOpen()}>
<span>Properties</span>
<i data-icon='filter-lines'>
<Icon variant='filter-lines' />
</i>
</button>
</div>
<div className='nft grid'>
{loading && !isNavigating &&
Array.from({ length: 9 }).map((_x, i) => (
<NftPlaceholder key={i} />
))}
{(!loading || isNavigating) &&
data.map((nft) => (
<NftCard
key={nft.tokenId}
name={nft.nickname}
nftId={nft.tokenId}
views={nft.views}
count={nft.siblings}
image={`${imageOrigin}/thumbnails/${nft.tokenId}.webp`}
/>
))}
</div>
{!loading && !isNavigating && data.length === 0 &&
<div className='noresult found'>
<div className='content'>
<h2>No Results Found</h2>
<p>Adjust your filters and try again.</p>
</div>
</div>}
</div>
<Pagination
currentPage={curentPage}
totalPages={totalPages}
getHref={page => page ? getMarketplaceUrl(page, searchValue, properties) : '#'}
/>
</div>
</div>
</div>
)
}
export { MarketPlaceSection }