11import { collection , getDocs , query , where } from "firebase/firestore"
2- import { getFunctions } from "firebase/functions"
32import { useTranslation } from "next-i18next"
43import { useCallback , useEffect , useMemo , useState } from "react"
54import { useAuth } from "../auth"
@@ -10,6 +9,7 @@ import UnfollowItem, { UnfollowModalConfig } from "./UnfollowModal"
109import { FollowedItem } from "./FollowingTabComponents"
1110import { BillElement , UserElement } from "./FollowingTabComponents"
1211import { deleteItem } from "components/shared/FollowingQueries"
12+ import { PaginationButtons } from "../table"
1313
1414export function FollowingTab ( { className } : { className ?: string } ) {
1515 const { user } = useAuth ( )
@@ -29,6 +29,10 @@ export function FollowingTab({ className }: { className?: string }) {
2929 const [ billsFollowing , setBillsFollowing ] = useState < BillElement [ ] > ( [ ] )
3030 const [ usersFollowing , setUsersFollowing ] = useState < UserElement [ ] > ( [ ] )
3131
32+ const [ currentBillsPage , setCurrentBillsPage ] = useState ( 1 )
33+ const [ currentUsersPage , setCurrentUsersPage ] = useState ( 1 )
34+ const itemsPerPage = 10
35+
3236 const billsFollowingQuery = useCallback ( async ( ) => {
3337 if ( ! subscriptionRef ) return // handle the case where subscriptionRef is null
3438 const billList : BillElement [ ] = [ ]
@@ -44,12 +48,12 @@ export function FollowingTab({ className }: { className?: string }) {
4448 } )
4549 if ( billsFollowing . length === 0 && billList . length != 0 ) {
4650 setBillsFollowing ( billList )
47- }
51+ } // this limits the code from falling into an infinite loop
4852 } , [ subscriptionRef , uid , billsFollowing ] )
4953
5054 useEffect ( ( ) => {
5155 uid ? billsFollowingQuery ( ) : null
52- } )
56+ } , [ uid , billsFollowingQuery ] )
5357
5458 const orgsFollowingQuery = useCallback ( async ( ) => {
5559 if ( ! subscriptionRef ) return // handle the case where subscriptionRef is null
@@ -67,7 +71,7 @@ export function FollowingTab({ className }: { className?: string }) {
6771
6872 if ( usersFollowing . length === 0 && usersList . length != 0 ) {
6973 setUsersFollowing ( usersList )
70- }
74+ } // this limits the code from falling into an infinite loop
7175 } , [ subscriptionRef , uid , usersFollowing ] )
7276
7377 const fetchFollowedItems = useCallback ( async ( ) => {
@@ -104,6 +108,21 @@ export function FollowingTab({ className }: { className?: string }) {
104108 setUnfollow ( null )
105109 }
106110
111+ const getPaginatedBills = ( ) => {
112+ const startIndex = ( currentBillsPage - 1 ) * itemsPerPage
113+ const endIndex = startIndex + itemsPerPage
114+ return billsFollowing . slice ( startIndex , endIndex )
115+ }
116+
117+ const getPaginatedUsers = ( ) => {
118+ const startIndex = ( currentUsersPage - 1 ) * itemsPerPage
119+ const endIndex = startIndex + itemsPerPage
120+ return usersFollowing . slice ( startIndex , endIndex )
121+ }
122+
123+ const totalBillsPages = Math . ceil ( billsFollowing . length / itemsPerPage )
124+ const totalUsersPages = Math . ceil ( usersFollowing . length / itemsPerPage )
125+
107126 const { t } = useTranslation ( "editProfile" )
108127
109128 return (
@@ -112,7 +131,7 @@ export function FollowingTab({ className }: { className?: string }) {
112131 < div className = { `mx-4 mt-3 d-flex flex-column gap-3` } >
113132 < Stack >
114133 < h2 > { t ( "follow.bills" ) } </ h2 >
115- { billsFollowing . map ( ( element : BillElement , index : number ) => (
134+ { getPaginatedBills ( ) . map ( ( element : BillElement , index : number ) => (
116135 < FollowedItem
117136 key = { index }
118137 index = { index }
@@ -121,14 +140,26 @@ export function FollowingTab({ className }: { className?: string }) {
121140 type = { "bill" }
122141 />
123142 ) ) }
143+ { billsFollowing . length > 0 && (
144+ < PaginationButtons
145+ pagination = { {
146+ currentPage : currentBillsPage ,
147+ hasNextPage : currentBillsPage < totalBillsPages ,
148+ hasPreviousPage : currentBillsPage > 1 ,
149+ nextPage : ( ) => setCurrentBillsPage ( prev => prev + 1 ) ,
150+ previousPage : ( ) => setCurrentBillsPage ( prev => prev - 1 ) ,
151+ itemsPerPage
152+ } }
153+ />
154+ ) }
124155 </ Stack >
125156 </ div >
126157 </ TitledSectionCard >
127158 < TitledSectionCard className = { `${ className } ` } >
128159 < div className = { `mx-4 mt-3 d-flex flex-column gap-3` } >
129160 < Stack >
130161 < h2 className = "pb-3" > { t ( "follow.orgs" ) } </ h2 >
131- { usersFollowing . map ( ( element : UserElement , index : number ) => (
162+ { getPaginatedUsers ( ) . map ( ( element : UserElement , index : number ) => (
132163 < FollowedItem
133164 key = { index }
134165 index = { index }
@@ -137,6 +168,18 @@ export function FollowingTab({ className }: { className?: string }) {
137168 type = { "org" }
138169 />
139170 ) ) }
171+ { usersFollowing . length > 0 && (
172+ < PaginationButtons
173+ pagination = { {
174+ currentPage : currentUsersPage ,
175+ hasNextPage : currentUsersPage < totalUsersPages ,
176+ hasPreviousPage : currentUsersPage > 1 ,
177+ nextPage : ( ) => setCurrentUsersPage ( prev => prev + 1 ) ,
178+ previousPage : ( ) => setCurrentUsersPage ( prev => prev - 1 ) ,
179+ itemsPerPage
180+ } }
181+ />
182+ ) }
140183 </ Stack >
141184 </ div >
142185 </ TitledSectionCard >
0 commit comments