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, onError: (message: string) => void, } /** * 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 MachineCardComponent: React.FC = ({ user, machine, onShowMachine, onReserveMachine, onError, onLoginRequested }) => { 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); } const machinePicture = (): ReactNode => { if (!machine.machine_image) { return
; } return (
) } return (
{machinePicture()}
{machine.name}
{!machine.disabled && setLoading(true)} onLoadingEnd={() => setLoading(false)} onError={onError} onReserveMachine={handleReserveMachine} onLoginRequested={onLoginRequested} className="reserve-button"> {t('app.public.machine_card.book')} }
); } export const MachineCard: React.FC = ({ user, machine, onShowMachine, onReserveMachine, onError, onLoginRequested }) => { return ( ); }