From 7874f229a02963ec9de11ed922b3ec048600eaf9 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Sun, 4 Aug 2024 11:58:02 +0200 Subject: [PATCH] refactor; sanity check before saving new order to localstorage --- src/lib/TSHelpers/ComponentOrder.ts | 28 +++++++++++++++++++ .../TilesAndModals/DashReorderModal.svelte | 28 +++++++++++-------- 2 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/lib/TSHelpers/ComponentOrder.ts diff --git a/src/lib/TSHelpers/ComponentOrder.ts b/src/lib/TSHelpers/ComponentOrder.ts new file mode 100644 index 0000000..2a67f98 --- /dev/null +++ b/src/lib/TSHelpers/ComponentOrder.ts @@ -0,0 +1,28 @@ +export const components: string[] = [ + 'BasicInfoTile', + 'GradesTile', + 'MensaTile', + 'CalendarTile', + 'ExamSignupTile' +]; + +export const tileNames: Map = 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; + } +} diff --git a/src/lib/TilesAndModals/DashReorderModal.svelte b/src/lib/TilesAndModals/DashReorderModal.svelte index f6729f6..077e934 100644 --- a/src/lib/TilesAndModals/DashReorderModal.svelte +++ b/src/lib/TilesAndModals/DashReorderModal.svelte @@ -7,31 +7,37 @@ export let parent: SvelteComponent; export let componentOrder: Writable; - const tileNames: Map = 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'; -
+
{#each items as item (item.id)}
{/each} -
+