compute some error on submit

This commit is contained in:
Sebastian 2023-05-26 17:32:33 -03:00
parent 8d85426f0e
commit f5f3d3e23c
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
4 changed files with 30 additions and 13 deletions

View File

@ -13,7 +13,7 @@ export function NiceForm<T extends object>({
}: {
children?: ComponentChildren;
initial: Partial<T>;
onSubmit?: (v: T) => void;
onSubmit?: (v: Partial<T>) => void;
form: FlexibleForm<T>;
onUpdate?: (d: Partial<T>) => void;
}) {

View File

@ -58,8 +58,8 @@ export function FormProvider<T>({
}: {
initialValue?: Partial<T>;
onUpdate?: (v: Partial<T>) => void;
onSubmit?: (v: T) => void;
computeFormState?: (v: T) => FormState<T>;
onSubmit?: (v: Partial<T>, s: FormState<T> | undefined) => void;
computeFormState?: (v: Partial<T>) => FormState<T>;
children: ComponentChildren;
}): VNode {
// const value = useRef(initialValue ?? {});
@ -85,7 +85,11 @@ export function FormProvider<T>({
onSubmit={(e) => {
e.preventDefault();
//@ts-ignore
if (onSubmit) onSubmit(value.current);
if (onSubmit)
onSubmit(
value.current,
!computeFormState ? undefined : computeFormState(value.current),
);
}}
>
{children}

View File

@ -1,5 +1,8 @@
import { TranslatedString } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import {
notifyError,
useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { VNode, h } from "preact";
import { createNewForm } from "../handlers/forms.js";
@ -41,16 +44,26 @@ export function CreateAccount({
: undefined,
},
repeat: {
// error: !v.repeat
// ? i18n.str`required`
// // : v.repeat !== v.password
// // ? i18n.str`doesn't match`
// : undefined,
error: !v.repeat
? i18n.str`required`
: v.repeat !== v.password
? i18n.str`doesn't match`
: undefined,
},
};
}}
onSubmit={async (v) => {
onNewAccount(v.password);
onSubmit={async (v, s) => {
console.log(v, s);
const error = s?.password?.error ?? s?.repeat?.error;
console.log(error);
if (error) {
notifyError(
"Can't create account" as TranslatedString,
error as TranslatedString,
);
} else {
onNewAccount(v.password!);
}
}}
>
<div class="mb-4">

View File

@ -32,7 +32,7 @@ export function UnlockAccount({
<Form.Provider
onSubmit={async (v) => {
try {
await onAccountUnlocked(v.password);
await onAccountUnlocked(v.password!);
notifyInfo("Account unlocked" as TranslatedString);
} catch (e) {