1
1
import { collection , getDocs , query , where } from "firebase/firestore"
2
- import { getFunctions } from "firebase/functions"
3
2
import { useTranslation } from "next-i18next"
4
3
import { useCallback , useEffect , useMemo , useState } from "react"
5
4
import { useAuth } from "../auth"
@@ -10,6 +9,7 @@ import UnfollowItem, { UnfollowModalConfig } from "./UnfollowModal"
10
9
import { FollowedItem } from "./FollowingTabComponents"
11
10
import { BillElement , UserElement } from "./FollowingTabComponents"
12
11
import { deleteItem } from "components/shared/FollowingQueries"
12
+ import { PaginationButtons } from "../table"
13
13
14
14
export function FollowingTab ( { className } : { className ?: string } ) {
15
15
const { user } = useAuth ( )
@@ -29,6 +29,10 @@ export function FollowingTab({ className }: { className?: string }) {
29
29
const [ billsFollowing , setBillsFollowing ] = useState < BillElement [ ] > ( [ ] )
30
30
const [ usersFollowing , setUsersFollowing ] = useState < UserElement [ ] > ( [ ] )
31
31
32
+ const [ currentBillsPage , setCurrentBillsPage ] = useState ( 1 )
33
+ const [ currentUsersPage , setCurrentUsersPage ] = useState ( 1 )
34
+ const itemsPerPage = 10
35
+
32
36
const billsFollowingQuery = useCallback ( async ( ) => {
33
37
if ( ! subscriptionRef ) return // handle the case where subscriptionRef is null
34
38
const billList : BillElement [ ] = [ ]
@@ -42,14 +46,12 @@ export function FollowingTab({ className }: { className?: string }) {
42
46
// doc.data() is never undefined for query doc snapshots
43
47
billList . push ( doc . data ( ) . billLookup )
44
48
} )
45
- if ( billsFollowing . length === 0 && billList . length != 0 ) {
46
- setBillsFollowing ( billList )
47
- }
48
- } , [ subscriptionRef , uid , billsFollowing ] )
49
+ setBillsFollowing ( billList )
50
+ } , [ subscriptionRef , uid ] )
49
51
50
52
useEffect ( ( ) => {
51
53
uid ? billsFollowingQuery ( ) : null
52
- } )
54
+ } , [ uid , billsFollowingQuery ] )
53
55
54
56
const orgsFollowingQuery = useCallback ( async ( ) => {
55
57
if ( ! subscriptionRef ) return // handle the case where subscriptionRef is null
@@ -104,6 +106,21 @@ export function FollowingTab({ className }: { className?: string }) {
104
106
setUnfollow ( null )
105
107
}
106
108
109
+ const getPaginatedBills = ( ) => {
110
+ const startIndex = ( currentBillsPage - 1 ) * itemsPerPage
111
+ const endIndex = startIndex + itemsPerPage
112
+ return billsFollowing . slice ( startIndex , endIndex )
113
+ }
114
+
115
+ const getPaginatedUsers = ( ) => {
116
+ const startIndex = ( currentUsersPage - 1 ) * itemsPerPage
117
+ const endIndex = startIndex + itemsPerPage
118
+ return usersFollowing . slice ( startIndex , endIndex )
119
+ }
120
+
121
+ const totalBillsPages = Math . ceil ( billsFollowing . length / itemsPerPage )
122
+ const totalUsersPages = Math . ceil ( usersFollowing . length / itemsPerPage )
123
+
107
124
const { t } = useTranslation ( "editProfile" )
108
125
109
126
return (
@@ -112,7 +129,7 @@ export function FollowingTab({ className }: { className?: string }) {
112
129
< div className = { `mx-4 mt-3 d-flex flex-column gap-3` } >
113
130
< Stack >
114
131
< h2 > { t ( "follow.bills" ) } </ h2 >
115
- { billsFollowing . map ( ( element : BillElement , index : number ) => (
132
+ { getPaginatedBills ( ) . map ( ( element : BillElement , index : number ) => (
116
133
< FollowedItem
117
134
key = { index }
118
135
index = { index }
@@ -121,14 +138,26 @@ export function FollowingTab({ className }: { className?: string }) {
121
138
type = { "bill" }
122
139
/>
123
140
) ) }
141
+ { billsFollowing . length > 0 && (
142
+ < PaginationButtons
143
+ pagination = { {
144
+ currentPage : currentBillsPage ,
145
+ hasNextPage : currentBillsPage < totalBillsPages ,
146
+ hasPreviousPage : currentBillsPage > 1 ,
147
+ nextPage : ( ) => setCurrentBillsPage ( prev => prev + 1 ) ,
148
+ previousPage : ( ) => setCurrentBillsPage ( prev => prev - 1 ) ,
149
+ itemsPerPage
150
+ } }
151
+ />
152
+ ) }
124
153
</ Stack >
125
154
</ div >
126
155
</ TitledSectionCard >
127
156
< TitledSectionCard className = { `${ className } ` } >
128
157
< div className = { `mx-4 mt-3 d-flex flex-column gap-3` } >
129
158
< Stack >
130
159
< h2 className = "pb-3" > { t ( "follow.orgs" ) } </ h2 >
131
- { usersFollowing . map ( ( element : UserElement , index : number ) => (
160
+ { getPaginatedUsers ( ) . map ( ( element : UserElement , index : number ) => (
132
161
< FollowedItem
133
162
key = { index }
134
163
index = { index }
@@ -137,6 +166,18 @@ export function FollowingTab({ className }: { className?: string }) {
137
166
type = { "org" }
138
167
/>
139
168
) ) }
169
+ { usersFollowing . length > 0 && (
170
+ < PaginationButtons
171
+ pagination = { {
172
+ currentPage : currentUsersPage ,
173
+ hasNextPage : currentUsersPage < totalUsersPages ,
174
+ hasPreviousPage : currentUsersPage > 1 ,
175
+ nextPage : ( ) => setCurrentUsersPage ( prev => prev + 1 ) ,
176
+ previousPage : ( ) => setCurrentUsersPage ( prev => prev - 1 ) ,
177
+ itemsPerPage
178
+ } }
179
+ />
180
+ ) }
140
181
</ Stack >
141
182
</ div >
142
183
</ TitledSectionCard >
0 commit comments