import React, { ReactNode, useState } from 'react'; import { Machine } from '../../models/machine'; import { useTranslation } from 'react-i18next'; import { Loader } from '../base/loader'; import { ReserveButton } from './reserve-button'; import { User } from '../../models/user'; interface MachineCardProps { user?: User, machine: Machine, onShowMachine: (machine: Machine) => void, onReserveMachine: (machine: Machine) => void, onLoginRequested: () => Promise, onEnrollRequested: (trainingId: number) => void, onError: (message: string) => void, onSuccess: (message: string) => void, canProposePacks: boolean, } /** * This component is a box showing the picture of the given machine and two buttons: one to start the reservation process * and another to redirect the user to the machine description page. */ const MachineCard: React.FC = ({ user, machine, onShowMachine, onReserveMachine, onError, onSuccess, onLoginRequested, onEnrollRequested, canProposePacks }) => { const { t } = useTranslation('public'); // shall we display a loader to prevent double-clicking, while the machine details are loading? const [loading, setLoading] = useState(false); /** * Callback triggered when the user clicks on the 'reserve' button and has passed all the verifications */ const handleReserveMachine = (): void => { onReserveMachine(machine); }; /** * Callback triggered when the user clicks on the 'view' button */ const handleShowMachine = (): void => { onShowMachine(machine); }; /** * Return the machine's picture or a placeholder */ const machinePicture = (): ReactNode => { if (!machine.machine_image) { return
; } return (
); }; return (
{machinePicture()}
{machine.name}
{!machine.disabled && setLoading(true)} onLoadingEnd={() => setLoading(false)} onError={onError} onSuccess={onSuccess} onReserveMachine={handleReserveMachine} onLoginRequested={onLoginRequested} onEnrollRequested={onEnrollRequested} canProposePacks={canProposePacks} className="reserve-button"> {t('app.public.machine_card.book')} }
); }; const MachineCardWrapper: React.FC = (props) => { return ( ); }; export { MachineCardWrapper as MachineCard };