From 541f0d6e9f5cf08f3934e91d423fee5d8ce2e2e7 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Thu, 6 Jun 2024 17:35:41 +0200 Subject: [PATCH 01/14] new degrees config format --- src/lib/teachings.ts | 149 +++++++++++++------ src/routes/+page.svelte | 2 +- src/routes/[...dir]/[zfile=dir]/+page.svelte | 18 +-- src/routes/build/+page.svelte | 8 +- src/routes/dash/[course]/+page.svelte | 27 +++- 5 files changed, 138 insertions(+), 66 deletions(-) diff --git a/src/lib/teachings.ts b/src/lib/teachings.ts index 09419c3c..dfcab00d 100644 --- a/src/lib/teachings.ts +++ b/src/lib/teachings.ts @@ -2,73 +2,135 @@ import { getManifest } from '$lib/api'; import { filterAsync } from './filter'; export type Teaching = { - name: string; - url: string; - chat?: string; - website?: string; - professors?: string[]; - degree?: string; + name: string; + url: string; + chat?: string; + website?: string; + professors?: string[]; + degree?: string; }; -export type YearStudyDiagram = { - mandatory?: string[]; - electives?: string[]; +export type Year = { + year: number; + chat?: string; }; -export type Year = { - year: number; - chat?: string; - teachings: YearStudyDiagram; +export type DegreeTeaching = { + name: string; + year?: number; + mandatory: boolean; }; export type Degree = { - id: string; - name: string; - icon: string; - years?: Year[]; - chat?: string; + id: string; + name: string; + icon: string; + teachings?: DegreeTeaching[]; + years?: Year[]; + chat?: string; }; async function isTeachingActiveFromName(fetch: typeof window.fetch, teaching: string) { - try { - await getManifest(fetch, teaching); - return true; - } catch { - return false; - } + try { + await getManifest(fetch, teaching); + return true; + } catch { + return false; + } } export async function isTeachingActive(fetch: typeof window.fetch, teaching: Teaching) { - return isTeachingActiveFromName(fetch, teaching.url); + return isTeachingActiveFromName(fetch, teaching.url); +} + +export function yearToFlatTeachings(d: Degree, y: number): string[] { + let res: string[] = []; + + if (d.teachings) { + d.teachings.forEach((x) => { + if ((x.year && x.year == y) || (!x.year && y == 0)) + res = res.concat(x.name); + }); + } + + //if (studyDiagram.mandatory) res = res.concat(studyDiagram.mandatory); + //if (studyDiagram.electives) res = res.concat(studyDiagram.electives); + return res; } -export function yearToFlatTeachings(y: Year): string[] { - let res: string[] = []; - const studyDiagram = y.teachings; - if (studyDiagram.mandatory) res = res.concat(studyDiagram.mandatory); - if (studyDiagram.electives) res = res.concat(studyDiagram.electives); - return res; +export function allMandatoryTeachingsFromYear(d: Degree, y: number): string[] { + let res: string[] = []; + + if (d.teachings) { + d.teachings.forEach((x) => { + if ((x.year && x.year == y) || (!x.year && y == 0)) { + if (x.mandatory) { + res = res.concat(x.name); + } + } + }); + } + + return res; +} + +export function allElectivesTeachingsFromYear(d: Degree, y: number): string[] { + let res: string[] = []; + + if (d.teachings) { + d.teachings.forEach((x) => { + if ((x.year && x.year == y) || (!x.year && y == 0)) { + if (!x.mandatory) { + res = res.concat(x.name); + } + } + }); + } + + return res; +} + +export function allTeachingsMandatoryElectives(d: Degree, mandatory: boolean): string[] { + let res: string[] = []; + if (d.teachings) { + d.teachings.forEach((x) => { + if (x.mandatory == mandatory) { + res = res.concat(x.name); + } + }); + } + return res; +} + +export function allTeachingsToString(d: Degree): string[] { + let res: string[] = []; + + if (d.teachings) { + d.teachings.forEach((x) => (res = res.concat(x.name))); + } + + return res; } export async function getActiveTeachings( - fetch: typeof window.fetch, - degree: Degree + fetch: typeof window.fetch, + degree: Degree ): Promise { - const years = degree.years; - if (!years) return []; - const allTeachings = years.flatMap(yearToFlatTeachings); - const activeTeachings = await filterAsync(allTeachings, (t) => - isTeachingActiveFromName(fetch, t) - ); - - return activeTeachings; + const years = degree.years; + if (!years) return []; + const allTeachings = allTeachingsToString(degree); + const activeTeachings = await filterAsync(allTeachings, (t) => + isTeachingActiveFromName(fetch, t) + ); + + return activeTeachings; } // TEACHINGS import teachingsArray from '../config/teachings.json' assert { type: 'json' }; function teachingToIndexedTeaching(t: Teaching): [string, Teaching] { - return [t.url, t]; + return [t.url, t]; } const TEACHINGS = new Map(teachingsArray.map(teachingToIndexedTeaching)); @@ -76,4 +138,7 @@ export { TEACHINGS }; // DEGREES import DEGREES from '../config/degrees.json' assert { type: 'Degree[]' }; +import { text } from '@sveltejs/kit'; +import { expoOut } from 'svelte/easing'; +import { derived } from 'svelte/store'; export { DEGREES }; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 5f9dbe5b..4df7ee23 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -57,7 +57,7 @@