KW_DB Udpates
This commit is contained in:
@@ -148,10 +148,11 @@ export default function IndustriesSectorsKeywords() {
|
||||
console.warn('Could not fetch sectors or attached keywords:', err);
|
||||
}
|
||||
|
||||
// Build filters - fetch ALL results by paginating through all pages
|
||||
// Build filters - fetch up to 500 records max for performance
|
||||
const MAX_RECORDS = 500;
|
||||
const baseFilters: any = {
|
||||
industry: activeSite.industry,
|
||||
page_size: 1000,
|
||||
page_size: 100, // Fetch in batches of 100
|
||||
};
|
||||
|
||||
// Add sector filter if active sector is selected
|
||||
@@ -162,24 +163,35 @@ export default function IndustriesSectorsKeywords() {
|
||||
if (searchTerm) baseFilters.search = searchTerm;
|
||||
if (countryFilter) baseFilters.country = countryFilter;
|
||||
|
||||
// Fetch ALL pages to get complete dataset
|
||||
// Fetch up to MAX_RECORDS (500) for initial display
|
||||
let allResults: SeedKeyword[] = [];
|
||||
let currentPageNum = 1;
|
||||
let hasMore = true;
|
||||
let apiTotalCount = 0; // Store the total count from API
|
||||
|
||||
while (hasMore) {
|
||||
while (hasMore && allResults.length < MAX_RECORDS) {
|
||||
const filters = { ...baseFilters, page: currentPageNum };
|
||||
const data: SeedKeywordResponse = await fetchSeedKeywords(filters);
|
||||
|
||||
if (data.results && data.results.length > 0) {
|
||||
allResults = [...allResults, ...data.results];
|
||||
// Store total count from first response
|
||||
if (currentPageNum === 1 && data.count !== undefined) {
|
||||
apiTotalCount = data.count;
|
||||
}
|
||||
|
||||
hasMore = data.next !== null && data.next !== undefined;
|
||||
if (data.results && data.results.length > 0) {
|
||||
// Only add records up to MAX_RECORDS limit
|
||||
const remainingSpace = MAX_RECORDS - allResults.length;
|
||||
const recordsToAdd = data.results.slice(0, remainingSpace);
|
||||
allResults = [...allResults, ...recordsToAdd];
|
||||
}
|
||||
|
||||
// Stop if we've reached the limit or no more pages
|
||||
hasMore = data.next !== null && data.next !== undefined && allResults.length < MAX_RECORDS;
|
||||
currentPageNum++;
|
||||
|
||||
if (currentPageNum > 100) {
|
||||
console.warn('Reached maximum page limit (100) while fetching seed keywords');
|
||||
// Safety check to prevent infinite loops
|
||||
if (currentPageNum > 10) {
|
||||
console.warn('Reached safety limit while fetching seed keywords');
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -193,11 +205,15 @@ export default function IndustriesSectorsKeywords() {
|
||||
};
|
||||
});
|
||||
|
||||
// Calculate counts before applying filters
|
||||
// Calculate counts before applying filters (for display in header)
|
||||
const totalAdded = filteredResults.filter(sk => sk.isAdded).length;
|
||||
const totalAvailable = filteredResults.filter(sk => !sk.isAdded).length;
|
||||
const loadedAvailable = filteredResults.filter(sk => !sk.isAdded).length;
|
||||
|
||||
// Use API total count for available if we have it, otherwise use loaded count
|
||||
const actualAvailable = apiTotalCount > 0 ? apiTotalCount - totalAdded : loadedAvailable;
|
||||
|
||||
setAddedCount(totalAdded);
|
||||
setAvailableCount(totalAvailable);
|
||||
setAvailableCount(actualAvailable);
|
||||
|
||||
// Apply "not yet added" filter
|
||||
if (showNotAddedOnly) {
|
||||
@@ -256,8 +272,14 @@ export default function IndustriesSectorsKeywords() {
|
||||
const paginatedResults = filteredResults.slice(startIndex, endIndex);
|
||||
|
||||
setSeedKeywords(paginatedResults);
|
||||
setTotalCount(totalFiltered);
|
||||
setTotalPages(Math.ceil(totalFiltered / pageSizeNum));
|
||||
|
||||
// Use API total count if available and no client-side filters are applied
|
||||
// Otherwise use filtered count
|
||||
const shouldUseApiCount = apiTotalCount > 0 && !showNotAddedOnly && !difficultyFilter;
|
||||
const displayTotalCount = shouldUseApiCount ? apiTotalCount : totalFiltered;
|
||||
|
||||
setTotalCount(displayTotalCount);
|
||||
setTotalPages(Math.ceil(displayTotalCount / pageSizeNum));
|
||||
|
||||
setShowContent(true);
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user