/** * This component displays a summary of the monthly payment schedule for the current cart, with a subscription */ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { react2angular } from 'react2angular'; import moment from 'moment'; import { IApplication } from '../models/application'; import '../lib/i18n'; import { PaymentSchedule } from '../models/payment-schedule'; import { Loader } from './loader'; import { FabModal } from './fab-modal'; import { IFablab } from '../models/fablab'; declare var Application: IApplication; declare var Fablab: IFablab; interface PaymentScheduleSummaryProps { schedule: PaymentSchedule } const PaymentScheduleSummary: React.FC = ({ schedule }) => { const { t } = useTranslation('shared'); const [modal, setModal] = useState(false); /** * Return the formatted localized date for the given date */ const formatDate = (date: Date): string => { return Intl.DateTimeFormat().format(moment(date).toDate()); } /** * Return the formatted localized amount for the given price (eg. 20.5 => "20,50 €") */ const formatPrice = (price: number): string => { return new Intl.NumberFormat(Fablab.intl_locale, {style: 'currency', currency: Fablab.intl_currency}).format(price); } /** * Test if all payment deadlines have the same amount */ const hasEqualDeadlines = (): boolean => { const prices = schedule.items.map(i => i.amount); return prices.every(p => p === prices[0]); } /** * Open or closes the modal dialog showing the full payment schedule */ const toggleFullScheduleModal = (): void => { setModal(!modal); } return (

{ t('app.shared.cart.your_payment_schedule') }

{hasEqualDeadlines() &&
  • {t('app.shared.cart.NUMBER_monthly_payment_of_AMOUNT', { NUMBER: schedule.items.length, AMOUNT: formatPrice(schedule.items[0].amount) })} {t('app.shared.cart.first_debit')}
} {!hasEqualDeadlines() &&
  • {t('app.shared.cart.monthly_payment_NUMBER', { NUMBER: 1 })} {formatPrice(schedule.items[0].amount)} {t('app.shared.cart.debit')}
  • {t('app.shared.cart.NUMBER_monthly_payment_of_AMOUNT', { NUMBER: schedule.items.length - 1, AMOUNT: formatPrice(schedule.items[1].amount) })}
}
    {schedule.items.map(item => (
  • {formatDate(item.due_date)} {formatPrice(item.amount)}
  • ))}
); } const PaymentScheduleSummaryWrapper: React.FC = ({ schedule }) => { return ( ); } Application.Components.component('paymentScheduleSummary', react2angular(PaymentScheduleSummaryWrapper, ['schedule']));