import { ReactNode, useEffect, useState } from 'react'; import * as React from 'react'; import { FabPanel } from '../../base/fab-panel'; import { Loader } from '../../base/loader'; import { useTranslation } from 'react-i18next'; import { Credit, CreditableType } from '../../../models/credit'; import CreditAPI from '../../../api/credit'; import { HtmlTranslate } from '../../base/html-translate'; interface CreditsPanelProps { userId: number, onError: (message: string) => void, reservableType: CreditableType } /** * List all available credits for the given user and the given resource */ const CreditsPanel: React.FC = ({ userId, onError, reservableType }) => { const { t } = useTranslation('logged'); const [credits, setCredits] = useState>([]); useEffect(() => { CreditAPI.userResource(userId, reservableType) .then(res => setCredits(res)) .catch(error => onError(error)); }, []); /** * Compute the remaining hours for the given credit */ const remainingHours = (credit: Credit): number => { return credit.hours - credit.hours_used; }; /** * Display a placeholder when there's no credits to display */ const noCredits = (): ReactNode => { return (
{t('app.logged.dashboard.reservations_dashboard.credits_panel.no_credits')}
); }; return (

{t('app.logged.dashboard.reservations_dashboard.credits_panel.title')}

{credits.length !== 0 &&
{t('app.logged.dashboard.reservations_dashboard.credits_panel.info')}
}
{credits.map(c =>

{c.creditable.name}


{(c.hours_used && c.hours_used > 0) && }

)}
{credits.length === 0 && noCredits()}
); }; const CreditsPanelWrapper: React.FC = (props) => { return ( ); }; export { CreditsPanelWrapper as CreditsPanel };