Skip to content

Commit 3ac6b69

Browse files
committed
add on settled hook
1 parent ff5094c commit 3ac6b69

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ Runs if the form is submitted and returns sucessfully. You will have access to t
277277

278278
---
279279

280+
#### `options.onSettled()`
281+
282+
Runs after the form submission finishes, whether it succeeds or fails. Receives the returned value from `command` on success, or `null` on failure. NOTE: This runs AFTER any invalidation you may have called.
283+
284+
```html
285+
<script lang="ts">
286+
const cmd = new CommandForm(schema, {
287+
onSettled: (result) => {
288+
toast.dismiss(); // stop any loading toasts regardless of outcome
289+
console.log('Finished submitting', result);
290+
}
291+
// ... other options
292+
});
293+
</script>
294+
```
295+
296+
---
297+
280298
#### `options.onError()`
281299

282300
Runs if the command fails and an error is returned.

src/lib/command-form/command-form.svelte.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export class CommandForm<Schema extends StandardSchemaV1, TOut> {
159159
} finally {
160160
this._submitting = false;
161161
if (this.options.reset === 'always') this.reset();
162+
this.options.onSettled?.(this._result || null)
162163
}
163164
};
164165

src/lib/types/command-form.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type CommandFormOptions<TIn, TOut> = {
1414
preprocess?: (data: TIn) => Promise<TIn> | TIn;
1515
onSubmit?: (data: TIn) => Promise<void> | void;
1616
onSuccess?: (result: Awaited<TOut>) => Promise<void> | void;
17+
onSettled?: (result: Awaited<TOut> | null) => Promise<void> | void;
1718
onError?: (err: unknown) => Promise<void> | void;
1819
};
1920

src/routes/+page.server.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const load = async ({ depends }) => {
2+
depends('invalidate:test');
3+
let total = 1
4+
await new Promise((r) => setTimeout(r, 500));
5+
const options = Array.from({ length: Math.floor(Math.random() * 15) + total }, (_, i) => ({ id: i.toString(), value: `Option ${i + 1}` }))
6+
total++
7+
return {
8+
options
9+
}
10+
}

src/routes/+page.svelte

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22
import { CommandForm } from '$lib/command-form/command-form.svelte.js';
33
import { test } from './test.remote.ts';
44
import { schema, TestEnum } from './test.schema.ts';
5+
let { data } = $props();
6+
7+
let options = $derived(data.options);
58
69
const f = new CommandForm(schema, {
710
command: test,
11+
initial: () => {
12+
return {
13+
name: 'Cheese',
14+
options: options
15+
};
16+
},
817
onSubmit: async (data) => {
918
console.log(data);
1019
},
1120
onSuccess: async (res) => {
1221
console.log('success');
13-
// console.log(res.success);
22+
},
23+
onSettled: async (res) => {
24+
f.reset();
1425
},
1526
onError: async (err) => {
1627
console.error('error', err);
1728
},
18-
preprocess: (data) => {
19-
return { ...data, name: 'preprocessed' };
20-
}
29+
invalidate: 'invalidate:test'
2130
});
2231
</script>
2332

@@ -28,3 +37,7 @@
2837
{#if f.errors.name?.message}
2938
<p style="color: red">{f.errors.name.message}</p>
3039
{/if}
40+
41+
{#each f.form.options as option (option.id)}
42+
{option.value}
43+
{/each}

src/routes/test.remote.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { command } from "$app/server";
22
import { schema } from "./test.schema.ts";
3-
import { SchemaValidationError } from "$lib/index.ts";
4-
53

64
export const test = command(schema, async (data) => {
75
console.log(data)
8-
throw new SchemaValidationError([{ path: ['name'], message: 'Name is invalid server error!!' }])
6+
return { success: true };
97
})

src/routes/test.schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export enum TestEnum {
44
}
55
const schema = z.object({
66
name: z.string().min(2, "Name is required"),
7+
options: z.array(z.object({
8+
id: z.string(),
9+
value: z.string()
10+
})),
711
// age: z.number().min(0, "Age must be a positive number"),
812
// address: z.object({
913
// street: z.string().min(5, "Street is required"),

0 commit comments

Comments
 (0)