Skip to content

Commit

Permalink
finish subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Nov 18, 2024
1 parent 311f207 commit f82e009
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
24 changes: 20 additions & 4 deletions src/entrypoints/sig.content/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { storage } from "wxt/storage";
import { scrapeMenu } from "@/scripts/sig/homepage";
import { scrapeMenu, type Student } from "@/scripts/sig/homepage";
import { successToast } from "@/utils/toasts";
import "toastify-js/src/toastify.css";
import '@/assets/tailwind.css'
import { createStudent } from "@/services/next";

export default defineContentScript({
async main() {
Expand All @@ -19,10 +20,25 @@ export default defineContentScript({
if (shouldFormatItinerary) {
// fix here the way a receive the curriculum year, maybe asking for the user
// is the best use case
const student = await scrapeMenu($trs);
const student = await scrapeMenu($trs) as NonNullable<Student>;
storage.setItem("sync:student", student);
// this will be acessed in the ufabc matriculas, to be filtered.
storage.setItem('session:studied', student?.graduation.components)
// create the student for next - update code to handle the same ra in BCT and BCC
// it should increment the graduation with the BCC data.
console.log(student)
// storage.setItem("sync:student", student);
// storage.setItem('session:studied', student?.graduation.components)
// await createStudent({
// ra: student.ra,
// components: student.graduation.components,
// // grade: student.graduation.curriculumYear,
// // graduation data
// "mandatory_credits_number": 90,
// "limited_credits_number": 57,
// "free_credits_number": 43,
// "credits_total": 190
// })


successToast.showToast();
}

Expand Down
25 changes: 16 additions & 9 deletions src/scripts/sig/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@/services/ufabc-parser";
import { transformCourseName, type Course } from "@/utils/transformCourse";
import { capitalizeStr } from "@/utils/capitalizeStr";
import { getPaginatedSubjects } from "@/services/next";
import { findSubjectByName, getPaginatedSubjects, type PaginatedSubjects } from "@/services/next";

type SigStudent = {
matricula: string;
Expand Down Expand Up @@ -158,8 +158,9 @@ export async function scrapeMenu(
curriculumByRa?.grade,
);


const hydrateSigComponentsPromises = graduationHistory.map(c => hydrateSigComponent(c))
const subjects = await getPaginatedSubjects();
console.log(subjects)
const hydrateSigComponentsPromises = graduationHistory.map(c => hydrateSigComponent(c, subjects))
const hydrateSigComponents = await Promise.all(hydrateSigComponentsPromises)
const components = hydrateSigComponents.map((component) =>
hydrateComponents(component, curriculumComponents.components),
Expand Down Expand Up @@ -281,13 +282,19 @@ function hydrateComponents(
};
}

async function hydrateSigComponent(sigComponent: SigComponent) {
const subjects = await getPaginatedSubjects(true);
const match = subjects.data.find(
subject => normalizeDiacritics(subject.name.toLowerCase()) === normalizeDiacritics(sigComponent.name.toLowerCase())
);
async function hydrateSigComponent(sigComponent: SigComponent, subjects: PaginatedSubjects) {

const match = subjects.data.find(s => s.name.toLowerCase() === sigComponent.name.toLowerCase())

if(!match) {
return {
...sigComponent,
credits: 0,
}
}

return {
...sigComponent,
credits: match?.credits ?? 0, // Default to 0 if no match found
credits: match.credits,
};
}
54 changes: 27 additions & 27 deletions src/services/next.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Student } from "@/scripts/sig/homepage";
import { ofetch } from "ofetch";

type PaginatedSubjects = {
export type PaginatedSubjects = {
total: number;
pages: number
data: {
Expand All @@ -9,6 +10,8 @@ type PaginatedSubjects = {
}[]
}

const SUBJECTS_CACHE_KEY = 'next:subjects'

function resolveEndpoint() {
if (import.meta.env.PROD) {
return 'https://api.v2.ufabcnext.com'
Expand All @@ -22,35 +25,32 @@ export const nextService = ofetch.create({
baseURL: resolveEndpoint(),
})

export async function getPaginatedSubjects(fetchAll = false) {
const ITEMS_PER_PAGE = 200;

const firstPage = await nextService<PaginatedSubjects>("/entities/subjects", {
export async function getPaginatedSubjects(page = 1, limit = 2_000) {
const cachedSubjects = await storage.getItem<PaginatedSubjects>(
`session:${SUBJECTS_CACHE_KEY}`,
);

if (cachedSubjects) {
return cachedSubjects;
}

const paginatedSubjects = await nextService<PaginatedSubjects>("/entities/subjects", {
params: {
page: 1,
limit: ITEMS_PER_PAGE,
page: page,
limit: limit,
},
});

if (!fetchAll || firstPage.pages <= 1) {
return firstPage;
}
const remainingPages = Array.from({ length: firstPage.pages - 1 }, (_, i) =>
nextService<PaginatedSubjects>("/entities/subjects", {
params: {
page: i + 2, // Start from page 2
limit: ITEMS_PER_PAGE,
},
})
);

const additionalPages = await Promise.all(remainingPages);

const allData = {
total: firstPage.total,
pages: firstPage.pages,
data: [...firstPage.data, ...additionalPages.flatMap((page) => page.data)],
};

return allData;
storage.setItem(`session:${SUBJECTS_CACHE_KEY}`, paginatedSubjects)
return paginatedSubjects
}


export async function createStudent(student: Student) {
const createdStudent = await nextService('/entities/student', {
method: 'POST',
body: student,
})
return createdStudent;
}

0 comments on commit f82e009

Please sign in to comment.