-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
113 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<script lang="ts"> | ||
import { applyAction } from '$app/forms'; | ||
import { invalidateAll } from '$app/navigation'; | ||
import { enhance } from '$app/forms'; | ||
import toast from 'svelte-french-toast'; | ||
import { ActionResult } from '@sveltejs/kit'; | ||
import { loading } from '$state/loading'; | ||
let formLoading = false; | ||
export let global = true; // By default, the global loader UI is used | ||
type FormActionMessage = { | ||
message?: string; | ||
}; | ||
export const form_action = ( | ||
opts?: FormActionMessage, | ||
callback?: (data: any | unknown) => any | ||
) => { | ||
return function form_enhance() { | ||
if (global) { | ||
loading.setLoading(true); | ||
} | ||
formLoading = true; | ||
return async ({ result }: { result: ActionResult<any, any> }) => { | ||
if (result.type === 'success') { | ||
toast.success('Siiiiick ' + result.data.message + ' was a success'); | ||
} else if (result.type === 'error') { | ||
console.log(result); | ||
toast.error(`Major bummer: ${result.error.message}`); | ||
} else { | ||
toast.error(`Something went wrong. Check the console`); | ||
console.log(result); | ||
} | ||
await invalidateAll(); | ||
await applyAction(result); | ||
formLoading = false; | ||
if (global) { | ||
loading.setLoading(false); | ||
} | ||
if (callback && 'data' in result && result?.data) callback(result.data); | ||
}; | ||
}; | ||
}; | ||
</script> | ||
|
||
<form {...$$restProps} use:enhance={form_action()}> | ||
<slot loading={formLoading} /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters