Skip to content

Commit 775b8aa

Browse files
committed
Fix issues with current page persisting
1 parent ff4dc96 commit 775b8aa

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

site/src/component/SearchModule/SearchModule.tsx

+18-15
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
2323
const dispatch = useAppDispatch();
2424
const search = useAppSelector((state) => state.search[index]);
2525
const [pendingRequest, setPendingRequest] = useState<NodeJS.Timeout | null>(null);
26+
const [prevIndex, setPrevIndex] = useState<SearchIndex | null>(null);
2627

2728
const searchNames = useCallback(
28-
(query: string) => {
29+
(query: string, pageNumber: number, lastQuery?: string) => {
2930
// Get all results only when query changes or user reaches the fourth page or after
3031
const nameResults = wfs({
3132
query: query,
3233
resultType: index === 'courses' ? 'COURSE' : 'INSTRUCTOR',
3334
// Load INITIAL_MAX_PAGE pages first
3435
// when user reaches the 4th page or after, load all results
3536
numResults:
36-
search.lastQuery !== query || search.pageNumber < FULL_RESULT_THRESHOLD
37+
lastQuery !== query || pageNumber < FULL_RESULT_THRESHOLD
3738
? NUM_RESULTS_PER_PAGE * INITIAL_MAX_PAGE
3839
: undefined,
3940
});
@@ -52,23 +53,30 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
5253
}
5354
dispatch(setNames({ index, names }));
5455
// reset page number and hasFullResults flag if query changes
55-
if (query !== search.lastQuery) {
56+
if (query !== lastQuery) {
5657
dispatch(setPageNumber({ index, pageNumber: 0 }));
5758
dispatch(setHasFullResults({ index, hasFullResults: false }));
5859
dispatch(setLastQuery({ index, lastQuery: query }));
5960
}
6061
},
61-
[dispatch, search.pageNumber, search.lastQuery, index],
62+
[dispatch, index],
6263
);
6364

65+
// Search empty string to load some results on intial visit/when switching between courses and professors tabs
66+
// make sure this runs before everything else for best performance and avoiding bugs
67+
if (index !== prevIndex) {
68+
setPrevIndex(index);
69+
searchNames('', 0);
70+
}
71+
6472
const searchResults = useCallback(async () => {
6573
if (search.names.length === 0) {
6674
dispatch(setResults({ index, results: [] }));
6775
return;
6876
}
6977
if (!search.hasFullResults && search.pageNumber >= FULL_RESULT_THRESHOLD) {
7078
dispatch(setHasFullResults({ index, hasFullResults: true }));
71-
searchNames(search.lastQuery);
79+
searchNames(search.lastQuery, search.pageNumber, search.lastQuery);
7280
return;
7381
}
7482
// Get the subset of names based on the page
@@ -80,12 +88,7 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
8088
dispatch(setResults({ index, results: Object.values(results) }));
8189
}, [dispatch, search.names, search.pageNumber, index, search.hasFullResults, search.lastQuery, searchNames]);
8290

83-
// Refresh search results when names and page number changes (controlled by searchResults dependency array)
84-
useEffect(() => {
85-
searchResults();
86-
}, [index, searchResults]);
87-
88-
// reset page number and clear results on unmount
91+
// clear results and reset page number when component unmounts
8992
// results will persist otherwise, e.g. current page of results from catalogue carries over to roadmap search container
9093
useEffect(() => {
9194
return () => {
@@ -96,17 +99,17 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
9699
};
97100
}, [dispatch]);
98101

99-
// Search empty string to load some results on intial visit/when switching between courses and professors tabs
102+
// Refresh search results when names and page number changes (controlled by searchResults dependency array)
100103
useEffect(() => {
101-
searchNames('');
102-
}, [index, searchNames]);
104+
searchResults();
105+
}, [index, searchResults]);
103106

104107
const searchNamesAfterTimeout = (query: string) => {
105108
if (pendingRequest) {
106109
clearTimeout(pendingRequest);
107110
}
108111
const timeout = setTimeout(() => {
109-
searchNames(query);
112+
searchNames(query, 0);
110113
setPendingRequest(null);
111114
}, SEARCH_TIMEOUT_MS);
112115
setPendingRequest(timeout);

site/src/helpers/util.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export async function searchAPIResults(
7777
transformed[key] = transformGQLData(index, data[id]);
7878
}
7979
}
80+
console.log('api', transformed);
8081
return transformed;
8182
}
8283

0 commit comments

Comments
 (0)