Skip to content

Commit 0f182da

Browse files
committed
Paging bug fixed
1 parent c4d8258 commit 0f182da

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

scripts/dashboard.js

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// const placeholder = '<span class="searchPlaceholder">Search...</span>'
32

43
// //The tags currently displayed
@@ -398,40 +397,57 @@ async function fetchData(key = 0) {
398397
const JWTAccessToken = sessionStorage.getItem('accessToken');
399398
let apiUrl = 'https://recipiebeckend.azurewebsites.net/recipes/home';
400399
if (key !== undefined) {
401-
apiUrl += `?key=${key}`;
400+
apiUrl += `?key=${key}`;
402401
}
403-
402+
404403
const headers = {
405-
'Content-Type': 'application/json',
406-
'Authorization': JWTAccessToken,
404+
'Content-Type': 'application/json',
405+
'Authorization': JWTAccessToken,
407406
};
408-
407+
409408
try {
410-
const response = await fetch(apiUrl, {
411-
method: 'GET',
412-
headers: JWTAccessToken ? headers : { 'Content-Type': 'application/json' },
413-
});
414-
415-
if (!response.ok) {
416-
throw new Error(`HTTP error! status: ${response.status}`);
417-
}
418-
419-
const data = await response.json();
420-
console.log(data);
421-
422-
const maxPage = await getMaxPage(); // Await the result of getMaxPage
423-
if (maxPage !== null) {
424-
// displayPage(key, maxPage);
425-
} else {
426-
console.error('Failed to get the maximum number of pages.');
427-
}
428-
429-
displayDashboard(data);
409+
const response = await fetch(apiUrl, {
410+
method: 'GET',
411+
headers: JWTAccessToken ? headers : { 'Content-Type': 'application/json' },
412+
});
413+
414+
if (!response.ok) {
415+
throw new Error(`HTTP error! status: ${response.status}`);
416+
}
417+
418+
const data = await response.json();
419+
console.log(data);
420+
421+
displayDashboard(data);
422+
displayPagination(data);
430423
} catch (error) {
431-
console.error('Error fetching or displaying data:', error);
424+
console.error('Error fetching or displaying data:', error);
432425
}
433-
}
426+
}
434427

428+
let isPaginationInitialized = false; // Flag to track pagination initialization
429+
430+
async function displayPagination(data) {
431+
if (!isPaginationInitialized) { // Check if pagination is already initialized
432+
const maxPage = await getMaxPage(data);
433+
if (maxPage !== null) {
434+
$('#pagination-demo').twbsPagination('destroy'); // Clear previous pagination
435+
$('#pagination-demo').twbsPagination({
436+
startPage: 1, // Assuming page starts from 1 (adjust if needed)
437+
totalPages: maxPage,
438+
visiblePages: 5,
439+
next: 'Next',
440+
prev: 'Prev',
441+
onPageClick: function (event, page) {
442+
fetchData(page - 1); // Call fetchData with the selected page number
443+
}
444+
});
445+
isPaginationInitialized = true; // Set flag to prevent re-initialization
446+
} else {
447+
console.error('Failed to get the maximum number of pages.');
448+
}
449+
}
450+
}
435451

436452

437453
const fetchDataByMealType = async (mealType) => {
@@ -739,30 +755,4 @@ async function getMaxPage() {
739755
console.error('Error fetching maximum page:', error);
740756
return null; // Return null in case of error
741757
}
742-
}
743-
744-
745-
function displayPage(startPage, total) {
746-
if (typeof startPage !== 'number' || startPage < 0 || startPage >= total) {
747-
console.error(`Invalid startPage value: ${startPage}`);
748-
startPage = 0; // Set a default valid startPage value
749-
}
750-
751-
if (typeof total !== 'number' || total <= 0) {
752-
console.error(`Invalid totalPages value: ${total}`);
753-
total = 1; // Set a default valid totalPages value
754-
}
755-
756-
$('#pagination-demo').twbsPagination('destroy');
757-
$('#pagination-demo').twbsPagination({
758-
startPage: startPage + 1, // twbsPagination uses 1-based index
759-
totalPages: total,
760-
visiblePages: 5,
761-
next: 'Next',
762-
prev: 'Prev',
763-
onPageClick: function (event, page) {
764-
fetchData(page - 1); // Convert 1-based page number to 0-based index
765-
console.log("Page =", page - 1);
766-
}
767-
});
768-
}
758+
}

0 commit comments

Comments
 (0)