Loading UI and Progressive enhancement #7041
Answered
by
david-plugge
exseniorastronaut
asked this question in
Q&A
-
|
I have trouble understanding how to correctly do loading UI with Progressive enhancement forms. One way I thought is like so But that is a lot of code. I believe there should be a nicer way to see if a form is submitting data. Maybe there is already a better way that I am just not aware. |
Beta Was this translation helpful? Give feedback.
Answered by
david-plugge
Sep 27, 2022
Replies: 1 comment 1 reply
-
|
You can create your own enhance method (i copied the code from sveltekits enhance method): import { applyAction, enhance } from '$app/forms';
import { invalidateAll } from '$app/navigation';
import { writable } from 'svelte/store';
const { set, subscribe } = writable(false);
export const formLoading = { subscribe };
export function enhanceWithLoad(form: HTMLFormElement) {
return enhance(form, () => {
set(true);
return async ({ result, action }) => {
if (result.type === 'success') {
await invalidateAll();
}
// For success/invalid results, only apply action if it belongs to the
// current page, otherwise `form` will be updated erroneously
if (
location.origin + location.pathname === action.origin + action.pathname ||
result.type === 'redirect' ||
result.type === 'error'
) {
applyAction(result);
}
set(false);
};
});
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
exseniorastronaut
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can create your own enhance method (i copied the code from sveltekits enhance method):