From 97f7a0aac89121990972b26ae999195b127266ff Mon Sep 17 00:00:00 2001 From: Du Peng Date: Fri, 1 Apr 2022 16:16:32 +0200 Subject: [PATCH] Fix bug: unable to show payment modal for stripe --- .../payment-schedules-table.tsx | 173 +++++++++++------- .../payment/stripe/stripe-elements.tsx | 1 - 2 files changed, 102 insertions(+), 72 deletions(-) diff --git a/app/frontend/src/javascript/components/payment-schedule/payment-schedules-table.tsx b/app/frontend/src/javascript/components/payment-schedule/payment-schedules-table.tsx index b36a40335..9164c9555 100644 --- a/app/frontend/src/javascript/components/payment-schedule/payment-schedules-table.tsx +++ b/app/frontend/src/javascript/components/payment-schedule/payment-schedules-table.tsx @@ -1,4 +1,4 @@ -import React, { ReactEventHandler, useState } from 'react'; +import React, { ReactEventHandler, useState, useEffect, ReactElement } from 'react'; import { useTranslation } from 'react-i18next'; import { Loader } from '../base/loader'; import _ from 'lodash'; @@ -11,6 +11,8 @@ import { import FormatLib from '../../lib/format'; import { PaymentScheduleItemActions, TypeOnce } from './payment-schedule-item-actions'; import { StripeElements } from '../payment/stripe/stripe-elements'; +import SettingAPI from '../../api/setting'; +import { Setting, SettingName } from '../../models/setting'; interface PaymentSchedulesTableProps { paymentSchedules: Array, @@ -35,6 +37,13 @@ const PaymentSchedulesTableComponent: React.FC = ({ [TypeOnce.CardUpdate, new Map()], [TypeOnce.UpdatePaymentMean, new Map()] ])); + const [gateway, setGateway] = useState(null); + + useEffect(() => { + SettingAPI.get(SettingName.PaymentGateway) + .then(setting => setGateway(setting)) + .catch(error => onError(error)); + }, []); /** * Check if the requested payment schedule is displayed with its deadlines (PaymentScheduleItem) or without them @@ -110,76 +119,98 @@ const PaymentSchedulesTableComponent: React.FC = ({ refreshList(); }; - return ( -
- - - - - - - - {showCustomer && } - - - - {paymentSchedules.map(p => - - )} - -
- {t('app.shared.schedules_table.schedule_num')}{t('app.shared.schedules_table.date')}{t('app.shared.schedules_table.price')}{t('app.shared.schedules_table.customer')} -
- - - - - - - - {showCustomer && } - - - - - - -
{expandCollapseIcon(p.id)}{p.reference}{FormatLib.date(_.minBy(p.items, 'due_date').due_date)}{FormatLib.price(p.total)}{p.user.name}{downloadScheduleButton(p.id)}
- -
- - - - - - - - - - {_.orderBy(p.items, 'due_date').map(item => - - - - - )} - -
{t('app.shared.schedules_table.deadline')}{t('app.shared.schedules_table.amount')}{t('app.shared.schedules_table.state')} -
{FormatLib.date(item.due_date)}{FormatLib.price(item.amount)}{formatState(item, p)} - -
-
-
-
-
-
- ); + const renderPaymentSchedulesTable = (): ReactElement => { + return ( + + + + + + + {showCustomer && } + + + + {paymentSchedules.map(p => + + )} + +
+ {t('app.shared.schedules_table.schedule_num')}{t('app.shared.schedules_table.date')}{t('app.shared.schedules_table.price')}{t('app.shared.schedules_table.customer')} +
+ + + + + + + + {showCustomer && } + + + + + + +
{expandCollapseIcon(p.id)}{p.reference}{FormatLib.date(_.minBy(p.items, 'due_date').due_date)}{FormatLib.price(p.total)}{p.user.name}{downloadScheduleButton(p.id)}
+ +
+ + + + + + + + + + {_.orderBy(p.items, 'due_date').map(item => + + + + + )} + +
{t('app.shared.schedules_table.deadline')}{t('app.shared.schedules_table.amount')}{t('app.shared.schedules_table.state')} +
{FormatLib.date(item.due_date)}{FormatLib.price(item.amount)}{formatState(item, p)} + +
+
+
+
+ ); + }; + + /** + * Determine which gateway is enabled and return the appropriate payment schedules + */ + if (gateway === null) return
; + + switch (gateway.value) { + case 'stripe': + return ( + + {renderPaymentSchedulesTable()} + + ); + case 'payzen': + return ( +
+ {renderPaymentSchedulesTable()} +
+ ); + case null: + default: + console.error(`[PaymentSchedulesTable] Unimplemented gateway: ${gateway.value}`); + return
; + } }; PaymentSchedulesTableComponent.defaultProps = { showCustomer: false }; diff --git a/app/frontend/src/javascript/components/payment/stripe/stripe-elements.tsx b/app/frontend/src/javascript/components/payment/stripe/stripe-elements.tsx index 7d6f1f07b..68a912733 100644 --- a/app/frontend/src/javascript/components/payment/stripe/stripe-elements.tsx +++ b/app/frontend/src/javascript/components/payment/stripe/stripe-elements.tsx @@ -27,7 +27,6 @@ export const StripeElements: React.FC = memo(({ children }) => { {stripe && {children} } - {!stripe && children}
); });