From df29f3e28f2b07d01e1a46674bb60f8197dee1a9 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:57:47 +0000 Subject: [PATCH] feat: custom input helper --- src/lib/components/eventModal.svelte | 79 +++++++++++++++++++++------- src/lib/helpers/array.ts | 34 ++++++++++++ 2 files changed, 94 insertions(+), 19 deletions(-) diff --git a/src/lib/components/eventModal.svelte b/src/lib/components/eventModal.svelte index b91ba051c..3686b8fad 100644 --- a/src/lib/components/eventModal.svelte +++ b/src/lib/components/eventModal.svelte @@ -3,7 +3,7 @@ import { Modal } from '$lib/components'; import { Pill } from '$lib/elements'; import { createEventDispatcher } from 'svelte'; - import { empty } from '$lib/helpers/array'; + import { at, empty, last } from '$lib/helpers/array'; import Copy from './copy.svelte'; // Props @@ -154,6 +154,56 @@ } } + function resetSelected() { + Object.keys(selected).forEach((key) => (selected[key] = null)); + } + + function toggleShowInput(e?: Event) { + e?.preventDefault(); + + showInput = !showInput; + if (showInput) return; + + helper = null; + + resetSelected(); + + for (const field of customInput.split('.')) { + if (isService(field)) { + selected.service = services.find((s) => s.name === field); + } else if (isResource(field)) { + const resource = selected.service?.resources.find((r) => r.name === field); + selected.resource = resource; + } else if (isAction(field)) { + const action = + selected.resource?.actions.find((a) => a.name === field) || + selected.service?.actions.find((a) => a.name === field); + selected.action = action; + } else if (isAttribute(field)) { + const attribute = selected.action?.attributes?.find((a) => a === field); + selected.attribute = attribute; + } + } + } + + function getHelperStr(input: string): string { + const fields = input.split('.'); + const secondToLastField = at(fields, -3); + const prevField = at(fields, -2); + const currField = last(fields); + + if (fields.length === 1) return 'service'; + if (isAttribute(currField) || isAction(prevField)) return 'attribute'; + if (isAction(currField)) return 'action'; + if (isResource(currField)) return 'resource'; + if (isService(currField)) return 'service'; + if (isService(prevField) || isResource(prevField)) return `ID of ${prevField}`; + if (isService(secondToLastField)) return 'resource or action'; + if (isResource(secondToLastField)) return 'action'; + + return ''; + } + // Event string helpers const serviceNames = services.map((s) => s.name); const isService = (s: string) => serviceNames.includes(s); @@ -246,16 +296,16 @@ $: inputValue = eventString.map((d) => d.value).join('.'); - $: if (!showInput) { - helper = null; - } - $: if (!show) { - Object.keys(selected).forEach((key) => (selected[key] = null)); + resetSelected(); helper = null; customInput = null; showInput = false; } + + $: if (showInput) { + helper = getHelperStr(customInput); + } @@ -331,23 +381,14 @@
- -
+

{helper ?? ''}

{:else}
@@ -368,7 +409,7 @@