From cd05e5e7fcaa02dab30f230c178f189d8052a7c9 Mon Sep 17 00:00:00 2001 From: Derek Chen Date: Mon, 20 Jan 2025 18:42:15 -0600 Subject: [PATCH] feat: validate login passed to background and implemented into add all injected button (#443) * docs: proper captializing * docs: confirmed that the for loop is entered but on first startup something else * feat: turns out - validate login status - not in background - pass background now * feat: kronk.gif * Update UserScheduleMessages.ts * Update addCourseByURL.ts * chore: format * chore: chore --------- Co-authored-by: doprz <52579214+doprz@users.noreply.github.com> --- .../background/handler/userScheduleHandler.ts | 4 ++++ src/pages/background/lib/addCourseByURL.ts | 9 +++++---- src/shared/messages/UserScheduleMessages.ts | 8 ++++++++ src/views/components/injected/AddAllButton.tsx | 17 ++++++++++++++--- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/pages/background/handler/userScheduleHandler.ts b/src/pages/background/handler/userScheduleHandler.ts index cee7461d..33bcb52a 100644 --- a/src/pages/background/handler/userScheduleHandler.ts +++ b/src/pages/background/handler/userScheduleHandler.ts @@ -8,6 +8,7 @@ import renameSchedule from '@pages/background/lib/renameSchedule'; import switchSchedule from '@pages/background/lib/switchSchedule'; import type { UserScheduleMessages } from '@shared/messages/UserScheduleMessages'; import { Course } from '@shared/types/Course'; +import { validateLoginStatus } from '@shared/util/checkLoginStatus'; import type { MessageHandler } from 'chrome-extension-toolkit'; const userScheduleHandler: MessageHandler = { @@ -41,6 +42,9 @@ const userScheduleHandler: MessageHandler = { .then(res => (response === 'json' ? res.json() : res.text())) .then(sendResponse); }, + validateLoginStatus({ data, sendResponse }) { + validateLoginStatus(data.url).then(sendResponse); + }, exportSchedule({ data, sendResponse }) { exportSchedule(data.scheduleId).then(sendResponse); }, diff --git a/src/pages/background/lib/addCourseByURL.ts b/src/pages/background/lib/addCourseByURL.ts index c5444ff2..54d48ce8 100644 --- a/src/pages/background/lib/addCourseByURL.ts +++ b/src/pages/background/lib/addCourseByURL.ts @@ -23,7 +23,9 @@ export async function addCourseByURL(activeSchedule: UserSchedule, link?: string if (!link) link = prompt('Enter course link') || undefined; // Exit if the user cancels the prompt - if (!link) return; + if (!link) { + return; + } try { let htmlText: string; @@ -46,17 +48,16 @@ export async function addCourseByURL(activeSchedule: UserSchedule, link?: string const scrapedCourses = scraper.scrape(tableRows, false); if (scrapedCourses.length !== 1) return; - const description = scraper.getDescription(doc); const row = scrapedCourses[0]!; const course = row.course!; course.description = description; if (activeSchedule.courses.every(c => c.uniqueId !== course.uniqueId)) { - console.log('adding course'); + console.log('Adding course'); await addCourse(activeSchedule.id, course); } else { - console.log('course already exists'); + console.log('Course already exists'); } } catch (error) { console.error('Error scraping course:', error); diff --git a/src/shared/messages/UserScheduleMessages.ts b/src/shared/messages/UserScheduleMessages.ts index f7c219e8..b65cdfa9 100644 --- a/src/shared/messages/UserScheduleMessages.ts +++ b/src/shared/messages/UserScheduleMessages.ts @@ -64,6 +64,14 @@ export interface UserScheduleMessages { */ renameSchedule: (data: { scheduleId: string; newName: string }) => string | undefined; + /** + * Checks the login status by making a request to the provided URL. + * + * @param data - The URL to check the login status against. + * @returns true if user was already logged into the provided URL, false otherwise + */ + validateLoginStatus: (data: { url: string }) => boolean; + /** * Exports the current schedule to a JSON file for backing up and sharing * diff --git a/src/views/components/injected/AddAllButton.tsx b/src/views/components/injected/AddAllButton.tsx index f1fbe7c0..bbf20cfa 100644 --- a/src/views/components/injected/AddAllButton.tsx +++ b/src/views/components/injected/AddAllButton.tsx @@ -1,4 +1,6 @@ import { addCourseByURL } from '@pages/background/lib/addCourseByURL'; +import { background } from '@shared/messages'; +import { validateLoginStatus } from '@shared/util/checkLoginStatus'; import { Button } from '@views/components/common/Button'; import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot'; import useSchedules from '@views/hooks/useSchedules'; @@ -30,9 +32,18 @@ export default function InjectedButton(): JSX.Element | null { // Make sure to remove duplicate anchorTags using set const uniqueAnchorTags = Array.from(new Set(anchorTags.map(a => a.href))); - for (const a of uniqueAnchorTags) { - // eslint-disable-next-line no-await-in-loop - await addCourseByURL(activeSchedule, a); + // Make sure user is logged in + const loggedInToUT = await background.validateLoginStatus({ + url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/utrp_login/', + }); + + if (loggedInToUT) { + for (const a of uniqueAnchorTags) { + // eslint-disable-next-line no-await-in-loop + await addCourseByURL(activeSchedule, a); + } + } else { + window.alert('Logged into UT Registrar.'); } };