Skip to content

Commit

Permalink
feat: validate login passed to background and implemented into add al…
Browse files Browse the repository at this point in the history
…l 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 <[email protected]>
  • Loading branch information
DereC4 and doprz authored Jan 21, 2025
1 parent f425510 commit cd05e5e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/pages/background/handler/userScheduleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserScheduleMessages> = {
Expand Down Expand Up @@ -41,6 +42,9 @@ const userScheduleHandler: MessageHandler<UserScheduleMessages> = {
.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);
},
Expand Down
9 changes: 5 additions & 4 deletions src/pages/background/lib/addCourseByURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/shared/messages/UserScheduleMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
17 changes: 14 additions & 3 deletions src/views/components/injected/AddAllButton.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.');
}
};

Expand Down

0 comments on commit cd05e5e

Please sign in to comment.