Skip to content

Commit a65939b

Browse files
committed
fix(quest-board): 🩹 ensure page sync on tab and search changes
1 parent 2c10d65 commit a65939b

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

‎frontend/src/components/quest-board.tsx‎

Lines changed: 29 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,35 +852,49 @@ const QuestBoard: React.FC = () => {
849852
}
850853
}
851854

855+
// Fetch quests when currentPage or activeTab changes.
856+
// The logic inside handles resetting page for tab changes.
852857
useEffect(() => {
853-
fetchQuests(currentPage);
854-
}, [currentPage, activeTab, searchTerm]);
855-
858+
fetchQuests(currentPage)
859+
}, [currentPage])
856860

857-
// Fetch quests on component mount and tab change
861+
// Reset to page 1 when active tab changes
858862
useEffect(() => {
859-
setCurrentPage(1) // Reset to first page when tab changes
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
867+
fetchQuests(1)
868+
}
860869
}, [activeTab])
861870

862871
// Debounced search effect
863872
useEffect(() => {
864-
const timeoutId = setTimeout(() => {
865-
setCurrentPage(1) // Reset to first page when searching
866-
873+
const handler = setTimeout(() => {
874+
if (currentPage !== 1) {
875+
setCurrentPage(1)
876+
} else {
877+
fetchQuests(1)
878+
}
867879
// Update URL with search term
868880
if (searchTerm) {
869881
setSearchParams({ search: searchTerm });
870882
} else {
871883
setSearchParams({});
872884
}
873-
}, 500) // 500ms delay
885+
}, 500)
874886

875-
return () => clearTimeout(timeoutId)
887+
return () => {
888+
clearTimeout(handler)
889+
}
876890
}, [searchTerm, setSearchParams])
877891

892+
878893
// Handle page changes
879894
const handlePageChange = (page: number) => {
880-
setCurrentPage(page)
895+
if (page !== currentPage) {
896+
setCurrentPage(page)
897+
}
881898
}
882899

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

0 commit comments

Comments
 (0)