fix: doesn't autoload on pages that don't have pages to load (#270)
* fix: no autoload on singular pages without next page * fix: formatted
This commit is contained in:
@@ -7,6 +7,7 @@ import { SiteSupport } from '@views/lib/getSiteSupport';
|
||||
import type { AutoLoadStatusType } from '@views/lib/loadNextCourseCatalogPage';
|
||||
import {
|
||||
AutoLoadStatus,
|
||||
getNextButton,
|
||||
loadNextCourseCatalogPage,
|
||||
removePaginationButtons,
|
||||
} from '@views/lib/loadNextCourseCatalogPage';
|
||||
@@ -27,6 +28,7 @@ type Props = {
|
||||
export default function AutoLoad({ addRows }: Props): JSX.Element | null {
|
||||
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
||||
const [status, setStatus] = useState<AutoLoadStatusType>(AutoLoadStatus.IDLE);
|
||||
const [isSinglePage, setIsSinglePage] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const portalContainer = document.createElement('div');
|
||||
@@ -35,6 +37,10 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null {
|
||||
setContainer(portalContainer);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
setIsSinglePage(!getNextButton(document));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
removePaginationButtons(document);
|
||||
console.log(`AutoLoad is now ${status}`);
|
||||
@@ -43,6 +49,7 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null {
|
||||
|
||||
// This hook will call the callback when the user scrolls to the bottom of the page.
|
||||
useInfiniteScroll(async () => {
|
||||
if (isSinglePage) return;
|
||||
// fetch the next page of courses
|
||||
const [status, nextRows] = await loadNextCourseCatalogPage();
|
||||
setStatus(status);
|
||||
@@ -55,9 +62,9 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null {
|
||||
|
||||
// add the scraped courses to the current page
|
||||
addRows(scrapedRows);
|
||||
}, [addRows]);
|
||||
}, [addRows, isSinglePage]);
|
||||
|
||||
if (!container || status === AutoLoadStatus.DONE) {
|
||||
if (!container || status === AutoLoadStatus.DONE || isSinglePage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ export async function loadNextCourseCatalogPage(): Promise<[AutoLoadStatusType,
|
||||
* @param doc the document to get the next button from
|
||||
* @returns the next button from the document
|
||||
*/
|
||||
function getNextButton(doc: Document) {
|
||||
export function getNextButton(doc: Document) {
|
||||
return doc.querySelector<HTMLAnchorElement>(NEXT_PAGE_BUTTON_SELECTOR);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user