1
1
import { PlusIcon } from "@heroicons/react/24/outline" ;
2
- import { Dispatch , SetStateAction , useContext , useEffect , useRef , useState } from "react" ;
2
+ import { Dispatch , SetStateAction , useContext , useRef , useState } from "react" ;
3
3
import { DirectExchangeRequest , MarketplaceRequest } from "../../../../@types" ;
4
4
import ScheduleContext from "../../../../contexts/ScheduleContext" ;
5
5
import useMarketplaceRequests from "../../../../hooks/useMarketplaceRequests" ;
@@ -13,6 +13,7 @@ import { ReceivedRequestCard } from "./cards/ReceivedRequestCard";
13
13
import { RequestCard } from "./cards/RequestCard" ;
14
14
import { ViewRequestsFilters } from "./ViewRequestsFilters" ;
15
15
import { ExchangeSidebarStatus } from "../../../../pages/Exchange" ;
16
+ import { MoonLoader } from "react-spinners" ;
16
17
17
18
type Props = {
18
19
setExchangeSidebarStatus : Dispatch < SetStateAction < ExchangeSidebarStatus > >
@@ -53,6 +54,34 @@ const RequestCardSkeletons = () => {
53
54
</ >
54
55
}
55
56
57
+ const ViewMoreButton = ( {
58
+ hasNext,
59
+ setSize,
60
+ size,
61
+ isValidating
62
+ } : {
63
+ hasNext : boolean
64
+ setSize : Dispatch < SetStateAction < number > >
65
+ size : number
66
+ isValidating : boolean
67
+ } ) => {
68
+ return < >
69
+ { hasNext &&
70
+ < Button
71
+ variant = "secondary"
72
+ onClick = { ( ) => {
73
+ setSize ( size + 1 )
74
+ } }
75
+ >
76
+ { isValidating
77
+ ? < MoonLoader size = { 20 } />
78
+ : "Ver mais"
79
+ }
80
+ </ Button >
81
+ }
82
+ </ >
83
+ }
84
+
56
85
export const ViewRequests = ( {
57
86
setExchangeSidebarStatus
58
87
} : Props ) => {
@@ -66,31 +95,10 @@ export const ViewRequests = ({
66
95
// This is to keep track of the request of the request card that is currently open
67
96
const [ chosenRequest , setChosenRequest ] = useState < MarketplaceRequest | null > ( null ) ;
68
97
69
- const { data , size, setSize, isLoading } = useMarketplaceRequests (
98
+ const { requests , size, setSize, isLoading, hasNext , isValidating } = useMarketplaceRequests (
70
99
filterCourseUnitNames , requestTypeFilters [ currentRequestTypeFilter ] , classesFilter
71
100
) ;
72
101
73
- const requests = data ? [ ] . concat ( ...data ) : [ ] ;
74
-
75
- const onScroll = ( ) => {
76
- if ( ! requestCardsContainerRef . current ) return ;
77
-
78
- if ( ( requestCardsContainerRef . current . scrollHeight - requestCardsContainerRef . current . scrollTop )
79
- <= requestCardsContainerRef . current . clientHeight + 100
80
- ) {
81
- setSize ( size + 1 ) ;
82
- }
83
- }
84
-
85
- useEffect ( ( ) => {
86
- if ( ! requestCardsContainerRef . current ) return ;
87
-
88
- requestCardsContainerRef . current . addEventListener ( 'scroll' , onScroll ) ;
89
- return ( ) => {
90
- if ( requestCardsContainerRef . current ) requestCardsContainerRef . current . removeEventListener ( 'scroll' , onScroll ) ;
91
- }
92
- } , [ ] ) ;
93
-
94
102
return < div className = "relative flex flex-row flex-wrap items-center justify-center gap-x-2 gap-y-2 lg:justify-start" >
95
103
< div className = "flex flex-row justify-between items-center w-full" >
96
104
< h1 className = "font-bold text-xl" > Pedidos</ h1 >
@@ -138,6 +146,12 @@ export const ViewRequests = ({
138
146
< RequestCard />
139
147
</ CommonRequestCard >
140
148
) ) }
149
+ < ViewMoreButton
150
+ hasNext = { hasNext }
151
+ setSize = { setSize }
152
+ size = { size }
153
+ isValidating = { isValidating }
154
+ />
141
155
</ EmptyRequestGuard >
142
156
}
143
157
</ >
@@ -170,6 +184,12 @@ export const ViewRequests = ({
170
184
</ CommonRequestCard >
171
185
172
186
) ) }
187
+ < ViewMoreButton
188
+ hasNext = { hasNext }
189
+ setSize = { setSize }
190
+ size = { size }
191
+ isValidating = { isValidating }
192
+ />
173
193
</ EmptyRequestGuard >
174
194
}
175
195
</ div >
@@ -198,6 +218,12 @@ export const ViewRequests = ({
198
218
/>
199
219
</ CommonRequestCard >
200
220
) ) }
221
+ < ViewMoreButton
222
+ hasNext = { hasNext }
223
+ setSize = { setSize }
224
+ size = { size }
225
+ isValidating = { isValidating }
226
+ />
201
227
</ EmptyRequestGuard >
202
228
}
203
229
</ div >
0 commit comments