This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement optimized date suggestions based on the timetable
- Loading branch information
Showing
7 changed files
with
176 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
type ObjectOf<T> = Record<string | number | symbol, T>; | ||
|
||
/** | ||
* Map over the values of an object, returning a new object with the same keys | ||
* and mapped values. | ||
*/ | ||
export const mapValues = <T, U>(object: ObjectOf<T>, fn: (value: T) => U) => { | ||
// iterate over the keys of the object | ||
const newObject: ObjectOf<U> = {}; | ||
for (const key of Object.keys(object)) { | ||
const mappedValue = fn(object[key]); | ||
newObject[key] = mappedValue; | ||
} | ||
return newObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<script lang="ts"> | ||
import I18n from '$lib/I18n.svelte'; | ||
import { i } from '../../languages/i18n'; | ||
import { subjectsSortetCapitalized } from '../../constants/subjecticons'; | ||
import QuickActionButton from '$lib/QuickActionButton.svelte'; | ||
import DatePicker from '$lib/dates/DatePicker.svelte'; | ||
import { swapArrayElements } from '$lib/utils/SwapItems'; | ||
import type { Assignment } from '../../types/homework'; | ||
import { nextCustomDateForWeekday, nextWeekdayForSubject } from '$lib/timetable'; | ||
import { getCurrentWeekdayAbbreviation } from '$lib/dates/dataWeekday'; | ||
export let assignment: Assignment; | ||
export let allAssignments: Assignment[]; | ||
let dueWeekday = nextWeekdayForSubject(getCurrentWeekdayAbbreviation(), assignment.subject); | ||
$: { | ||
dueWeekday = nextWeekdayForSubject(getCurrentWeekdayAbbreviation(), assignment.subject); | ||
assignment.due = nextCustomDateForWeekday(dueWeekday); | ||
} | ||
</script> | ||
|
||
<div class="flex flex-col justify-evenly items-center"> | ||
<QuickActionButton | ||
iconName="bx-up-arrow" | ||
focusedIconName="bxs-up-arrow" | ||
on:click={() => { | ||
const index = allAssignments.indexOf(assignment); | ||
const newIndex = index - 1; | ||
|
||
allAssignments = swapArrayElements(allAssignments, index, newIndex); | ||
}} | ||
disabled={allAssignments.indexOf(assignment) === 0} | ||
/> | ||
<QuickActionButton | ||
iconName="bx-down-arrow" | ||
focusedIconName="bxs-down-arrow" | ||
on:click={() => { | ||
const index = allAssignments.indexOf(assignment); | ||
const newIndex = index + 1; | ||
|
||
allAssignments = swapArrayElements(allAssignments, index, newIndex); | ||
}} | ||
disabled={allAssignments.indexOf(assignment) === allAssignments.length - 1} | ||
/> | ||
</div> | ||
<div class="w-full"> | ||
<span class="flex flex-row items-center justify-start gap-2 my-2"> | ||
<div class="flex flex-row w-full gap-2"> | ||
<I18n> | ||
<input | ||
type="text" | ||
bind:value={assignment.subject} | ||
placeholder={i('homework.add.subject')} | ||
class="w-full outline-1 outline-gray-400 outline rounded-sm p-1" | ||
list="subjects" | ||
/> | ||
</I18n> | ||
<datalist id="subjects"> | ||
{#each subjectsSortetCapitalized as subj} | ||
<option value={subj} /> | ||
{/each} | ||
</datalist> | ||
<span class="min-w-max outline-1 outline-gray-400 outline rounded-sm p-1"> | ||
<DatePicker bind:dateObj={assignment.due} /> | ||
</span> | ||
</div> | ||
</span> | ||
<I18n> | ||
<textarea | ||
bind:value={assignment.description} | ||
placeholder={i('homework.add.description')} | ||
class="w-full outline-1 outline-gray-400 outline rounded-sm p-1" | ||
rows="2" | ||
/> | ||
</I18n> | ||
</div> | ||
|
||
<style> | ||
input, | ||
textarea { | ||
color: var(--text); | ||
background-color: transparent; | ||
} | ||
:is(input, textarea):focus-visible { | ||
outline: 2px solid var(--accent); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createDate } from '$lib/dates/createDateObject'; | ||
import { getCurrentWeekdayAbbreviation } from '$lib/dates/dataWeekday'; | ||
import { mapValues } from '$lib/helpers/mapValues'; | ||
import { emptyTimeTable, type WeekDay } from '../../constants/weekDays'; | ||
import { timetable } from '../../stores'; | ||
|
||
let times = emptyTimeTable; | ||
|
||
timetable.subscribe((value) => { | ||
times = value; | ||
}); | ||
|
||
export const nextWeekdayForSubject = (day: WeekDay, subject: string) => { | ||
const mapped = mapValues(times, (localSubj) => localSubj.includes(subject)); | ||
const today = new Date().getDay(); | ||
const weekLength = Object.values(times).length; | ||
|
||
for (let i = today; i < today + weekLength; i++) { | ||
const dayIndex = i % weekLength; | ||
const dayName = Object.keys(mapped)[dayIndex] as WeekDay; | ||
|
||
if (mapped[dayName]) { | ||
return dayName; | ||
} | ||
} | ||
|
||
return day; | ||
}; | ||
|
||
export const nextCustomDateForWeekday = (day: WeekDay) => { | ||
const weekdays = Object.keys(times); | ||
|
||
const todayIndex = weekdays.indexOf(getCurrentWeekdayAbbreviation()); | ||
const dayIndex = weekdays.indexOf(day); | ||
|
||
const daysToNext = dayIndex - todayIndex; | ||
|
||
const currently = new Date(); | ||
currently.setDate(currently.getDate() + daysToNext); | ||
|
||
if (currently <= new Date()) { | ||
currently.setDate(currently.getDate() + 7); | ||
} | ||
|
||
return createDate(currently); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters