Skip to content

Commit 55b5e0e

Browse files
committed
[docs] Update useMutation demo
1 parent 061f408 commit 55b5e0e

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
import { ShadowHost } from "@/components/shadow";
3+
4+
import Form from "./_generic-backend.svelte";
5+
---
6+
7+
<ShadowHost client:only="svelte">
8+
<Form client:only="svelte" />
9+
</ShadowHost>

apps/docs/src/content/docs/integrations/_generic-backend.svelte

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,68 @@
44
55
import { useCustomForm } from "@/components/custom-form";
66
7-
let isError = $state(false);
8-
let duration = $state(0);
97
let data = $state<string>();
108
9+
interface Config {
10+
reject: boolean;
11+
delay: number;
12+
value: string;
13+
}
14+
1115
const mutation = useMutation({
12-
mutate: (_signal, value: string | undefined = "") =>
16+
mutate: (_signal, { reject: isError, delay, value }: Config) =>
1317
new Promise<string>((resolve, reject) => {
1418
data = undefined;
15-
isError = Math.random() > 0.5;
16-
duration = Math.random() * 5000;
1719
setTimeout(() => {
1820
if (isError) {
1921
reject(value);
2022
} else {
2123
resolve(value);
2224
}
23-
}, duration);
25+
}, delay);
2426
}),
2527
onSuccess(response) {
2628
data = response;
27-
form.reset();
2829
},
2930
onFailure: console.error,
30-
delayedMs: 1000,
31-
timeoutMs: 3000,
31+
delayedMs: 500,
32+
timeoutMs: 2000,
3233
});
3334
3435
const form = useCustomForm({
3536
schema: {
36-
type: "string",
37+
properties: {
38+
delay: {
39+
type: "integer",
40+
enum: [250, 1500, 2500],
41+
default: 1500,
42+
},
43+
reject: {
44+
type: "boolean",
45+
},
46+
value: {
47+
type: "string",
48+
},
49+
},
50+
},
51+
uiSchema: {
52+
delay: {
53+
"ui:widget": "radio",
54+
"ui:options": {
55+
enumNames: ["250ms", "1.5s", "2.5s"],
56+
},
57+
},
58+
},
59+
onSubmit(config: Config | undefined) {
60+
if (!config) return;
61+
mutation.run(config);
3762
},
38-
onSubmit: mutation.run,
3963
get disabled() {
4064
return mutation.isProcessed;
4165
},
4266
});
4367
</script>
4468

45-
<p>
46-
Reject: {isError}, delay: {(duration / 1000).toFixed(2)}sec
47-
</p>
48-
4969
<form use:form.enhance style="display: flex; flex-direction: column; gap: 1rem">
5070
<FormContent bind:value={form.formValue} />
5171
{#if mutation.isDelayed}

apps/docs/src/content/docs/integrations/generic-backend.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ sidebar:
66

77
import { Card, Code } from '@astrojs/starlight/components'
88

9-
import Form from './_generic-backend.svelte';
9+
import Form from './_generic-backend.astro';
1010
import formCode from './_generic-backend.svelte?raw';
1111

1212
You can improve the experience of sending data to the server by using the `useMutation` hook.
1313

1414
<Code code={formCode} lang="svelte" />
1515

1616
<Card>
17-
<Form client:only="svelte" />
17+
<Form />
1818
</Card>

0 commit comments

Comments
 (0)