Skip to content

Commit 9313703

Browse files
authored
Merge pull request #123 from chrispoupart/feat/update-quest-resets
refactor(quest-board): remove unused pagination state update
2 parents 000143a + a65939b commit 9313703

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

frontend/src/components/quest-board.tsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,10 @@ const QuestBoard: React.FC = () => {
837837

838838
// Update pagination state
839839
if (questData.pagination) {
840-
setCurrentPage(questData.pagination.page)
840+
// This is important to sync with server-side state, e.g., if requested page is out of bounds
841+
if (questData.pagination.page !== currentPage) {
842+
setCurrentPage(questData.pagination.page)
843+
}
841844
setTotalPages(questData.pagination.totalPages)
842845
setTotalQuests(questData.pagination.total)
843846
}
@@ -849,33 +852,49 @@ const QuestBoard: React.FC = () => {
849852
}
850853
}
851854

852-
// Fetch quests on component mount and tab change
855+
// Fetch quests when currentPage or activeTab changes.
856+
// The logic inside handles resetting page for tab changes.
853857
useEffect(() => {
854-
setCurrentPage(1) // Reset to first page when tab changes
855-
fetchQuests(1)
856-
}, [activeTab])
858+
fetchQuests(currentPage)
859+
}, [currentPage])
857860

858-
// Debounced search effect
861+
// Reset to page 1 when active tab changes
859862
useEffect(() => {
860-
const timeoutId = setTimeout(() => {
861-
setCurrentPage(1) // Reset to first page when searching
863+
if (currentPage !== 1) {
864+
setCurrentPage(1)
865+
} else {
866+
// If already on page 1, the currentPage effect won't run, so we need to fetch manually
862867
fetchQuests(1)
868+
}
869+
}, [activeTab])
863870

871+
// Debounced search effect
872+
useEffect(() => {
873+
const handler = setTimeout(() => {
874+
if (currentPage !== 1) {
875+
setCurrentPage(1)
876+
} else {
877+
fetchQuests(1)
878+
}
864879
// Update URL with search term
865880
if (searchTerm) {
866881
setSearchParams({ search: searchTerm });
867882
} else {
868883
setSearchParams({});
869884
}
870-
}, 500) // 500ms delay
885+
}, 500)
871886

872-
return () => clearTimeout(timeoutId)
887+
return () => {
888+
clearTimeout(handler)
889+
}
873890
}, [searchTerm, setSearchParams])
874891

892+
875893
// Handle page changes
876894
const handlePageChange = (page: number) => {
877-
setCurrentPage(page)
878-
fetchQuests(page)
895+
if (page !== currentPage) {
896+
setCurrentPage(page)
897+
}
879898
}
880899

881900
const handleQuestAction = async (questId: number, action: string) => {

0 commit comments

Comments
 (0)