Skip to content

Commit

Permalink
Implement default subjects for courses
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Jun 27, 2024
1 parent 3d6099e commit 3619775
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
import QuickAction from '$lib/components/buttons/QuickAction.svelte';
import NotTyppable from '$lib/components/select/NotTyppable.svelte';
import { self } from '$lib/utils/utils';
import { mapObject } from '$lib/utils/objects/map';
const timetable = svocal('settings.timetable');
const presets = svocal('settings.homeworkPresets');
const defaultSubjects = svocal('settings.homework.defaultSubject');
const dispatch = createEventDispatcher<{ submit: CreationPayload }>();
Expand All @@ -60,7 +62,11 @@
const initiallySelected = allowedClasses.filter(showClass);
let classes: Class[] = [];
let classInput = initiallySelected.length === 1 ? initiallySelected[0] : '';
const classInput = writable(initiallySelected.length === 1 ? initiallySelected[0] : '');
$: {
const initiallySelected = selectableClasses.filter(showClass);
classInput.set(initiallySelected.length === 1 ? initiallySelected[0] : '');
}
let subject = '';
let description = '';
Expand All @@ -71,7 +77,7 @@
let inputMode: 'text' | 'presets' = 'text';
$: disabled = !(
classInput &&
$classInput &&
subject &&
description &&
customDateToNormal(due) > customDateToNormal(from)
Expand All @@ -90,6 +96,11 @@
due = normalToCustomDate(date);
}
classInput.subscribe((cl) => {
const lowercased = mapObject($defaultSubjects, (key) => key.toLowerCase(), self);
subject = lowercased[cl.toLowerCase()] || subject;
});
wasSuccessfull.subscribe((successfull) => {
if (successfull) {
subject = '';
Expand All @@ -115,8 +126,8 @@
label: readable(name),
value: name
}))}
value={[classInput]}
bind:firstValue={classInput}
value={[$classInput]}
bind:firstValue={$classInput}
threshold={0.1}
/>
{/if}
Expand Down Expand Up @@ -175,7 +186,7 @@
{disabled}
on:click={() => {
disabled = true;
dispatch('submit', { description, subject, due, from, school, class: classInput });
dispatch('submit', { description, subject, due, from, school, class: $classInput });
}}
>
<Store store={i('assignments.create.create')} />
Expand Down
7 changes: 6 additions & 1 deletion src/lib/constants/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Readable } from 'svelte/store';
import { Calendar, Cog, PaintBrush, User, type IconSource } from 'svelte-hero-icons';
import { Calendar, Cog, PaintBrush, User, BookOpen, type IconSource } from 'svelte-hero-icons';
import { i } from '$lib/i18n/store';

type Setting =
Expand Down Expand Up @@ -31,6 +31,11 @@ export const settings: Setting[] = [
uri: '/color',
label: i('settings.color'),
icon: PaintBrush
},
{
uri: '/homework',
label: i('settings.assignments'),
icon: BookOpen
}
];

