diff options
Diffstat (limited to 'packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx')
-rw-r--r-- | packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx | 175 |
1 files changed, 11 insertions, 164 deletions
diff --git a/packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx b/packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx index bbfa65ca7..2f3ee054d 100644 --- a/packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx +++ b/packages/aml-backoffice-ui/src/pages/CaseUpdate.tsx @@ -23,35 +23,27 @@ import { assertUnreachable, } from "@gnu-taler/taler-util"; import { - Addon, Button, + FormMetadata, InternationalizationAPI, LocalNotificationBanner, RenderAllFieldsByUiConfig, - StringConverter, - UIFieldHandler, - UIFormField, + UIHandlerId, useExchangeApiContext, useLocalNotificationHandler, - useTranslationContext, + useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { privatePages } from "../Routing.js"; import { - FormMetadata, - UIFieldBaseDescription, - UIFormFieldBaseConfig, - UIFormFieldConfig, - UIHandlerId, - useUiFormsContext, + useUiFormsContext } from "../context/ui-forms.js"; import { preloadedForms } from "../forms/index.js"; -import { FormErrors, FormHandler, useFormState } from "../hooks/form.js"; +import { FormErrors, convertUiField, useFormState } from "../hooks/form.js"; import { useOfficer } from "../hooks/officer.js"; import { Justification } from "./CaseDetails.js"; -import { HandleAccountNotReady } from "./HandleAccountNotReady.js"; import { undefinedIfEmpty } from "./CreateAccount.js"; -import { getConverterById } from "../utils/converter.js"; +import { HandleAccountNotReady } from "./HandleAccountNotReady.js"; function searchForm( i18n: InternationalizationAPI, @@ -112,16 +104,18 @@ export function CaseUpdate({ theForm.config.design.forEach((section) => { section.fields.forEach((field) => { if ("id" in field.properties) { + //FIXME: this should be a validation + if (shape.indexOf(field.properties.id) !== -1) { + throw Error(`already present: ${field.properties.id}`) + } shape.push(field.properties.id); - // const path = field.properties.id.split("."); - // defaultValue = setValueDeeper(defaultValue, path, undefined); } }); }); const [form, state] = useFormState<FormType>(shape, initial, (st) => { const errors = undefinedIfEmpty<FormErrors<FormType>>({ - state: !st.state ? i18n.str`required` : undefined, + state: st.state === undefined ? i18n.str`required` : undefined, threshold: !st.threshold ? i18n.str`required` : undefined, when: !st.when ? i18n.str`required` : undefined, comment: !st.comment ? i18n.str`required` : undefined, @@ -140,8 +134,6 @@ export function CaseUpdate({ }; }); - console.log("NOW FORM", form); - const validatedForm = state.status !== "ok" ? undefined : state.result; const submitHandler = @@ -249,7 +241,6 @@ export function SelectForm({ account }: { account: string }) { const { i18n } = useTranslationContext(); const { forms } = useUiFormsContext(); const pf = preloadedForms(i18n); - return ( <div> <pre>New form for account: {account.substring(0, 16)}...</pre> @@ -279,147 +270,3 @@ export function SelectForm({ account }: { account: string }) { ); } -function getValueDeeper( - object: Record<string, any>, - names: string[], -): UIFieldHandler { - if (names.length === 0) return object as UIFieldHandler; - const [head, ...rest] = names; - if (!head) { - return getValueDeeper(object, rest); - } - if (object === undefined) { - throw Error("handler not found"); - } - return getValueDeeper(object[head], rest); -} - -function setValueDeeper(object: any, names: string[], value: any): any { - if (names.length === 0) return value; - const [head, ...rest] = names; - if (!head) { - return setValueDeeper(object, rest, value); - } - if (object === undefined) { - return { [head]: setValueDeeper({}, rest, value) }; - } - return { ...object, [head]: setValueDeeper(object[head] ?? {}, rest, value) }; -} - -function getAddonById(_id: string | undefined): Addon { - return undefined!; -} - - -function converInputFieldsProps( - form: FormHandler<unknown>, - p: UIFormFieldBaseConfig, -) { - return { - converter: getConverterById(p.converterId), - handler: getValueDeeper(form, p.id.split(".")), - }; -} - -function converBaseFieldsProps( - i18n_: InternationalizationAPI, - p: UIFieldBaseDescription, -) { - return { - after: getAddonById(p.addonAfterId), - before: getAddonById(p.addonBeforeId), - hidden: p.hidden, - name: p.name, - help: i18n_.str`${p.help}`, - label: i18n_.str`${p.label}`, - tooltip: i18n_.str`${p.tooltip}`, - }; -} - -function convertUiField( - i18n_: InternationalizationAPI, - fieldConfig: UIFormFieldConfig[], - form: FormHandler<unknown>, -): UIFormField[] { - return fieldConfig.map((config) => { - // NON input fields - switch (config.type) { - case "caption": { - const resp: UIFormField = { - type: config.type, - properties: converBaseFieldsProps(i18n_, config.properties), - }; - return resp; - } - case "group": { - const resp: UIFormField = { - type: config.type, - properties: { - ...converBaseFieldsProps(i18n_, config.properties), - fields: convertUiField(i18n_, config.properties.fields, form), - }, - }; - return resp; - } - } - // Input Fields - switch (config.type) { - case "absoluteTime": { - return undefined!; - } - case "amount": { - return { - type: "amount", - properties: { - ...converBaseFieldsProps(i18n_, config.properties), - ...converInputFieldsProps(form, config.properties), - }, - } as UIFormField; - } - case "array": { - return undefined!; - } - case "choiceHorizontal": { - return { - type: "choiceHorizontal", - properties: { - ...converBaseFieldsProps(i18n_, config.properties), - ...converInputFieldsProps(form, config.properties), - choices: config.properties.choices, - }, - } as UIFormField; - } - case "choiceStacked": - case "file": - case "integer": - case "selectMultiple": - case "selectOne": { - return undefined!; - } - case "text": { - return { - type: "text", - properties: { - ...converBaseFieldsProps(i18n_, config.properties), - ...converInputFieldsProps(form, config.properties), - }, - } as UIFormField; - } - case "textArea": { - return { - type: "text", - properties: { - ...converBaseFieldsProps(i18n_, config.properties), - ...converInputFieldsProps(form, config.properties), - }, - } as UIFormField; - } - case "toggle": { - return undefined!; - } - default: { - assertUnreachable(config); - } - } - }); -} |