Skip to content

Commit

Permalink
fix: missing isLoading state for form, to block while validating, and…
Browse files Browse the repository at this point in the history
… submitting.
  • Loading branch information
patroza committed Jan 22, 2025
1 parent 6143d26 commit 936b25e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-rules-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-app/vue": minor
---

fix: missing isLoading state for form, to block while validating, and submitting.
16 changes: 12 additions & 4 deletions packages/vue/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,17 @@ export const buildFormFromSchema = <
const parse = S.decodeUnknownSync<any, any>(S.Struct(Struct.omit(s.fields, "_tag")) as any)
const isDirty = ref(false)
const isValid = ref(true)
const isLoading = ref(false)

const submit1 = <A>(onSubmit: (a: To) => Promise<A>) => async <T extends Promise<{ valid: boolean }>>(e: T) => {
const r = await e
if (!r.valid) return
return onSubmit(new s(parse(state.value)))
isLoading.value = true
try {
const r = await e
if (!r.valid) return
return onSubmit(new s(parse(state.value)))
} finally {
isLoading.value = false
}
}
const submit = submit1(onSubmit)

Expand All @@ -413,7 +419,9 @@ export const buildFormFromSchema = <
/** optimized for Native form submit callback or general use */
submitFromState,
isDirty,
isValid
isValid,
/** includes whole submit, including potential asynchronous validation steps */
isLoading
}
}

Expand Down
18 changes: 12 additions & 6 deletions packages/vue/src/makeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,19 @@ export const makeClient = <Locale extends string, R>(
const parse = S.decodeUnknown<any, any, R>(schema)
const isDirty = ref(false)
const isValid = ref(true)
const isLoading = ref(false)
const runPromise = Runtime.runPromise(getRuntime(runtime))

const submit1 =
(onSubmit: (a: To) => Effect<OnSubmitA, never, R>) => async <T extends Promise<{ valid: boolean }>>(e: T) => {
const r = await e
if (!r.valid) return
return runPromise(onSubmit(new s(await runPromise(parse(state.value)))))
isLoading.value = true
try {
const r = await e
if (!r.valid) return
return await runPromise(onSubmit(new s(await runPromise(parse(state.value)))))
} finally {
isLoading.value = false
}
}
const submit = submit1(onSubmit)

Expand All @@ -635,9 +641,8 @@ export const makeClient = <Locale extends string, R>(
)

const submitFromState = Effect.gen(function*() {
if (!isValid.value) return
return yield* onSubmit(yield* parse(state.value))
}) // () => submit(Promise.resolve({ valid: isValid.value }))
})

return {
fields,
Expand All @@ -646,7 +651,8 @@ export const makeClient = <Locale extends string, R>(
/** optimized for Native form submit callback or general use */
submitFromState,
isDirty,
isValid
isValid,
isLoading
}
}

Expand Down

0 comments on commit 936b25e

Please sign in to comment.