Expand Down
12 changes: 9 additions & 3 deletions src/lib/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,6 @@ const de = {
'Ziehe ein Rechteck hier her, um es zu aus der Navigation zu entfernen.',
'settings.general.calendar': 'Kalender',
'settings.general.calendar.weekStartsOn': 'Erster Tag der Woche',
'settings.general.assignments': 'Hausaufgaben',
'settings.general.assignments.transparency': 'Transparenz überfälliger Hausaufgaben',
'settings.general.assignments.presets': 'Voreinstellungen für neue Hausaufgaben',
'settings.general.launcher': 'Suche',
'settings.general.launcher.outlineWidth': 'Breite der grünen Umrandung',
'settings.general.launcher.width': 'Breite',
Expand Down Expand Up @@ -526,6 +523,15 @@ const de = {
'settings.api.success': 'Erfolgreich gespeichert',
'settings.api.error': 'Hierbei handelt es sich nicht um einen Dlool Server',

'settings.assignments': 'Hausaufgaben',
'settings.assignments.title': 'Hausaufgaben Einstellungen',
'settings.assignments.transparency': 'Transparenz überfälliger Hausaufgaben',
'settings.assignments.presets': 'Voreinstellungen für neue Hausaufgaben',
'settings.assignments.presets.new': 'Neues hinzufügen',
'settings.assignments.default': 'Standard Fächer für Kurse',
'settings.assignments.default.class.placeholder': 'Klasse/Kurs',
'settings.assignments.default.class.subject': 'Fach',

'settings.save': 'Speichern',

'holiday.noData': 'Es gibt keine Daten für diesen Monat',
Expand Down
12 changes: 9 additions & 3 deletions src/lib/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,6 @@ const en = {
'Drag and drop one rectangle here to remove it from your navigation.',
'settings.general.calendar': 'Calendar',
'settings.general.calendar.weekStartsOn': 'First day of the week',
'settings.general.assignments': 'Assignments',
'settings.general.assignments.transparency': 'transparency of overdue assignments',
'settings.general.assignments.presets': 'Presets for new assignments',
'settings.general.launcher': 'Launcher',
'settings.general.launcher.outlineWidth': 'Width of the green outline',
'settings.general.launcher.width': 'Width',
Expand Down Expand Up @@ -513,6 +510,15 @@ const en = {
'settings.api.success': 'Successfully saved',
'settings.api.error': "This isn't a Dlool-server",

'settings.assignments': 'Assignments',
'settings.assignments.title': 'Assignment-Settings',
'settings.assignments.transparency': 'transparency of overdue assignments',
'settings.assignments.presets': 'Presets for new assignments',
'settings.assignments.presets.new': 'Add a new one',
'settings.assignments.default': 'Default subjects for courses',
'settings.assignments.default.class.placeholder': 'Course',
'settings.assignments.default.class.subject': 'Subject',

'settings.save': 'Save',

'holiday.noData': 'There is not data for this month',
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils/store/svocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const sv = {
'holidays.state': ['holidays.state', () => 'HE'],
'holidays.autoDetect': ['holidays.autoDetect', () => true],
'settings.homeworkPresets': ['settings.homeworkPresets', () => [] as string[]],
'settings.homework.defaultSubject': [
'settings.homework.defaultSubject',
() => ({}) as Record<string, string>
],
'dlool-version': ['dlool-version', () => '2']
} as const;

Expand Down
44 changes: 0 additions & 44 deletions src/routes/settings/general/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import TextInput from '$lib/components/input/Text.svelte';
import BoolSetting from '$lib/components/settings/BoolSetting.svelte';
import { i } from '$lib/i18n/store';
import { svocal } from '$lib/utils/store/svocal';
Expand All @@ -12,18 +11,13 @@
import { currentLang } from '$lib/stores';
import { Holiday } from 'open-holiday-js';
import NavbarCustomizor from '$lib/components/settings/navigation/NavbarCustomizor.svelte';
import QuickAction from '$lib/components/buttons/QuickAction.svelte';
import { Plus, Trash } from 'svelte-hero-icons';
import { removeNthElement } from '$lib/utils/arrays/remove';
import SettingsButton from '$lib/components/buttons/SettingsButton.svelte';
import { onDestroy } from 'svelte';
import { self } from '$lib/utils/utils';
import { browser } from '$app/environment';
import Api from '$lib/components/settings/Api.svelte';
const navTexts = svocal('settings.nav.texts');
const weekStartsOn = svocal('settings.weekStartsOn');
const homeworkTransparency = svocal('settings.homework.transparency');
const homeworkPresets = svocal('settings.homeworkPresets');
const launcherOutlineWidth = svocal('settings.launcher.outlineWidth');
const launcherWidth = svocal('settings.launcher.width');
Expand Down Expand Up @@ -54,44 +48,6 @@
<BoolSetting label={i('settings.general.nav.texts')} bind:value={$navTexts} />
</section>

<section class="flex flex-col gap-2">
<h3><Store store={i('settings.general.assignments')} /></h3>

<RangeSettings
label={i('settings.general.assignments.transparency')}
bind:value={$homeworkTransparency}
min={0}
max={1}
step={0.05}
valueFmt={(num) => `${Math.round(num * 100)}%`}
/>

<div class="flex justify-between">
<span><Store store={i('settings.general.assignments.presets')} /></span>

<div class="flex flex-col gap-2">
{#each $homeworkPresets as preset, ind}
<span class="flex gap-1">
<TextInput placeholder={readable('')} bind:value={preset} />
<QuickAction
icon={Trash}
on:click={() => homeworkPresets.update((old) => removeNthElement(old, ind))}
/>
</span>
{/each}

<SettingsButton
icon={Plus}
color="green"
disabled={$homeworkPresets.map((x) => x.trim()).includes('')}
on:click={() => homeworkPresets.update((old) => ['', ...old])}
>
<span> Neues hinzufügen </span>
</SettingsButton>
</div>
</div>
</section>

<section class="flex flex-col gap-2">
<h3><Store store={i('settings.general.calendar')} /></h3>

Expand Down
117 changes: 117 additions & 0 deletions src/routes/settings/homework/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<script lang="ts">
import TextInput from '$lib/components/input/Text.svelte';
import { i } from '$lib/i18n/store';
import { svocal } from '$lib/utils/store/svocal';
import MetaData from '$lib/components/utils/MetaData.svelte';
import RangeSettings from '$lib/components/settings/RangeSettings.svelte';
import Store from '$lib/components/utils/Store.svelte';
import { readable } from 'svelte/store';
import QuickAction from '$lib/components/buttons/QuickAction.svelte';
import { AcademicCap, Plus, Trash } from 'svelte-hero-icons';
import { removeNthElement } from '$lib/utils/arrays/remove';
import SettingsButton from '$lib/components/buttons/SettingsButton.svelte';
import { onDestroy } from 'svelte';
import { self } from '$lib/utils/utils';
import { browser } from '$app/environment';
import { objectEntries } from '$lib/utils/objects/entries';
import { replaceKey } from '$lib/utils/objects/replaceKey';
import { removeKey } from '$lib/utils/objects/removeKey';
const homeworkTransparency = svocal('settings.homework.transparency');
const homeworkPresets = svocal('settings.homeworkPresets');
const defaultSubjects = svocal('settings.homework.defaultSubject');
onDestroy(() => {
if (!browser) return;
homeworkPresets.update((old) => old.map((x) => x.trim()).filter(self));
});
</script>

<MetaData title={i('settings.assignments.title')} />

<section class="flex flex-col gap-2">
<h3><Store store={i('settings.assignments')} /></h3>

<RangeSettings
label={i('settings.assignments.transparency')}
bind:value={$homeworkTransparency}
min={0}
max={1}
step={0.05}
valueFmt={(num) => `${Math.round(num * 100)}%`}
/>

<div class="flex justify-between">
<span><Store store={i('settings.assignments.presets')} /></span>

<div class="flex flex-col gap-2">
{#each $homeworkPresets as preset, ind}
<span class="flex gap-1">
<TextInput placeholder={readable('')} bind:value={preset} />
<QuickAction
icon={Trash}
on:click={() => homeworkPresets.update((old) => removeNthElement(old, ind))}
/>
</span>
{/each}

<SettingsButton
icon={Plus}
color="green"
disabled={$homeworkPresets.map((x) => x.trim()).includes('')}
on:click={() => homeworkPresets.update((old) => ['', ...old])}
>
<span><Store store={i('settings.assignments.presets.new')} /></span>
</SettingsButton>
</div>
</div>

<div>
<h3><Store store={i('settings.assignments.default')} /></h3>
<div class="flex flex-col gap-2">
{#each objectEntries($defaultSubjects) as [className, subject]}
<div class="wrap flex items-center justify-between gap-2">
<span class="flex items-center gap-2">
<QuickAction
small
icon={Trash}
on:click={() => {
defaultSubjects.update((obj) => removeKey(obj, className));
}}
/>
<TextInput
placeholder={i('settings.assignments.default.class.placeholder')}
icon={AcademicCap}
value={className}
on:input={({ detail }) => {
defaultSubjects.update((obj) => replaceKey(obj, className, detail));
}}
/>
</span>

<span>
<TextInput
placeholder={i('settings.assignments.default.class.subject')}
value={subject}
on:input={({ detail }) => {
defaultSubjects.update((old) => {
old[className] = detail;
return old;
});
}}
/>
</span>
</div>
{/each}

<div>
<QuickAction
icon={Plus}
on:click={() => {
defaultSubjects.update((old) => ({ ...old, '': '' }));
}}
/>
</div>
</div>
</div>
</section>

0 comments on commit 3619775

Please sign in to comment.