@@ -17,7 +17,10 @@ import {
17
17
ProposalVote ,
18
18
ProposalVoteVoter ,
19
19
} from "./ProposalDetailScreenModel" ;
20
- import { useProposalVotesQuery } from "./ProposalDetailScreenAPI" ;
20
+ import {
21
+ ProposalVoteSortableColumn ,
22
+ useProposalVotesQuery ,
23
+ } from "./ProposalDetailScreenAPI" ;
21
24
22
25
const PROPOSAL_VOTE_PAGE_SIZE = 5 ;
23
26
@@ -149,10 +152,22 @@ const ProposalVotesPanel: React.FC = () => {
149
152
const { translate } = useLocale ( ) ;
150
153
const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
151
154
152
- const currentOffset = useMemo ( ( ) => {
155
+ const [ currentOffset , sortOrder ] = useMemo ( ( ) => {
153
156
const page = searchParams . get ( "page" ) ?? "1" ;
157
+ const sortBy = searchParams . get ( "sortBy" ) ;
158
+ const sortDirection = searchParams . get ( "sortDirection" ) ;
159
+
160
+ const sortOrder =
161
+ sortBy && sortDirection
162
+ ? {
163
+ id : sortBy as ProposalVoteSortableColumn ,
164
+ direction : sortDirection as SectionedTable . ColumnOrder [ "direction" ] ,
165
+ }
166
+ : undefined ;
167
+
168
+ const offset = ( parseInt ( page , 10 ) - 1 ) * PROPOSAL_VOTE_PAGE_SIZE ;
154
169
155
- return ( parseInt ( page , 10 ) - 1 ) * PROPOSAL_VOTE_PAGE_SIZE ;
170
+ return [ offset , sortOrder ] ;
156
171
} , [ searchParams ] ) ;
157
172
158
173
const { fetch, requestState } = useProposalVotesQuery (
@@ -170,6 +185,16 @@ const ProposalVotesPanel: React.FC = () => {
170
185
[ setSearchParams ]
171
186
) ;
172
187
188
+ const setSortOrder = useCallback (
189
+ ( order : SectionedTable . ColumnOrder ) => {
190
+ setSearchParams ( {
191
+ sortBy : order . id ,
192
+ sortDirection : order . direction ,
193
+ } ) ;
194
+ } ,
195
+ [ setSearchParams ]
196
+ ) ;
197
+
173
198
const tableSections =
174
199
useMemo ( ( ) : SectionedTable . SectionItem < ProposalVote > [ ] => {
175
200
if ( ! isRequestStateLoaded ( requestState ) ) {
@@ -202,9 +227,14 @@ const ProposalVotesPanel: React.FC = () => {
202
227
fetch ( {
203
228
first : PROPOSAL_VOTE_PAGE_SIZE ,
204
229
after : currentOffset ,
205
- order : { } ,
230
+ order : sortOrder
231
+ ? {
232
+ id : sortOrder . id ,
233
+ direction : sortOrder . direction ,
234
+ }
235
+ : null ,
206
236
} ) ;
207
- } , [ fetch , currentOffset ] ) ;
237
+ } , [ fetch , currentOffset , sortOrder ] ) ;
208
238
209
239
useEffect ( ( ) => {
210
240
if ( isRequestStateError ( requestState ) ) {
@@ -219,16 +249,18 @@ const ProposalVotesPanel: React.FC = () => {
219
249
sections = { tableSections }
220
250
isLoading = { ! isRequestStateLoaded ( requestState ) }
221
251
emptyMessageID = "ProposalDetail.votes.empty"
252
+ sortOrder = { sortOrder }
253
+ onSort = { setSortOrder }
222
254
>
223
255
< SectionedTable . Column < ProposalVote >
224
- id = "voter"
256
+ id = { ProposalVoteSortableColumn . Voter }
225
257
titleId = "ProposalDetail.votes.voter"
226
258
sortable = { true }
227
259
>
228
260
{ ( item ) => < ProposalVoter voter = { item . voter } /> }
229
261
</ SectionedTable . Column >
230
262
< SectionedTable . Column < ProposalVote >
231
- id = "option"
263
+ id = { ProposalVoteSortableColumn . Option }
232
264
titleId = "ProposalDetail.votes.option"
233
265
sortable = { true }
234
266
>
0 commit comments