2021-06-17 17:08:22 +02:00
|
|
|
import React, { ReactNode, useState } from 'react';
|
2021-06-15 17:34:12 +02:00
|
|
|
import { Machine } from '../../models/machine';
|
2021-06-16 18:03:06 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Loader } from '../base/loader';
|
2021-06-17 17:08:22 +02:00
|
|
|
import { ReserveButton } from './reserve-button';
|
|
|
|
import { User } from '../../models/user';
|
2021-06-15 17:34:12 +02:00
|
|
|
|
|
|
|
interface MachineCardProps {
|
2021-06-17 17:08:22 +02:00
|
|
|
user?: User,
|
2021-06-16 18:03:06 +02:00
|
|
|
machine: Machine,
|
|
|
|
onShowMachine: (machine: Machine) => void,
|
|
|
|
onReserveMachine: (machine: Machine) => void,
|
2021-06-17 17:08:22 +02:00
|
|
|
onLoginRequested: () => Promise<User>,
|
|
|
|
onError: (message: string) => void,
|
2021-06-15 17:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-06-17 17:08:22 +02:00
|
|
|
const MachineCardComponent: React.FC<MachineCardProps> = ({ user, machine, onShowMachine, onReserveMachine, onError, onLoginRequested }) => {
|
2021-06-16 18:03:06 +02:00
|
|
|
const { t } = useTranslation('public');
|
|
|
|
|
2021-06-17 17:08:22 +02:00
|
|
|
// shall we display a loader to prevent double-clicking, while the machine details are loading?
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
2021-06-17 10:25:13 +02:00
|
|
|
/**
|
2021-06-17 17:08:22 +02:00
|
|
|
* Callback triggered when the user clicks on the 'reserve' button and has passed all the verifications
|
2021-06-17 10:25:13 +02:00
|
|
|
*/
|
|
|
|
const handleReserveMachine = (): void => {
|
|
|
|
onReserveMachine(machine);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Callback triggered when the user clicks on the 'view' button
|
|
|
|
*/
|
|
|
|
const handleShowMachine = (): void => {
|
|
|
|
onShowMachine(machine);
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:03:06 +02:00
|
|
|
const machinePicture = (): ReactNode => {
|
|
|
|
if (!machine.machine_image) {
|
|
|
|
return (
|
|
|
|
<div className="machine-picture">
|
|
|
|
<img src="data:image/png;base64,"
|
|
|
|
data-src="holder.js/100%x100%/text:/font:'Font Awesome 5 Free'/icon"
|
|
|
|
className="img-responsive"
|
|
|
|
alt={machine.name} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="machine-picture" style={{ backgroundImage: `url(${machine.machine_image})` }} onClick={handleShowMachine} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-06-17 17:08:22 +02:00
|
|
|
<div className={`machine-card ${loading ? 'loading' : ''}`}>
|
2021-06-16 18:03:06 +02:00
|
|
|
{machinePicture()}
|
|
|
|
<div className="machine-name">
|
|
|
|
{machine.name}
|
|
|
|
</div>
|
|
|
|
<div className="machine-actions">
|
2021-06-17 17:08:22 +02:00
|
|
|
{!machine.disabled && <ReserveButton currentUser={user}
|
|
|
|
machineId={machine.id}
|
|
|
|
onLoadingStart={() => setLoading(true)}
|
|
|
|
onLoadingEnd={() => setLoading(false)}
|
|
|
|
onError={onError}
|
|
|
|
onReserveMachine={handleReserveMachine}
|
|
|
|
onLoginRequested={onLoginRequested}
|
|
|
|
className="reserve-button">
|
2021-06-16 18:03:06 +02:00
|
|
|
<i className="fas fa-bookmark" />
|
|
|
|
{t('app.public.machine_card.book')}
|
2021-06-17 17:08:22 +02:00
|
|
|
</ReserveButton>}
|
2021-06-16 18:03:06 +02:00
|
|
|
<button onClick={handleShowMachine} className={`show-button ${machine.disabled ? 'single-button': ''}`}>
|
|
|
|
<i className="fas fa-eye" />
|
|
|
|
{t('app.public.machine_card.consult')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-17 17:08:22 +02:00
|
|
|
export const MachineCard: React.FC<MachineCardProps> = ({ user, machine, onShowMachine, onReserveMachine, onError, onLoginRequested }) => {
|
2021-06-15 17:34:12 +02:00
|
|
|
return (
|
2021-06-16 18:03:06 +02:00
|
|
|
<Loader>
|
2021-06-17 17:08:22 +02:00
|
|
|
<MachineCardComponent user={user} machine={machine} onShowMachine={onShowMachine} onReserveMachine={onReserveMachine} onError={onError} onLoginRequested={onLoginRequested} />
|
2021-06-16 18:03:06 +02:00
|
|
|
</Loader>
|
|
|
|
);
|
2021-06-15 17:34:12 +02:00
|
|
|
}
|