1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-08 23:46:14 +01:00
fab-manager/app/frontend/src/javascript/components/payment-schedules-dashboard.tsx

100 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2021-02-09 17:18:33 +01:00
import { IApplication } from '../models/application';
import { useTranslation } from 'react-i18next';
2021-04-08 09:35:09 +02:00
import { Loader } from './base/loader';
2021-02-09 17:18:33 +01:00
import { react2angular } from 'react2angular';
import PaymentScheduleAPI from '../api/payment-schedule';
import { PaymentSchedulesTable } from './payment-schedules-table';
2021-04-08 09:35:09 +02:00
import { FabButton } from './base/fab-button';
2021-02-09 17:18:33 +01:00
import { User } from '../models/user';
import { PaymentSchedule } from '../models/payment-schedule';
2021-02-09 17:18:33 +01:00
declare var Application: IApplication;
interface PaymentSchedulesDashboardProps {
currentUser: User
}
// how many payment schedules should we display for each page?
2021-02-09 17:18:33 +01:00
const PAGE_SIZE = 20;
/**
* This component shows a list of all payment schedules with their associated deadlines (aka. PaymentScheduleItem) and invoices
* for the currentUser
*/
2021-02-09 17:18:33 +01:00
const PaymentSchedulesDashboard: React.FC<PaymentSchedulesDashboardProps> = ({ currentUser }) => {
const { t } = useTranslation('logged');
// list of displayed payment schedules
const [paymentSchedules, setPaymentSchedules] = useState<Array<PaymentSchedule>>([]);
// current page
const [pageNumber, setPageNumber] = useState<number>(1);
/**
* When the component is loaded first, refresh the list of schedules to fill the first page.
*/
useEffect(() => {
handleRefreshList();
}, []);
2021-02-09 17:18:33 +01:00
/**
* Fetch from the API the next payment schedules to display, for the current filters, and append them to the current results table.
*/
const handleLoadMore = (): void => {
setPageNumber(pageNumber + 1);
const api = new PaymentScheduleAPI();
api.index({ query: { page: pageNumber + 1, size: PAGE_SIZE }}).then((res) => {
const list = paymentSchedules.concat(res);
setPaymentSchedules(list);
});
}
/**
* Reload from te API all the currently displayed payment schedules
*/
const handleRefreshList = (onError?: (msg: any) => void): void => {
2021-02-09 17:18:33 +01:00
const api = new PaymentScheduleAPI();
api.index({ query: { page: 1, size: PAGE_SIZE * pageNumber }}).then((res) => {
setPaymentSchedules(res);
}).catch((err) => {
if (typeof onError === 'function') { onError(err.message); }
2021-02-09 17:18:33 +01:00
});
}
/**
* Check if the current collection of payment schedules is empty or not.
*/
const hasSchedules = (): boolean => {
return paymentSchedules.length > 0;
}
/**
* Check if there are some results for the current filters that aren't currently shown.
*/
const hasMoreSchedules = (): boolean => {
return hasSchedules() && paymentSchedules.length < paymentSchedules[0].max_length;
}
return (
<div className="payment-schedules-dashboard">
{!hasSchedules() && <div>{t('app.logged.dashboard.payment_schedules.no_payment_schedules')}</div>}
2021-02-09 17:18:33 +01:00
{hasSchedules() && <div className="schedules-list">
<PaymentSchedulesTable paymentSchedules={paymentSchedules} showCustomer={false} refreshList={handleRefreshList} operator={currentUser} />
{hasMoreSchedules() && <FabButton className="load-more" onClick={handleLoadMore}>{t('app.logged.dashboard.payment_schedules.load_more')}</FabButton>}
2021-02-09 17:18:33 +01:00
</div>}
</div>
);
}
const PaymentSchedulesDashboardWrapper: React.FC<PaymentSchedulesDashboardProps> = ({ currentUser }) => {
return (
<Loader>
<PaymentSchedulesDashboard currentUser={currentUser} />
</Loader>
);
}
Application.Components.component('paymentSchedulesDashboard', react2angular(PaymentSchedulesDashboardWrapper, ['currentUser']));