Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): add new search link #456

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/views/components/CourseCatalogMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Course, ScrapedRow } from '@shared/types/Course';
import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot';
import AutoLoad from '@views/components/injected/AutoLoad/AutoLoad';
import CourseCatalogInjectedPopup from '@views/components/injected/CourseCatalogInjectedPopup/CourseCatalogInjectedPopup';
import NewSearchLink from '@views/components/injected/NewSearchLink';
import RecruitmentBanner from '@views/components/injected/RecruitmentBanner/RecruitmentBanner';
import TableHead from '@views/components/injected/TableHead';
import TableRow from '@views/components/injected/TableRow/TableRow';
Expand Down Expand Up @@ -61,6 +62,7 @@ export default function CourseCatalogMain({ support }: Props): JSX.Element | nul

return (
<ExtensionRoot>
<NewSearchLink />
<RecruitmentBanner />
<TableHead>Plus</TableHead>
{rows.map(
Expand Down
49 changes: 49 additions & 0 deletions src/views/components/injected/NewSearchLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';

/**
* This creates a 'Begin a new search' link above the course catalog table.
*
* @returns a react portal to the new link container or null if the container is not found.
*/
export default function NewSearchLink() {
const [container, setContainer] = useState<HTMLElement | null>(null);
const newContainerId = 'ut-registration-plus-new-search-link';

const searchLink = document.querySelector('#bottom_nav > p:nth-child(2)');
const linkContent = {
href: searchLink?.querySelector('a')?.href,
title: searchLink?.querySelector('a')?.title,
text: searchLink?.querySelector('a')?.textContent,
};

useEffect(() => {
if (document.getElementById(newContainerId)) {
return;
}

const innerBody = document.querySelector('#inner_body');
if (!innerBody) {
return;
}

const containerElement = document.createElement('div');
containerElement.setAttribute('id', newContainerId);

innerBody.prepend(containerElement);
setContainer(containerElement);
}, []);

if (!container) {
return null;
}

return createPortal(
<p>
<a href={linkContent.href} title={linkContent.title}>
{linkContent.text}
</a>
</p>,
container
);
}
Loading