From 64e3705669e7c12b8013704654f17cf8eaf659d4 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 25 May 2023 18:08:20 -0300 Subject: cases, account details and new-form screen --- .../src/handlers/FormProvider.tsx | 43 ++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx') diff --git a/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx b/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx index 87c4c43fb..4ac90ad57 100644 --- a/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx +++ b/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx @@ -1,6 +1,16 @@ -import { AbsoluteTime, TranslatedString } from "@gnu-taler/taler-util"; +import { + AbsoluteTime, + AmountJson, + TranslatedString, +} from "@gnu-taler/taler-util"; import { ComponentChildren, VNode, createContext, h } from "preact"; -import { MutableRef, StateUpdater, useEffect, useRef } from "preact/hooks"; +import { + MutableRef, + StateUpdater, + useEffect, + useRef, + useState, +} from "preact/hooks"; export interface FormType { value: MutableRef>; @@ -14,6 +24,8 @@ export const FormContext = createContext>({}); export type FormState = { [field in keyof T]?: T[field] extends AbsoluteTime + ? Partial + : T[field] extends AmountJson ? Partial : T[field] extends Array ? Partial> @@ -40,22 +52,31 @@ export interface InputArrayFieldState extends InputFieldState { export function FormProvider({ children, initialValue, - onUpdate, + onUpdate: notify, onSubmit, computeFormState, }: { initialValue?: Partial; onUpdate?: (v: Partial) => void; - onSubmit: (v: T) => void; + onSubmit?: (v: T) => void; computeFormState?: (v: T) => FormState; children: ComponentChildren; }): VNode { - const value = useRef(initialValue ?? {}); - useEffect(() => { - return function onUnload() { - value.current = initialValue ?? {}; - }; - }); + // const value = useRef(initialValue ?? {}); + // useEffect(() => { + // return function onUnload() { + // value.current = initialValue ?? {}; + // }; + // }); + // const onUpdate = notify + const [state, setState] = useState>(initialValue ?? {}); + const value = { current: state }; + // console.log("RENDER", initialValue, value); + const onUpdate = (v: typeof state) => { + // console.log("updated"); + setState(v); + if (notify) notify(v); + }; return ( ({ onSubmit={(e) => { e.preventDefault(); //@ts-ignore - onSubmit(value.current); + if (onSubmit) onSubmit(value.current); }} > {children} -- cgit v1.2.3