Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Implement optimized date suggestions based on the timetable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Jan 18, 2024
1 parent 18b17f0 commit 5aa1be5
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/constants/weekDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const emptyTimeTable = {
fr: [],
sa: [],
su: []
} satisfies TimeTable;
} as TimeTable;

export type WeekDay = (typeof weekdays)[number];

Expand Down
23 changes: 23 additions & 0 deletions src/lib/dates/dataWeekday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
15 changes: 15 additions & 0 deletions src/lib/helpers/mapValues.ts
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;
};
87 changes: 87 additions & 0 deletions src/lib/homework/CreateHomeworkAssignment.svelte
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>
74 changes: 2 additions & 72 deletions src/lib/homework/CreateHomeworkInner.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<script lang="ts">
import QuickActionButton from '$lib/QuickActionButton.svelte';
import DatePicker from '$lib/dates/DatePicker.svelte';
import { createDate } from '$lib/dates/createDateObject';
import { swapArrayElements } from '$lib/utils/SwapItems';
import type { Assignment } from '../../types/homework';
import SubmitButton from '$lib/SubmitButton.svelte';
import I18n from '$lib/I18n.svelte';
import { i } from '../../languages/i18n';
import { subjectsSortetCapitalized } from '../../constants/subjecticons';
import { getOneWeekFromNow } from '../../constants/generateDates';
import CreateHomeworkAssignment from './CreateHomeworkAssignment.svelte';
let date = createDate(getOneWeekFromNow());
Expand All @@ -24,61 +19,7 @@
<ul class="list-none">
{#each assignments as assignment}
<li class="flex flex-row">
<div class="flex flex-col justify-evenly items-center">
<QuickActionButton
iconName="bx-up-arrow"
focusedIconName="bxs-up-arrow"
on:click={() => {
const index = assignments.indexOf(assignment);
const newIndex = index - 1;

assignments = swapArrayElements(assignments, index, newIndex);
}}
disabled={assignments.indexOf(assignment) === 0}
/>
<QuickActionButton
iconName="bx-down-arrow"
focusedIconName="bxs-down-arrow"
on:click={() => {
const index = assignments.indexOf(assignment);
const newIndex = index + 1;

assignments = swapArrayElements(assignments, index, newIndex);
}}
disabled={assignments.indexOf(assignment) === assignments.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>
<CreateHomeworkAssignment bind:assignment bind:allAssignments={assignments} />
</li>
{/each}
</ul>
Expand All @@ -103,14 +44,3 @@
}}
/>
</div>

<style>
input,
textarea {
color: var(--text);
background-color: transparent;
}
:is(input, textarea):focus-visible {
outline: 2px solid var(--accent);
}
</style>
46 changes: 46 additions & 0 deletions src/lib/timetable/index.ts
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);
};
4 changes: 2 additions & 2 deletions src/routes/settings/timetable/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@
<input
type="text"
value={lesson}
on:input={(e) => {
on:blur={(e) => {
$timetable[weekdays[j].abbr][i] = e.currentTarget.value;
$timetable = $timetable;
sanitizeTimetable();
}}
on:blur={sanitizeTimetable}
class="rounded-sm text-light-text dark:text-light-text px-2 py-0.5 bg-gray-200"
/>
<QuickActionButton
Expand Down

0 comments on commit 5aa1be5

Please sign in to comment.