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>
This commit is contained in:
@@ -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> = {
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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,10 +32,19 @@ 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)));
|
||||
|
||||
// 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.');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user