From 28e3b64780e3439eae737198b40fa7899be1b9e1 Mon Sep 17 00:00:00 2001 From: ArtrenH <79058115+ArtrenH@users.noreply.github.com> Date: Sat, 30 Sep 2023 00:44:02 +0200 Subject: [PATCH 01/61] Favourites work-in-progress in frontend --- client/src/App.svelte | 60 ++----- client/src/Favourites.svelte | 254 ++++++++++++++++++++++++++++ client/src/components/Navbar.svelte | 1 + client/src/components/Plan.svelte | 24 ++- client/src/utils.js | 30 +++- 5 files changed, 316 insertions(+), 53 deletions(-) create mode 100644 client/src/Favourites.svelte diff --git a/client/src/App.svelte b/client/src/App.svelte index bbf8c4d..5b9f9e8 100644 --- a/client/src/App.svelte +++ b/client/src/App.svelte @@ -6,17 +6,18 @@ import Navbar from "./components/Navbar.svelte"; import Settings from "./components/Settings.svelte"; import AboutUs from "./components/AboutUs.svelte"; + import Favourites from "./Favourites.svelte"; import SveltyPicker from 'svelty-picker'; import {get_settings, group_rooms, update_colors, navigate_page, init_indexed_db, clear_plan_cache} from "./utils.js"; import {notifications} from './notifications.js'; import {logged_in, title, current_page, preferences, settings, active_modal, pwa_prompt, indexed_db} from './stores.js' - import {customFetch, format_revision_date} from "./utils.js"; import SchoolManager from "./components/SchoolManager.svelte"; import Preferences from "./components/Preferences.svelte"; import Changelog from "./components/Changelog.svelte"; import Select from "./base_components/Select.svelte"; import Contact from "./components/Contact.svelte"; import Impressum from "./components/Impressum.svelte"; + import {customFetch, format_revision_date, load_meta} from "./utils.js"; import {de} from 'svelty-picker/i18n'; import PwaInstallHelper from "./components/PWAInstallHelper.svelte"; import Dropdown from "./base_components/Dropdown.svelte"; @@ -84,51 +85,22 @@ } function get_meta() { - let data_from_cache = false; - let data = localStorage.getItem(`${school_num}_meta`); - if (data !== "undefined" && data) { - data = JSON.parse(data); - meta = data; - all_rooms = data.rooms; - teacher_list = Object.keys(data.teachers); - grouped_forms = data.forms.grouped_forms; - enabled_dates = Object.keys(data.dates); - if(!date) { - date = data.date; - } - course_lists = data.forms.forms; - data_from_cache = true; - } - customFetch(`${api_base}/meta`) + load_meta(school_num) .then(data => { - // console.log("Meta geladen"); - try { - localStorage.setItem(`${school_num}_meta`, JSON.stringify(data)); - } catch (error) { - if (error.name === 'QuotaExceededError' ) { - notifications.danger("Die Schulmetadaten konnten nicht gecached werden.") - } else { - throw error; - } + if (!data) { + return } - meta = data; - all_rooms = data.rooms; - teacher_list = Object.keys(data.teachers); - grouped_forms = data.forms.grouped_forms; - enabled_dates = Object.keys(data.dates); + meta = data[0]; + all_rooms = meta.rooms; + teacher_list = Object.keys(meta.teachers); + grouped_forms = meta.forms.grouped_forms; + enabled_dates = Object.keys(meta.dates); if(!date) { - date = data.date; + date = meta.date; } - course_lists = data.forms.forms; + course_lists = meta.forms.forms; + //data_from_cache = data[1]; }) - .catch(error => { - if (data_from_cache) { - // notifications.info("Metadaten aus Cache geladen", 2000); - console.log("Metadaten aus Cache geladen"); - } else { - notifications.danger(error.message); - } - }); } function check_login_status() { @@ -293,7 +265,7 @@ plan_type = decodeURI(tmp_variables[3]); plan_value = decodeURI(tmp_variables[4]); } - } + } $logged_in = localStorage.getItem('logged_in') === 'true'; init_vars(); @@ -311,7 +283,7 @@ $: (Object.keys($settings).length !== 0) && localStorage.setItem("settings", `${JSON.stringify($settings)}`); $: update_colors($settings); $: $logged_in && get_greeting(); - + $: selected_form && set_plan("forms", selected_form); $: gen_form_arr(grouped_forms); $: selected_teacher && set_plan("teachers", selected_teacher); @@ -372,6 +344,8 @@ {:else if $current_page === "impressum"} + {:else if $current_page === "favourites"} + {:else if $logged_in} {#if $current_page.substring(0, 4) === "plan" || $current_page === "weekplan"}

{emoji} {greeting}

diff --git a/client/src/Favourites.svelte b/client/src/Favourites.svelte new file mode 100644 index 0000000..8fd6a4f --- /dev/null +++ b/client/src/Favourites.svelte @@ -0,0 +1,254 @@ + + + + +{#each favourites as _, favourite} +

+ + + + + {#if favourites[favourite].plan_type === "forms"} + + + {#each Object.entries( + get_subjects(favourite, all_meta) + ).sort(([subj1, _], [subj2, __]) => subj1.localeCompare(subj2)).sort(([_, courses1], [__, courses2]) => courses2.length - courses1.length) as [subject, courses]} + {#if courses.length === 1} +

  • {subject}: + {courses[0].class_number} + {courses[0].teacher} | + {courses[0].subject} + {#if courses[0].group != null} + ({courses[0].group}) + {/if} +
  • + {:else} +
  • + {subject} + {#if courses.length > 2} + + + {/if} +
  • +
      + {#each courses as course} +
    • + + {course.class_number} + {course.teacher} | + {course.subject} + {#if course.group != null} + ({course.group}) + {/if} +
    • + {/each} +
    + {/if} + {/each} + + + + {:else if favourites[favourite].plan_type !== "free_rooms"} + + {/if} + +

    +{/each} + + + \ No newline at end of file diff --git a/client/src/components/Navbar.svelte b/client/src/components/Navbar.svelte index f6e8408..e340482 100644 --- a/client/src/components/Navbar.svelte +++ b/client/src/components/Navbar.svelte @@ -32,6 +32,7 @@ + diff --git a/client/src/components/Plan.svelte b/client/src/components/Plan.svelte index 4e917b1..2ab8ec6 100644 --- a/client/src/components/Plan.svelte +++ b/client/src/components/Plan.svelte @@ -94,7 +94,10 @@ week_letter = info.week; } - function load_plan(date, revision=".newest") { + function load_plan(date, revision=".newest", enabled_dates) { + if (enabled_dates === null || enabled_dates === undefined) { + return; + } if (date === null || date === undefined || !enabled_dates.includes(date)) { return; } @@ -139,6 +142,7 @@ data_from_cache = false; }) .catch(error => { + console.error(error); loading = false; network_loading_failed = true; }); @@ -253,7 +257,7 @@ function load_lessons_check_plan(plan_data, plan_type, plan_value, use_grouped_form_plans) { let curr_time = new Date(); if(!last_updated || curr_time - last_updated > 30_000) { - load_plan(date, selected_revision); + load_plan(date, selected_revision, enabled_dates); last_updated = new Date(); } @@ -269,16 +273,18 @@ // console.log("Mounted Plan.svelte"); }); - $: $indexed_db, load_plan(date, selected_revision); + $: $indexed_db, load_plan(date, selected_revision, enabled_dates); $: load_lessons_check_plan(plan_data, plan_type, plan_value, $settings.use_grouped_form_plans); $: if (plan_type === "teachers") { - full_teacher_name = meta.teachers[plan_value]?.surname || null; - teacher_contact_link = meta.teachers[plan_value]?.contact_link || null; - teacher_image_path = "/public/base_static/images/teachers/" + school_num + "/" + meta.teachers[plan_value]?.image_path || null; - teacher_image_path = meta.teachers[plan_value]?.image_path || null; - if (teacher_image_path) { - teacher_image_path = `/public/base_static/images/teachers/${school_num}/${teacher_image_path}`; + if (meta.teachers) { + full_teacher_name = meta.teachers[plan_value]?.surname || null; + teacher_contact_link = meta.teachers[plan_value]?.contact_link || null; + teacher_image_path = "/public/base_static/images/teachers/" + school_num + "/" + meta.teachers[plan_value]?.image_path || null; + teacher_image_path = meta.teachers[plan_value]?.image_path || null; + if (teacher_image_path) { + teacher_image_path = `/public/base_static/images/teachers/${school_num}/${teacher_image_path}`; + } } } diff --git a/client/src/utils.js b/client/src/utils.js index e28b953..c389744 100644 --- a/client/src/utils.js +++ b/client/src/utils.js @@ -417,4 +417,32 @@ export function arraysEqual(a, b) { if (a[i] !== b[i]) return false; } return true; -} \ No newline at end of file +} + +export async function load_meta(school_num) { + let meta_data; + return customFetch(`/api/v69.420/${school_num}/meta`) + .then(data => { + try { + localStorage.setItem(`${school_num}_meta`, JSON.stringify(data)); + } catch (error) { + if (error.name === 'QuotaExceededError' ) { + notifications.danger("Die Schulmetadaten konnten nicht gecached werden.") + } else { + throw error; + } + } + meta_data = data; + return [meta_data, false] + }) + .catch(error => { + meta_data = localStorage.getItem(`${school_num}_meta`); + if (meta_data !== "undefined" && meta_data) { + console.log("Metadaten aus Cache geladen"); + return [JSON.parse(meta_data), true]; + } else { + console.log("Metadaten konnten nicht geladen werden."); + notifications.danger(error.message); + } + }) +} From fe5148940460d99a5faa688bc5500b4cd6537717 Mon Sep 17 00:00:00 2001 From: ArtrenH <79058115+ArtrenH@users.noreply.github.com> Date: Sun, 1 Oct 2023 03:47:57 +0200 Subject: [PATCH 02/61] favourits now usable in frontend --- client/src/App.svelte | 40 ++++++++-- client/src/Favourites.svelte | 115 +++++++++++++++++----------- client/src/components/Navbar.svelte | 13 +++- client/src/components/Plan.svelte | 15 ++-- client/src/stores.js | 9 ++- 5 files changed, 135 insertions(+), 57 deletions(-) diff --git a/client/src/App.svelte b/client/src/App.svelte index 5b9f9e8..3637fcb 100644 --- a/client/src/App.svelte +++ b/client/src/App.svelte @@ -10,7 +10,7 @@ import SveltyPicker from 'svelty-picker'; import {get_settings, group_rooms, update_colors, navigate_page, init_indexed_db, clear_plan_cache} from "./utils.js"; import {notifications} from './notifications.js'; - import {logged_in, title, current_page, preferences, settings, active_modal, pwa_prompt, indexed_db} from './stores.js' + import {logged_in, title, current_page, preferences, settings, active_modal, pwa_prompt, indexed_db, selected_favourite, favourites} from './stores.js' import SchoolManager from "./components/SchoolManager.svelte"; import Preferences from "./components/Preferences.svelte"; import Changelog from "./components/Changelog.svelte"; @@ -118,7 +118,7 @@ ); } - function get_preferences() { + /*function get_preferences() { customFetch(`${api_base}/preferences`) .then(data => { preferences.set(data); @@ -126,7 +126,7 @@ .catch(error => { console.error("Preferences konnten nicht geladen werden."); }) - } + }*/ function choose(choices) { var index = Math.floor(Math.random() * choices.length); @@ -226,7 +226,7 @@ function gen_revision_arr(all_revisions) { revision_arr = []; for(const [index, revision] of Object.entries(all_revisions)) { - if(index == 1) {continue;} + if (index == 1) {continue;} revision_arr.push({ "id": revision, "display_name": format_revision_date(revision, all_revisions[1]) @@ -267,6 +267,32 @@ } } + function select_plan(favourites, selected_favourite) { + // check if selected_favourite is in favourites (selected_favourite is the index) + if (selected_favourite === -1) { + plan_type = ""; + plan_value = ""; + } + if (selected_favourite !== null && favourites[selected_favourite]) { + selected_favourite = favourites[selected_favourite]; + school_num = selected_favourite.school_num; + localStorage.setItem('school_num', school_num); + plan_type = selected_favourite.plan_type; + plan_value = selected_favourite.plan_value; + } + + } + + $: select_plan($favourites, $selected_favourite); + $: if ($selected_favourite !== -1) { + if ($favourites[$selected_favourite].plan_value !== plan_value) { + selected_favourite.set(-1); + } else if ($favourites[$selected_favourite].plan_type !== plan_type) { + selected_favourite.set(-1); + } + } + + $logged_in = localStorage.getItem('logged_in') === 'true'; init_vars(); check_login_status(); @@ -277,7 +303,7 @@ $: school_num && (api_base = `/api/v69.420/${school_num}`); $: school_num && get_meta(); $: all_revisions = [".newest"].concat((meta?.dates || {})[date] || []); - $: school_num && get_preferences(); + //$: school_num && get_preferences(); $: all_rooms && (grouped_rooms = group_rooms(all_rooms)); $: $logged_in && get_settings(); $: (Object.keys($settings).length !== 0) && localStorage.setItem("settings", `${JSON.stringify($settings)}`); @@ -349,6 +375,10 @@ {:else if $logged_in} {#if $current_page.substring(0, 4) === "plan" || $current_page === "weekplan"}

    {emoji} {greeting}

    + {#if $selected_favourite !== -1 && $favourites[$selected_favourite]} + Gewählter Favorit: {$favourites[$selected_favourite].name} +
    + {/if}
    diff --git a/client/src/Favourites.svelte b/client/src/Favourites.svelte index 8fd6a4f..02e8345 100644 --- a/client/src/Favourites.svelte +++ b/client/src/Favourites.svelte @@ -1,33 +1,49 @@