From 7a72a2c04299697b1453219750ce810f1cadf50b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 17 May 2024 01:08:01 -0300 Subject: fix #8857 --- .../paths/instance/templates/create/CreatePage.tsx | 37 +++++++++-------- .../paths/instance/templates/update/UpdatePage.tsx | 48 ++++++++++++---------- 2 files changed, 48 insertions(+), 37 deletions(-) (limited to 'packages/merchant-backoffice-ui') diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/create/CreatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/create/CreatePage.tsx index 0e9b5a284..50262be17 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/create/CreatePage.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/create/CreatePage.tsx @@ -70,7 +70,7 @@ interface Props { export function CreatePage({ onCreate, onBack }: Props): VNode { const { i18n } = useTranslationContext(); const { config } = useSessionContext(); - const {state:session} = useSessionContext(); + const { state: session } = useSessionContext(); const devices = useInstanceOtpDevices(); const [state, setState] = useState>({ @@ -100,11 +100,11 @@ export function CreatePage({ onCreate, onBack }: Props): VNode { : undefined, description: !state.description ? i18n.str`should not be empty` : undefined, amount: !state.amount - ? undefined + ? state.amount_editable ? undefined : i18n.str`required` : !parsedPrice ? i18n.str`not valid` : Amounts.isZero(parsedPrice) - ? i18n.str`must be greater than 0` + ? state.amount_editable ? undefined : i18n.str`must be greater than 0` : undefined, minimum_age: state.minimum_age && state.minimum_age < 0 @@ -125,25 +125,30 @@ export function CreatePage({ onCreate, onBack }: Props): VNode { (k) => (errors as Record)[k] !== undefined, ); + const zero = Amounts.stringify(Amounts.zeroOfCurrency(config.currency)) + const submitForm = () => { if (hasErrors) return Promise.reject(); + const contract_amount = state.amount_editable ? undefined : state.amount as AmountString + const contract_summary = state.summary_editable ? undefined : state.summary + const template_contract: TalerMerchantApi.TemplateContractDetails = { + minimum_age: state.minimum_age!, + pay_duration: Duration.toTalerProtocolDuration(state.pay_duration!), + amount: contract_amount, + summary: contract_summary, + currency: + cList.length > 1 && state.currency_editable + ? undefined + : config.currency, + } return onCreate({ template_id: state.id!, template_description: state.description!, - template_contract: { - minimum_age: state.minimum_age!, - pay_duration: Duration.toTalerProtocolDuration(state.pay_duration!), - amount: state.amount_editable ? undefined : state.amount, - summary: state.summary_editable ? undefined : state.summary, - currency: - cList.length > 1 && state.currency_editable - ? undefined - : config.currency, - }, - required_currency: config.currency, + template_contract, + required_currency: contract_amount !== undefined ? undefined : config.currency, editable_defaults: { - amount: !state.amount_editable ? undefined : state.amount, - summary: !state.summary_editable ? undefined : state.summary, + amount: !state.amount_editable ? undefined : (state.amount ?? zero), + summary: !state.summary_editable ? undefined : (state.summary ?? ""), currency: cList.length === 1 || !state.currency_editable ? undefined diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx index f8e3ac9b5..6bdcf6d18 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx @@ -68,7 +68,7 @@ interface Props { export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { const { i18n } = useTranslationContext(); const { config } = useSessionContext(); - const {state:session} = useSessionContext(); + const { state: session } = useSessionContext(); const [state, setState] = useState>({ description: template.template_description, @@ -76,8 +76,8 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { otpId: template.otp_id, pay_duration: template.template_contract.pay_duration ? Duration.fromTalerProtocolDuration( - template.template_contract.pay_duration, - ) + template.template_contract.pay_duration, + ) : undefined, summary: template.editable_defaults?.summary ?? template.template_contract.summary, @@ -85,13 +85,14 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { template.editable_defaults?.amount ?? (template.template_contract.amount as AmountString | undefined), currency_editable: !!template.editable_defaults?.currency, - summary_editable: !!template.editable_defaults?.summary, - amount_editable: !!template.editable_defaults?.amount, + summary_editable: template.editable_defaults?.summary !== undefined, + amount_editable: template.editable_defaults?.amount !== undefined, }); function updateState(up: (s: Partial) => Partial) { setState((old) => { const newState = up(old); + console.log("====", newState.amount) if (!newState.amount_editable) { newState.currency_editable = false; } @@ -117,11 +118,11 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { const errors: FormErrors = { description: !state.description ? i18n.str`should not be empty` : undefined, amount: !state.amount - ? undefined + ? state.amount_editable ? undefined : i18n.str`required` : !parsedPrice ? i18n.str`not valid` : Amounts.isZero(parsedPrice) - ? i18n.str`must be greater than 0` + ? state.amount_editable ? undefined : i18n.str`must be greater than 0` : undefined, minimum_age: state.minimum_age && state.minimum_age < 0 @@ -142,25 +143,30 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { (k) => (errors as Record)[k] !== undefined, ); + const zero = Amounts.stringify(Amounts.zeroOfCurrency(config.currency)) + const submitForm = () => { if (hasErrors) return Promise.reject(); + const contract_amount = state.amount_editable ? undefined : state.amount as AmountString + const contract_summary = state.summary_editable ? undefined : state.summary + const template_contract: TalerMerchantApi.TemplateContractDetails = { + minimum_age: state.minimum_age!, + pay_duration: Duration.toTalerProtocolDuration(state.pay_duration!), + // FIXME: Check if amount_editable is a plain currency, in that case the user must specify it. + amount: contract_amount, + summary: contract_summary, + currency: + cList.length > 1 && state.currency_editable + ? undefined + : config.currency, + } return onUpdate({ template_description: state.description!, - template_contract: { - minimum_age: state.minimum_age!, - pay_duration: Duration.toTalerProtocolDuration(state.pay_duration!), - // FIXME: Check if amount_editable is a plain currency, in that case the user must specify it. - amount: (state.amount_editable ? undefined : state.amount) as AmountString, - summary: state.summary_editable ? undefined : state.summary, - currency: - cList.length > 1 && state.currency_editable - ? undefined - : config.currency, - }, - required_currency: config.currency, + template_contract, + required_currency: contract_amount !== undefined ? undefined : config.currency, editable_defaults: { - amount: !state.amount_editable ? undefined : state.amount, - summary: !state.summary_editable ? undefined : state.summary, + amount: !state.amount_editable ? undefined : (state.amount ?? zero), + summary: !state.summary_editable ? undefined : (state.summary ?? ""), currency: cList.length === 1 || !state.currency_editable ? undefined -- cgit v1.2.3