Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist selected class #88

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/selectedClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { get, writable } from "svelte/store";

const key = "selectedClass";

interface Class {
classNumber: string;
classLetter: string;
}

const selectedClass = writable<Class>({
classNumber: "-1",
classLetter: "0",
});

const { subscribe, set } = selectedClass;

const isBrowser = () => typeof window != "undefined" && localStorage;

if (isBrowser()) {
const selectedClassStored = localStorage.getItem(key);

if (selectedClassStored) {
selectedClass.set(JSON.parse(selectedClassStored));
}
}

const saveValue = (value) => {
if (value) {
if (isBrowser()) {
localStorage.setItem(key, JSON.stringify(value));
}

set(value);
}
};

const selectedClassStore = {
subscribe,
set: (value) => saveValue(value),
update: (updateFunction: (selectedClass) => Class) => {
const updatedStore = updateFunction(get(selectedClass));

saveValue(updatedStore);
},
get: () => get(selectedClass),
};

export { selectedClassStore };
19 changes: 17 additions & 2 deletions src/routes/student/books/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { env } from "$env/dynamic/public";
import BookPreview from "$lib/BookPreview.svelte";
import { onMount } from "svelte";
import { selectedClassStore } from "../../../lib/selectedClass";

setLayout(secondaryLayout)

Expand All @@ -22,6 +23,12 @@
const changeBooks = () => {
const className = classSelect.options[classSelect.selectedIndex].value

selectedClassStore.update((selectedClass) => {
selectedClass.classNumber = className

return selectedClass
})

const QUERY = `
query($className: String!) {
Books(where: {class: {like: $className}}, limit: 100) {
Expand Down Expand Up @@ -49,9 +56,17 @@
}

onMount(() => {
if (data.classNumber && classNumbers.indexOf(parseInt(data.classNumber)) !== -1) {
classSelect.selectedIndex = classNumbers.indexOf(parseInt(data.classNumber))
let classNumber
if (data.classNumber) {
classNumber = data.classNumber
} else {
classNumber = selectedClassStore.get().classNumber
}

if (classNumber && classNumbers.indexOf(classNumber) !== -1) {
classSelect.selectedIndex = classNumbers.indexOf(classNumber)
}

changeBooks()
})
</script>
Expand Down
61 changes: 42 additions & 19 deletions src/routes/student/weekly-schedule/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { onMount } from "svelte";
import { env } from "$env/dynamic/public";
import { getClassNumberName } from "../../../lib/classNumberNames";
import { selectedClassStore } from "../../../lib/selectedClass";

setLayout(secondaryLayout)

Expand All @@ -14,6 +15,13 @@
const dateFormatOptions: Intl.DateTimeFormatOptions = {hour: '2-digit', minute: '2-digit', hour12: false}

const classes = data.WeeklySchedules.docs.map((val) => val.class)
const maxNumberOfLessons = data.Schedule.weeklySchedule.weeklySchedulesAutofill.maxNumberOfLessons

let classNumberSelect;
let classLetterSelect;
let currentClassNumber;
let currentClassLetter;
let days = [];

const classNumbers = [...new Set(classes.map((val) => {
return val.match(/\d+/)[0]
Expand All @@ -23,12 +31,19 @@
return val.match(/[A-Za-zА-Яа-я]/)[0]
}))].sort()

let classNumberSelect;
let classLetterSelect;
let currentClassNumber;
let currentClassLetter;
let days = [];
const maxNumberOfLessons = data.Schedule.weeklySchedule.weeklySchedulesAutofill.maxNumberOfLessons
const isLetterDisabled = (classNumber: string, classLetter: string) => {
return classNumberSelect && !classes.includes(classNumber + classLetter)
}

const validateClassNumber = (classNumber) => {
return classNumber && classNumbers.indexOf(classNumber) !== -1;
}

const validateClassLetter = (classNumber, classLetter) => {
return classLetter &&
classLetters.indexOf(classLetter) !== -1 &&
!isLetterDisabled(classNumber, classLetter)
}

const changeSchedule = () => {
currentClassNumber = classNumberSelect.value
Expand All @@ -40,11 +55,13 @@
currentClassLetter = classLetterSelect.value
}

setSchedule(currentClassNumber + currentClassLetter)
}
selectedClassStore.update((selectedClass) => {
selectedClass.classNumber = currentClassNumber
selectedClass.classLetter = currentClassLetter

const isLetterDisabled = (classNumber: string, classLetter: string) => {
return classNumberSelect && !classes.includes(classNumber + classLetter)
return selectedClass
})
setSchedule(currentClassNumber + currentClassLetter)
}

const hours = data.Schedule.dailySchedule.hours.slice(0)
Expand Down Expand Up @@ -73,18 +90,24 @@
}

onMount(() => {
let classLetter;
let classNumber;

if (data.className) {
let classNumber = data.className.match(/\d+/)[0]
if (classNumber && classNumbers.indexOf(classNumber) !== -1) {
classNumberSelect.value = classNumber
let classLetter = data.className.match(/[A-Za-zА-Яа-я]/)[0]
if (classLetter &&
classLetters.indexOf(classLetter) !== -1 &&
!isLetterDisabled(classNumber, classLetter)) {
classLetterSelect.value = classLetter
}
classNumber = data.className.match(/\d+/)[0]
classLetter = data.className.match(/[A-Za-zА-Яа-я]/)[0]
} else {
classNumber = selectedClassStore.get().classNumber
classLetter = selectedClassStore.get().classLetter
}

if (validateClassNumber(classNumber)) {
classNumberSelect.value = classNumber
if (validateClassLetter(classNumber, classLetter)) {
classLetterSelect.value = classLetter
}
}

changeSchedule()
})
</script>
Expand Down