@@ -14,11 +14,14 @@ var firebaseConfig = {
1414// Initialize Firebase
1515firebase . initializeApp ( firebaseConfig ) ;
1616
17+ let currentPage = 1 ;
18+ const profilesPerPage = 12 ; // Number of profiles per page
19+
1720document . addEventListener ( "DOMContentLoaded" , function ( ) {
1821 let contributors = [ ] ;
1922 const noProfilesMessage = document . querySelector ( ".no-profiles-message" ) ;
2023 noProfilesMessage . style . display = "none"
21- function renderProfiles ( filter = "" ) {
24+ function renderProfiles ( filter = "" , page = 1 , profilesPerPage = 12 ) {
2225
2326 const container = document . querySelector ( ".profiles" ) ;
2427 container . innerHTML = "" ;
@@ -34,7 +37,15 @@ document.addEventListener("DOMContentLoaded", function () {
3437
3538 return
3639 }
37- contributors . forEach ( ( contributor , index ) => {
40+
41+ // Calculate start and end index for pagination
42+ const startIndex = ( page - 1 ) * profilesPerPage ;
43+ const endIndex = startIndex + profilesPerPage ;
44+
45+ // Slice the filtered contributors based on the current page
46+ const paginatedContributors = filteredContributors . slice ( startIndex , endIndex ) ;
47+
48+ paginatedContributors . forEach ( ( contributor , index ) => {
3849 noProfilesMessage . style . display = "none" ;
3950
4051 if ( contributor . login . toLowerCase ( ) . includes ( filter . toLowerCase ( ) ) ) {
@@ -175,14 +186,43 @@ document.addEventListener("DOMContentLoaded", function () {
175186
176187 }
177188 } ) ;
189+
190+ // Update pagination controls
191+ updatePaginationControls ( page , profilesPerPage , filteredContributors . length ) ;
192+
193+
194+ }
195+
196+ function updatePaginationControls ( currentPage , profilesPerPage , totalProfiles ) {
197+ const totalPages = Math . ceil ( totalProfiles / profilesPerPage ) ;
198+ const pageInfo = document . getElementById ( "page-info" ) ;
199+ pageInfo . textContent = `Page ${ currentPage } of ${ totalPages } ` ;
200+
201+ // Disable previous button if on the first page
202+ document . getElementById ( "prev-page" ) . disabled = currentPage === 1 ;
203+
204+ // Disable next button if on the last page
205+ document . getElementById ( "next-page" ) . disabled = currentPage === totalPages ;
178206 }
179207
208+ document . getElementById ( "prev-page" ) . addEventListener ( "click" , function ( ) {
209+ if ( currentPage > 1 ) {
210+ currentPage -- ;
211+ renderProfiles ( "" , currentPage , profilesPerPage ) ;
212+ }
213+ } ) ;
214+
215+ document . getElementById ( "next-page" ) . addEventListener ( "click" , function ( ) {
216+ currentPage ++ ;
217+ renderProfiles ( "" , currentPage , profilesPerPage ) ;
218+ } ) ;
219+
180220 // Fetch contributors data
181221 fetch ( "https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc" )
182222 . then ( ( response ) => response . json ( ) )
183223 . then ( ( data ) => {
184224 contributors = data . contributors ;
185- renderProfiles ( ) ;
225+ renderProfiles ( "" , 1 , profilesPerPage ) ; //Start on page one
186226 } ) ;
187227
188228 // Add event listener to the search bar
0 commit comments