From c680f5aa71b08e978444df07f93c381f9d47ab82 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 5 Jun 2023 10:04:09 -0300 Subject: rename aml --- .../src/handlers/Caption.tsx | 35 --- .../src/handlers/FormProvider.tsx | 99 -------- .../exchange-backoffice-ui/src/handlers/Group.tsx | 41 --- .../src/handlers/InputAmount.tsx | 34 --- .../src/handlers/InputArray.tsx | 183 ------------- .../src/handlers/InputChoiceHorizontal.tsx | 86 ------- .../src/handlers/InputChoiceStacked.tsx | 111 -------- .../src/handlers/InputDate.tsx | 37 --- .../src/handlers/InputFile.tsx | 101 -------- .../src/handlers/InputInteger.tsx | 23 -- .../src/handlers/InputLine.tsx | 282 --------------------- .../src/handlers/InputSelectMultiple.tsx | 151 ----------- .../src/handlers/InputSelectOne.tsx | 134 ---------- .../src/handlers/InputText.tsx | 8 - .../src/handlers/InputTextArea.tsx | 8 - .../exchange-backoffice-ui/src/handlers/forms.ts | 135 ---------- .../src/handlers/useField.ts | 93 ------- 17 files changed, 1561 deletions(-) delete mode 100644 packages/exchange-backoffice-ui/src/handlers/Caption.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/Group.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputAmount.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputArray.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputChoiceHorizontal.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputChoiceStacked.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputDate.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputFile.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputInteger.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputLine.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputSelectMultiple.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputSelectOne.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputText.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/InputTextArea.tsx delete mode 100644 packages/exchange-backoffice-ui/src/handlers/forms.ts delete mode 100644 packages/exchange-backoffice-ui/src/handlers/useField.ts (limited to 'packages/exchange-backoffice-ui/src/handlers') diff --git a/packages/exchange-backoffice-ui/src/handlers/Caption.tsx b/packages/exchange-backoffice-ui/src/handlers/Caption.tsx deleted file mode 100644 index fbf154d89..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/Caption.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { VNode, h } from "preact"; -import { - IconAddon, - InputLine, - LabelWithTooltipMaybeRequired, - UIFormProps, -} from "./InputLine.js"; -import { TranslatedString } from "@gnu-taler/taler-util"; - -interface Props { - label: TranslatedString; - tooltip?: TranslatedString; - help?: TranslatedString; - before?: VNode; - after?: VNode; -} - -export function Caption({ before, after, label, tooltip, help }: Props): VNode { - return ( -
- {before !== undefined && ( - {before} - )} - - {after !== undefined && ( - {after} - )} - {help && ( -

- {help} -

- )} -
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx b/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx deleted file mode 100644 index a195c2051..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/FormProvider.tsx +++ /dev/null @@ -1,99 +0,0 @@ -import { - AbsoluteTime, - AmountJson, - TranslatedString, -} from "@gnu-taler/taler-util"; -import { ComponentChildren, VNode, createContext, h } from "preact"; -import { - MutableRef, - StateUpdater, - useEffect, - useRef, - useState, -} from "preact/hooks"; - -export interface FormType { - value: MutableRef>; - initialValue?: Partial; - onUpdate?: StateUpdater; - computeFormState?: (v: T) => FormState; -} - -//@ts-ignore -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> - : T[field] extends object - ? FormState - : Partial; -}; - -export interface InputFieldState { - /* should show the error */ - error?: TranslatedString; - /* should not allow to edit */ - readonly: boolean; - /* should show as disable */ - disabled: boolean; - /* should not show */ - hidden: boolean; -} - -export interface InputArrayFieldState extends InputFieldState { - elements: FormState[]; -} - -export function FormProvider({ - children, - initialValue, - onUpdate: notify, - onSubmit, - computeFormState, -}: { - initialValue?: Partial; - onUpdate?: (v: Partial) => void; - onSubmit?: (v: Partial, s: FormState | undefined) => void; - computeFormState?: (v: Partial) => FormState; - children: ComponentChildren; -}): VNode { - // 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 ( - -
{ - e.preventDefault(); - //@ts-ignore - if (onSubmit) - onSubmit( - value.current, - !computeFormState ? undefined : computeFormState(value.current), - ); - }} - > - {children} -
-
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/Group.tsx b/packages/exchange-backoffice-ui/src/handlers/Group.tsx deleted file mode 100644 index 0645f6d97..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/Group.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { TranslatedString } from "@gnu-taler/taler-util"; -import { VNode, h } from "preact"; -import { LabelWithTooltipMaybeRequired } from "./InputLine.js"; -import { RenderAllFieldsByUiConfig, UIFormField } from "./forms.js"; - -interface Props { - before?: TranslatedString; - after?: TranslatedString; - tooltipBefore?: TranslatedString; - tooltipAfter?: TranslatedString; - fields: UIFormField[]; -} - -export function Group({ - before, - after, - tooltipAfter, - tooltipBefore, - fields, -}: Props): VNode { - return ( -
-
- {before && ( - - )} -
-
- -
-
- {after && ( - - )} -
-
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputAmount.tsx b/packages/exchange-backoffice-ui/src/handlers/InputAmount.tsx deleted file mode 100644 index 9be9dd4d0..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputAmount.tsx +++ /dev/null @@ -1,34 +0,0 @@ -import { AmountJson, Amounts, TranslatedString } from "@gnu-taler/taler-util"; -import { VNode, h } from "preact"; -import { InputLine, UIFormProps } from "./InputLine.js"; -import { useField } from "./useField.js"; - -export function InputAmount( - props: { currency?: string } & UIFormProps, -): VNode { - const { value } = useField(props.name); - const currency = - !value || !(value as any).currency - ? props.currency - : (value as any).currency; - return ( - - type="text" - before={{ - type: "text", - text: currency as TranslatedString, - }} - converter={{ - //@ts-ignore - fromStringUI: (v): AmountJson => { - return Amounts.parseOrThrow(`${currency}:${v}`); - }, - //@ts-ignore - toStringUI: (v: AmountJson) => { - return v === undefined ? "" : Amounts.stringifyValue(v); - }, - }} - {...props} - /> - ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputArray.tsx b/packages/exchange-backoffice-ui/src/handlers/InputArray.tsx deleted file mode 100644 index 00379bed6..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputArray.tsx +++ /dev/null @@ -1,183 +0,0 @@ -import { Fragment, VNode, h } from "preact"; -import { useEffect, useState } from "preact/hooks"; -import { FormProvider, InputArrayFieldState } from "./FormProvider.js"; -import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js"; -import { RenderAllFieldsByUiConfig, UIFormField } from "./forms.js"; -import { useField } from "./useField.js"; -import { TranslatedString } from "@gnu-taler/taler-util"; - -function Option({ - label, - disabled, - isFirst, - isLast, - isSelected, - onClick, -}: { - label: TranslatedString; - isFirst?: boolean; - isLast?: boolean; - isSelected?: boolean; - disabled?: boolean; - onClick: () => void; -}): VNode { - let clazz = "relative flex border p-4 focus:outline-none disabled:text-grey"; - if (isFirst) { - clazz += " rounded-tl-md rounded-tr-md "; - } - if (isLast) { - clazz += " rounded-bl-md rounded-br-md "; - } - if (isSelected) { - clazz += " z-10 border-indigo-200 bg-indigo-50 "; - } else { - clazz += " border-gray-200"; - } - if (disabled) { - clazz += - " cursor-not-allowed bg-gray-50 text-gray-500 ring-gray-200 text-gray"; - } else { - clazz += " cursor-pointer"; - } - return ( - - ); -} - -export function InputArray( - props: { - fields: UIFormField[]; - labelField: string; - } & UIFormProps, -): VNode { - const { fields, labelField, name, label, required, tooltip } = props; - const { value, onChange, state } = useField(name); - const list = (value ?? []) as Array>; - const [selectedIndex, setSelected] = useState(undefined); - const selected = - selectedIndex === undefined ? undefined : list[selectedIndex]; - - return ( -
- - -
- {list.map((v, idx) => { - return ( -
- {selectedIndex !== undefined && ( - /** - * This form provider act as a substate of the parent form - * Consider creating an InnerFormProvider since not every feature is expected - */ - { - // current state is ignored - // the state is defined by the parent form - - // elements should be present in the state object since this is expected to be an array - //@ts-ignore - return state.elements[selectedIndex]; - }} - onSubmit={(v) => { - const newValue = [...list]; - newValue.splice(selectedIndex, 1, v); - onChange(newValue as T[K]); - setSelected(undefined); - }} - onUpdate={(v) => { - const newValue = [...list]; - newValue.splice(selectedIndex, 1, v); - onChange(newValue as T[K]); - }} - > -
-
- -
-
-
- )} - {selectedIndex !== undefined && ( -
-
- {selected !== undefined && ( - - )} -
-
- )} -
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputChoiceHorizontal.tsx b/packages/exchange-backoffice-ui/src/handlers/InputChoiceHorizontal.tsx deleted file mode 100644 index fdee35447..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputChoiceHorizontal.tsx +++ /dev/null @@ -1,86 +0,0 @@ -import { TranslatedString } from "@gnu-taler/taler-util"; -import { Fragment, VNode, h } from "preact"; -import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js"; -import { useField } from "./useField.js"; - -export interface Choice { - label: TranslatedString; - value: V; -} - -export function InputChoiceHorizontal( - props: { - choices: Choice[]; - } & UIFormProps, -): VNode { - const { - choices, - name, - label, - tooltip, - help, - placeholder, - required, - before, - after, - converter, - } = props; - const { value, onChange, state, isDirty } = useField(name); - if (state.hidden) { - return ; - } - - return ( -
- -
-
- {choices.map((choice, idx) => { - const isFirst = idx === 0; - const isLast = idx === choices.length - 1; - let clazz = - "relative inline-flex items-center px-3 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 focus:z-10"; - if (choice.value === value) { - clazz += - " text-white bg-indigo-600 hover:bg-indigo-500 ring-2 ring-indigo-600 hover:ring-indigo-500"; - } else { - clazz += " hover:bg-gray-100 border-gray-300"; - } - if (isFirst) { - clazz += " rounded-l-md"; - } else { - clazz += " -ml-px"; - } - if (isLast) { - clazz += " rounded-r-md"; - } - return ( - - ); - })} -
-
- {help && ( -

- {help} -

- )} -
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputChoiceStacked.tsx b/packages/exchange-backoffice-ui/src/handlers/InputChoiceStacked.tsx deleted file mode 100644 index c37984368..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputChoiceStacked.tsx +++ /dev/null @@ -1,111 +0,0 @@ -import { TranslatedString } from "@gnu-taler/taler-util"; -import { Fragment, VNode, h } from "preact"; -import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js"; -import { useField } from "./useField.js"; - -export interface Choice { - label: TranslatedString; - description?: TranslatedString; - value: V; -} - -export function InputChoiceStacked( - props: { - choices: Choice[]; - } & UIFormProps, -): VNode { - const { - choices, - name, - label, - tooltip, - help, - placeholder, - required, - before, - after, - converter, - } = props; - const { value, onChange, state, isDirty } = useField(name); - if (state.hidden) { - return ; - } - - return ( -
- -
-
- {choices.map((choice) => { - // const currentValue = !converter - // ? choice.value - // : converter.fromStringUI(choice.value) ?? ""; - - let clazz = - "border relative block cursor-pointer rounded-lg bg-white px-6 py-4 shadow-sm focus:outline-none sm:flex sm:justify-between"; - if (choice.value === value) { - clazz += - " border-transparent border-indigo-600 ring-2 ring-indigo-600"; - } else { - clazz += " border-gray-300"; - } - - return ( - - ); - })} -
-
- {help && ( -

- {help} -

- )} -
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputDate.tsx b/packages/exchange-backoffice-ui/src/handlers/InputDate.tsx deleted file mode 100644 index 1fd81aad9..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputDate.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { AbsoluteTime } from "@gnu-taler/taler-util"; -import { InputLine, UIFormProps } from "./InputLine.js"; -import { CalendarIcon } from "@heroicons/react/24/outline"; -import { VNode, h } from "preact"; -import { format, parse } from "date-fns"; - -export function InputDate( - props: { pattern?: string } & UIFormProps, -): VNode { - const pattern = props.pattern ?? "dd/MM/yyyy"; - return ( - - type="text" - after={{ - type: "icon", - icon: , - }} - converter={{ - //@ts-ignore - fromStringUI: (v): AbsoluteTime => { - if (!v) return AbsoluteTime.never(); - const t_ms = parse(v, pattern, Date.now()).getTime(); - return AbsoluteTime.fromMilliseconds(t_ms); - }, - //@ts-ignore - toStringUI: (v: AbsoluteTime) => { - return !v || !v.t_ms - ? "" - : v.t_ms === "never" - ? "never" - : format(v.t_ms, pattern); - }, - }} - {...props} - /> - ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputFile.tsx b/packages/exchange-backoffice-ui/src/handlers/InputFile.tsx deleted file mode 100644 index 0d89a98a3..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputFile.tsx +++ /dev/null @@ -1,101 +0,0 @@ -import { Fragment, VNode, h } from "preact"; -import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js"; -import { useField } from "./useField.js"; - -export function InputFile( - props: { maxBites: number; accept?: string } & UIFormProps, -): VNode { - const { - name, - label, - placeholder, - tooltip, - required, - help, - maxBites, - accept, - } = props; - const { value, onChange, state } = useField(name); - - if (state.hidden) { - return
; - } - return ( -
- - {!value || !(value as string).startsWith("data:image/") ? ( -
-
- -
- - {/*

or drag and drop

*/} -
-
-
- ) : ( -
- - -
{ - onChange(undefined!); - }} - > - Clear -
-
- )} - {help &&

{help}

} -
- ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputInteger.tsx b/packages/exchange-backoffice-ui/src/handlers/InputInteger.tsx deleted file mode 100644 index fb04e3852..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputInteger.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import { VNode, h } from "preact"; -import { InputLine, UIFormProps } from "./InputLine.js"; - -export function InputInteger( - props: UIFormProps, -): VNode { - return ( - { - return !v ? 0 : Number.parseInt(v, 10); - }, - //@ts-ignore - toStringUI: (v?: number): string => { - return v === undefined ? "" : String(v); - }, - }} - {...props} - /> - ); -} diff --git a/packages/exchange-backoffice-ui/src/handlers/InputLine.tsx b/packages/exchange-backoffice-ui/src/handlers/InputLine.tsx deleted file mode 100644 index 9448ef5e4..000000000 --- a/packages/exchange-backoffice-ui/src/handlers/InputLine.tsx +++ /dev/null @@ -1,282 +0,0 @@ -import { TranslatedString } from "@gnu-taler/taler-util"; -import { ComponentChildren, Fragment, VNode, h } from "preact"; -import { useField } from "./useField.js"; - -export interface IconAddon { - type: "icon"; - icon: VNode; -} -interface ButtonAddon { - type: "button"; - onClick: () => void; - children: ComponentChildren; -} -interface TextAddon { - type: "text"; - text: TranslatedString; -} -type Addon = IconAddon | ButtonAddon | TextAddon; - -interface StringConverter { - toStringUI: (v?: T) => string; - fromStringUI: (v?: string) => T; -} - -export interface UIFormProps { - name: K; - label: TranslatedString; - placeholder?: TranslatedString; - tooltip?: TranslatedString; - help?: TranslatedString; - before?: Addon; - after?: Addon; - required?: boolean; - converter?: StringConverter; -} - -export type FormErrors = { - [P in keyof T]?: string | FormErrors; -}; - -//@ts-ignore -const TooltipIcon = ( - - - -); - -export function LabelWithTooltipMaybeRequired({ - label, - required, - tooltip, -}: { - label: TranslatedString; - required?: boolean; - tooltip?: TranslatedString; -}): VNode { - const Label = ( - -
- -
-
- ); - const WithTooltip = tooltip ? ( -
- {Label} - - {TooltipIcon} - - -
- ) : ( - Label - ); - if (required) { - return ( -
- {WithTooltip} - * -
- ); - } - return WithTooltip; -} - -function InputWrapper({ - children, - label, - tooltip, - before, - after, - help, - error, - required, -}: { error?: string; children: ComponentChildren } & UIFormProps): VNode { - return ( -
- -
- {before && - (before.type === "text" ? ( - - {before.text} - - ) : before.type === "icon" ? ( -
- {before.icon} -
- ) : before.type === "button" ? ( - - ) : undefined)} - - {children} - - {after && - (after.type === "text" ? ( - - {after.text} - - ) : after.type === "icon" ? ( -
- {after.icon} -
- ) : after.type === "button" ? ( - - ) : undefined)} -
- {error && ( -

- {error} -

- )} - {help && ( -

- {help} -

- )} -
- ); -} - -function defaultToString(v: unknown) { - return v === undefined ? "" : typeof v !== "object" ? String(v) : ""; -} -function defaultFromString(v: string) { - return v; -} - -type InputType = "text" | "text-area" | "password" | "email" | "number"; - -export function InputLine( - props: { type: InputType } & UIFormProps, -): VNode { - const { name, placeholder, before, after, converter, type } = props; - const { value, onChange, state, isDirty } = useField(name); - - if (state.hidden) return
; - - let clazz = - "block w-full rounded-md border-0 py-1.5 shadow-sm ring-1 ring-inset focus:ring-2 focus:ring-inset sm:text-sm sm:leading-6 disabled:cursor-not-allowed disabled:bg-gray-50 disabled:text-gray-500 disabled:ring-gray-200"; - if (before) { - switch (before.type) { - case "icon": { - clazz += " pl-10"; - break; - } - case "button": { - clazz += " rounded-none rounded-r-md "; - break; - } - case "text": { - clazz += " min-w-0 flex-1 rounded-r-md rounded-none "; - break; - } - } - } - if (after) { - switch (after.type) { - case "icon": { - clazz += " pr-10"; - break; - } - case "button": { - clazz += " rounded-none rounded-l-md"; - break; - } - case "text": { - clazz += " min-w-0 flex-1 rounded-l-md rounded-none "; - break; - } - } - } - const showError = isDirty && state.error; - if (showError) { - clazz += - " text-red-900 ring-red-300 placeholder:text-red-300 focus:ring-red-500"; - } else { - clazz += - " text-gray-900 ring-gray-300 placeholder:text-gray-400 focus:ring-indigo-600"; - } - const fromString: (s: string) => any = - converter?.fromStringUI ?? defaultFromString; - const toString: (s: any) => string = converter?.toStringUI ?? defaultToString; - - if (type === "text-area") { - return ( - - {...props} - error={showError ? state.error : undefined} - > -