From 5aa1be5fe03c7bec2cfb444fa8a4e0c87a71a23a Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:49:56 +0100 Subject: [PATCH] Implement optimized date suggestions based on the timetable --- src/constants/weekDays.ts | 2 +- src/lib/dates/dataWeekday.ts | 23 +++++ src/lib/helpers/mapValues.ts | 15 ++++ .../homework/CreateHomeworkAssignment.svelte | 87 +++++++++++++++++++ src/lib/homework/CreateHomeworkInner.svelte | 74 +--------------- src/lib/timetable/index.ts | 46 ++++++++++ src/routes/settings/timetable/+page.svelte | 4 +- 7 files changed, 176 insertions(+), 75 deletions(-) create mode 100644 src/lib/helpers/mapValues.ts create mode 100644 src/lib/homework/CreateHomeworkAssignment.svelte create mode 100644 src/lib/timetable/index.ts diff --git a/src/constants/weekDays.ts b/src/constants/weekDays.ts index 875d86b..53c4297 100644 --- a/src/constants/weekDays.ts +++ b/src/constants/weekDays.ts @@ -7,7 +7,7 @@ export const emptyTimeTable = { fr: [], sa: [], su: [] -} satisfies TimeTable; +} as TimeTable; export type WeekDay = (typeof weekdays)[number]; diff --git a/src/lib/dates/dataWeekday.ts b/src/lib/dates/dataWeekday.ts index fc32059..99187c3 100644 --- a/src/lib/dates/dataWeekday.ts +++ b/src/lib/dates/dataWeekday.ts @@ -49,4 +49,27 @@ export function getWeekdayByAbbreviation(abbreviation: WeekDay) { } as const); } +export const getCurrentWeekdayAbbreviation = () => { + const weekdayIndex = new Date().getDay(); + + switch (weekdayIndex) { + case 0: + return 'su'; + case 1: + return 'mo'; + case 2: + return 'tu'; + case 3: + return 'we'; + case 4: + return 'th'; + case 5: + return 'fr'; + case 6: + return 'sa'; + default: + return 'mo'; + } +}; + export const getWeekdays = () => token.map((key) => i(key)) as WeekdayTranslation[]; diff --git a/src/lib/helpers/mapValues.ts b/src/lib/helpers/mapValues.ts new file mode 100644 index 0000000..f29de1c --- /dev/null +++ b/src/lib/helpers/mapValues.ts @@ -0,0 +1,15 @@ +type ObjectOf = Record; + +/** + * Map over the values of an object, returning a new object with the same keys + * and mapped values. + */ +export const mapValues = (object: ObjectOf, fn: (value: T) => U) => { + // iterate over the keys of the object + const newObject: ObjectOf = {}; + for (const key of Object.keys(object)) { + const mappedValue = fn(object[key]); + newObject[key] = mappedValue; + } + return newObject; +}; diff --git a/src/lib/homework/CreateHomeworkAssignment.svelte b/src/lib/homework/CreateHomeworkAssignment.svelte new file mode 100644 index 0000000..24761b6 --- /dev/null +++ b/src/lib/homework/CreateHomeworkAssignment.svelte @@ -0,0 +1,87 @@ + + +
+ { + const index = allAssignments.indexOf(assignment); + const newIndex = index - 1; + + allAssignments = swapArrayElements(allAssignments, index, newIndex); + }} + disabled={allAssignments.indexOf(assignment) === 0} + /> + { + const index = allAssignments.indexOf(assignment); + const newIndex = index + 1; + + allAssignments = swapArrayElements(allAssignments, index, newIndex); + }} + disabled={allAssignments.indexOf(assignment) === allAssignments.length - 1} + /> +
+
+ +
+ + + + + {#each subjectsSortetCapitalized as subj} + + + + +
+
+ +