diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx')
-rw-r--r-- | packages/merchant-backoffice-ui/src/paths/instance/templates/update/UpdatePage.tsx | 46 |
1 files changed, 26 insertions, 20 deletions
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 eedb77f28..32c5637aa 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 @@ -51,7 +51,7 @@ type Entity = { description?: string; otpId?: string | null; summary?: string; - amount?: AmountString; + amount?: string; minimum_age?: number; pay_duration?: Duration; summary_editable?: boolean; @@ -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<Partial<Entity>>({ 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,8 +85,8 @@ 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<Entity>) => Partial<Entity>) { @@ -117,11 +117,11 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { const errors: FormErrors<Entity> = { 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,23 +142,29 @@ export function UpdatePage({ template, onUpdate, onBack }: Props): VNode { (k) => (errors as Record<string, unknown>)[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 onUpdate({ 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, - }, + 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 |