diff --git a/frontend/src/lib/components/Forms/AutocompleteSelect.svelte b/frontend/src/lib/components/Forms/AutocompleteSelect.svelte index 8c100fb09..9c4f4e2d2 100644 --- a/frontend/src/lib/components/Forms/AutocompleteSelect.svelte +++ b/frontend/src/lib/components/Forms/AutocompleteSelect.svelte @@ -2,18 +2,23 @@ import { formFieldProxy } from 'sveltekit-superforms/client'; import { localItems, toCamelCase } from '$lib/utils/locales'; import { languageTag } from '$paraglide/runtime'; + import { onMount } from 'svelte'; + import { formSubmittedStore } from '$lib/utils/stores'; export let label: string | undefined = undefined; export let field: string; export let helpText: string | undefined = undefined; export let form; + export let origin: string; + export let URLModel: string; export let multiple = false; export let nullable = false; export let hide = false; const { value, errors, constraints } = formFieldProxy(form, field); + const dataSaving = origin === "create"; export let options: { label: string; value: string; suggested?: boolean }[]; @@ -28,6 +33,32 @@ $: selectedValues = selected.map((item) => item.value); + let _sessionStorage = null; + onMount(() => { + if (!dataSaving) return; + _sessionStorage = sessionStorage; + const savedData = JSON.parse(_sessionStorage.getItem("create_form_saved_data") ?? "{}"); + const currentData = savedData[URLModel]; + if (currentData) { + const savedValue = currentData[field]; + if (savedValue) { + selected = savedValue; + } + } + }); + + $: if (dataSaving && _sessionStorage && !$formSubmittedStore) { + const savedData = JSON.parse(_sessionStorage.getItem("create_form_saved_data") ?? "{}"); + + const currentData = savedData[URLModel] ?? {}; + if (!sessionStorage.hasOwnProperty(URLModel)) { + currentData[field] = selected; + } + savedData[URLModel] = currentData; + + _sessionStorage.setItem("create_form_saved_data",JSON.stringify(savedData)); + } + const default_value = nullable ? null : selectedValues[0]; $: ($value = multiple ? selectedValues : selectedValues[0] ?? default_value), handleSelectChange(); diff --git a/frontend/src/lib/components/Forms/Form.svelte b/frontend/src/lib/components/Forms/Form.svelte index 789fbaa40..827427a33 100644 --- a/frontend/src/lib/components/Forms/Form.svelte +++ b/frontend/src/lib/components/Forms/Form.svelte @@ -4,6 +4,8 @@ import SuperDebug from 'sveltekit-superforms/client/SuperDebug.svelte'; import type { AnyZodObject } from 'zod'; import { focusTrap } from '@skeletonlabs/skeleton'; + import { onMount } from 'svelte'; + import { formSubmittedStore } from '$lib/utils/stores'; import type { ModalStore } from '@skeletonlabs/skeleton'; import { getModalStore } from '@skeletonlabs/skeleton'; @@ -14,12 +16,15 @@ export let data: SuperValidated; export let dataType: 'form' | 'json'; + export let origin: String; + export let URLModel: string; export let invalidateAll = true; // set to false to keep form data using muliple forms on a page export let validators: AnyZodObject; export let applyAction = true; export let resetForm = false; export let onSubmit = (submit_data: any) => {}; export let taintedMessage: string | null = m.taintedFormMessage(); + const dataSaving = origin === "create"; export let debug = false; // set to true to enable SuperDebug component @@ -41,6 +46,13 @@ }); const { form, message /*, tainted*/, delayed, errors, allErrors, enhance } = _form; + + let _sessionStorage = null; + onMount(() => { + _sessionStorage = sessionStorage; + }) + + $: if (URLModel) { formSubmittedStore.set(false); } {#if debug} @@ -48,7 +60,14 @@ {/if} -
+ { + if (dataSaving) { + formSubmittedStore.set(true); + const savedData = JSON.parse(_sessionStorage?.getItem("create_form_saved_data") ?? "{}"); + savedData[URLModel] = {}; + _sessionStorage?.setItem("create_form_saved_data",JSON.stringify(savedData)); + } +}} use:enhance use:focusTrap={true} {...$$restProps}> {#if $errors._errors} {#each $errors._errors as error}

{error}

diff --git a/frontend/src/lib/components/Forms/ModelForm.svelte b/frontend/src/lib/components/Forms/ModelForm.svelte index 39c325b04..d2113fcbb 100644 --- a/frontend/src/lib/components/Forms/ModelForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm.svelte @@ -55,6 +55,8 @@ dataType={shape.attachment ? 'form' : 'json'} enctype={shape.attachment ? 'multipart/form-data' : 'application/x-www-form-urlencoded'} data={form} + {origin} + {URLModel} let:form let:data let:initialData @@ -97,22 +99,22 @@ /> {/if} {#if shape.name} - + {/if} {#if shape.description} -