2020-11-09 15:17:38 +01:00
|
|
|
import React, { useState } from 'react';
|
2020-11-05 17:32:37 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { react2angular } from 'react2angular';
|
2021-04-08 10:46:09 +02:00
|
|
|
import '../../lib/i18n';
|
|
|
|
import { Loader } from '../base/loader';
|
|
|
|
import { FabModal } from '../base/fab-modal';
|
|
|
|
import { PaymentSchedule } from '../../models/payment-schedule';
|
|
|
|
import { IApplication } from '../../models/application';
|
2021-06-30 15:32:10 +02:00
|
|
|
import FormatLib from '../../lib/format';
|
2020-11-05 17:32:37 +01:00
|
|
|
|
2021-07-01 12:34:10 +02:00
|
|
|
declare const Application: IApplication;
|
2020-11-05 17:32:37 +01:00
|
|
|
|
|
|
|
interface PaymentScheduleSummaryProps {
|
2020-11-24 13:26:15 +01:00
|
|
|
schedule: PaymentSchedule
|
2020-11-05 17:32:37 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 16:21:12 +02:00
|
|
|
/**
|
|
|
|
* This component displays a summary of the monthly payment schedule for the current cart, with a subscription
|
|
|
|
*/
|
2021-10-12 17:29:35 +02:00
|
|
|
export const PaymentScheduleSummary: React.FC<PaymentScheduleSummaryProps> = ({ schedule }) => {
|
2020-11-05 17:32:37 +01:00
|
|
|
const { t } = useTranslation('shared');
|
2021-04-07 16:21:12 +02:00
|
|
|
|
|
|
|
// is open, the modal dialog showing the full details of the payment schedule?
|
2020-11-05 17:32:37 +01:00
|
|
|
const [modal, setModal] = useState(false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if all payment deadlines have the same amount
|
|
|
|
*/
|
|
|
|
const hasEqualDeadlines = (): boolean => {
|
2020-11-12 16:44:55 +01:00
|
|
|
const prices = schedule.items.map(i => i.amount);
|
2020-11-05 17:32:37 +01:00
|
|
|
return prices.every(p => p === prices[0]);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2020-11-05 17:32:37 +01:00
|
|
|
/**
|
|
|
|
* Open or closes the modal dialog showing the full payment schedule
|
|
|
|
*/
|
|
|
|
const toggleFullScheduleModal = (): void => {
|
|
|
|
setModal(!modal);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2020-11-05 17:32:37 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="payment-schedule-summary">
|
|
|
|
<div>
|
2022-06-21 12:09:56 +02:00
|
|
|
<h4>{ t('app.shared.payment_schedule_summary.your_payment_schedule') }</h4>
|
2020-11-09 15:22:22 +01:00
|
|
|
{hasEqualDeadlines() && <ul>
|
|
|
|
<li>
|
|
|
|
<span className="schedule-item-info">
|
2022-06-21 12:09:56 +02:00
|
|
|
{t('app.shared.payment_schedule_summary.NUMBER_monthly_payment_of_AMOUNT', { NUMBER: schedule.items.length, AMOUNT: FormatLib.price(schedule.items[0].amount) })}
|
2020-11-09 15:22:22 +01:00
|
|
|
</span>
|
2022-06-21 12:09:56 +02:00
|
|
|
<span className="schedule-item-date">{t('app.shared.payment_schedule_summary.first_debit')}</span>
|
2020-11-09 15:22:22 +01:00
|
|
|
</li>
|
|
|
|
</ul>}
|
2020-11-05 17:32:37 +01:00
|
|
|
{!hasEqualDeadlines() && <ul>
|
|
|
|
<li>
|
2022-06-21 12:09:56 +02:00
|
|
|
<span className="schedule-item-info">{t('app.shared.payment_schedule_summary.monthly_payment_NUMBER', { NUMBER: 1 })}</span>
|
2021-06-30 15:32:10 +02:00
|
|
|
<span className="schedule-item-price">{FormatLib.price(schedule.items[0].amount)}</span>
|
2022-06-21 12:09:56 +02:00
|
|
|
<span className="schedule-item-date">{t('app.shared.payment_schedule_summary.debit')}</span>
|
2020-11-05 17:32:37 +01:00
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<span className="schedule-item-info">
|
2022-06-21 12:09:56 +02:00
|
|
|
{t('app.shared.payment_schedule_summary.NUMBER_monthly_payment_of_AMOUNT', { NUMBER: schedule.items.length - 1, AMOUNT: FormatLib.price(schedule.items[1].amount) })}
|
2020-11-05 17:32:37 +01:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</ul>}
|
2022-06-21 12:09:56 +02:00
|
|
|
<button className="view-full-schedule" onClick={toggleFullScheduleModal}>{t('app.shared.payment_schedule_summary.view_full_schedule')}</button>
|
|
|
|
<FabModal title={t('app.shared.payment_schedule_summary.your_payment_schedule')} isOpen={modal} toggleModal={toggleFullScheduleModal}>
|
2020-11-09 15:17:38 +01:00
|
|
|
<ul className="full-schedule">
|
2021-07-01 12:34:10 +02:00
|
|
|
{schedule.items.map(item => (
|
|
|
|
<li key={String(item.due_date)}>
|
|
|
|
<span className="schedule-item-date">{FormatLib.date(item.due_date)}</span>
|
|
|
|
<span> </span>
|
|
|
|
<span className="schedule-item-price">{FormatLib.price(item.amount)}</span>
|
|
|
|
</li>
|
|
|
|
))}
|
2020-11-09 15:17:38 +01:00
|
|
|
</ul>
|
|
|
|
</FabModal>
|
2020-11-05 17:32:37 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-10-11 18:50:53 +02:00
|
|
|
|
2020-11-24 13:26:15 +01:00
|
|
|
const PaymentScheduleSummaryWrapper: React.FC<PaymentScheduleSummaryProps> = ({ schedule }) => {
|
2020-11-05 17:32:37 +01:00
|
|
|
return (
|
2020-11-09 15:17:38 +01:00
|
|
|
<Loader>
|
2020-11-24 13:26:15 +01:00
|
|
|
<PaymentScheduleSummary schedule={schedule} />
|
2020-11-09 15:17:38 +01:00
|
|
|
</Loader>
|
2020-11-05 17:32:37 +01:00
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2020-11-05 17:32:37 +01:00
|
|
|
|
2020-11-24 13:26:15 +01:00
|
|
|
Application.Components.component('paymentScheduleSummary', react2angular(PaymentScheduleSummaryWrapper, ['schedule']));
|