Skip to content

Commit

Permalink
refactor; sanity check before saving new order to localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
fnschmidt committed Aug 4, 2024
1 parent be6893b commit 7874f22
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
28 changes: 28 additions & 0 deletions src/lib/TSHelpers/ComponentOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const components: string[] = [
'BasicInfoTile',
'GradesTile',
'MensaTile',
'CalendarTile',
'ExamSignupTile'
];

export const tileNames: Map<string, string> = new Map([
['BasicInfoTile', 'Deine Infos'],
['GradesTile', 'Noten'],
['ExamSignupTile', 'Prüfungen'],
['MensaTile', 'Mensa'],
['CalendarTile', 'Kalender']
]);

export function validateComponentOrder(order: string[]): boolean {
if (
order.length == components.length &&
components.every((e) => {
return order.includes(e);
})
) {
return true;
} else {
return false;
}
}
28 changes: 17 additions & 11 deletions src/lib/TilesAndModals/DashReorderModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,37 @@
export let parent: SvelteComponent;
export let componentOrder: Writable<string[]>;
const tileNames: Map<string, string> = new Map([
['BasicInfoTile', 'Deine Infos'],
['GradesTile', 'Noten'],
['ExamSignupTile', 'Prüfungen'],
['MensaTile', 'Mensa'],
['CalendarTile', 'Kalender']
]);
let items = $componentOrder.map((id) => ({ id, title: tileNames.get(id) }));
let dropTargetStyle = {};
function handleSort(e: CustomEvent) {
items = e.detail.items;
}
function finalize(e: CustomEvent) {
handleSort(e);
componentOrder.set(items.map((item) => item.id));
let newSort = items.map((item) => item.id);
if (!validateComponentOrder(newSort)) {
window.alert('Reorder failed');
} else {
componentOrder.set(newSort);
}
}
import { dndzone } from 'svelte-dnd-action';
import { flip } from 'svelte/animate';
import { tileNames, validateComponentOrder } from '$lib/TSHelpers/ComponentOrder';
</script>

<DashboardModal bind:parent title="Anordnung ändern">
<div class="space-y-2" use:dndzone={{ items }} on:consider={handleSort} on:finalize={finalize}>
<section
class="space-y-2"
use:dndzone={{ items, dropTargetStyle }}
on:consider={handleSort}
on:finalize={finalize}
>
{#each items as item (item.id)}
<div
animate:flip={{ duration: 200 }}
Expand All @@ -41,5 +47,5 @@
<i class="fa-solid fa-bars"></i>
</div>
{/each}
</div>
</section>
</DashboardModal>

0 comments on commit 7874f22

Please sign in to comment.