2022-11-16 10:08:09 +01:00
|
|
|
import { ReactNode, useState } from 'react';
|
|
|
|
import * as React 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>,
|
2021-06-18 16:05:36 +02:00
|
|
|
onEnrollRequested: (trainingId: number) => void,
|
2021-06-17 17:08:22 +02:00
|
|
|
onError: (message: string) => void,
|
2021-06-28 18:17:11 +02:00
|
|
|
onSuccess: (message: string) => void,
|
2022-03-18 19:44:30 +01:00
|
|
|
canProposePacks: boolean,
|
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.
|
|
|
|
*/
|
2022-05-24 17:55:19 +02:00
|
|
|
const MachineCard: React.FC<MachineCardProps> = ({ user, machine, onShowMachine, onReserveMachine, onError, onSuccess, onLoginRequested, onEnrollRequested, canProposePacks }) => {
|
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);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-17 10:25:13 +02:00
|
|
|
/**
|
|
|
|
* Callback triggered when the user clicks on the 'view' button
|
|
|
|
*/
|
|
|
|
const handleShowMachine = (): void => {
|
|
|
|
onShowMachine(machine);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-17 10:25:13 +02:00
|
|
|
|
2022-06-20 15:35:38 +02:00
|
|
|
/**
|
|
|
|
* Return the machine's picture or a placeholder
|
|
|
|
*/
|
2021-06-16 18:03:06 +02:00
|
|
|
const machinePicture = (): ReactNode => {
|
2022-12-05 11:21:14 +01:00
|
|
|
if (!machine.machine_image_attributes?.attachment_url) {
|
2021-06-18 11:51:26 +02:00
|
|
|
return <div className="machine-picture no-picture" />;
|
2021-06-16 18:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-12-05 11:21:14 +01:00
|
|
|
<div className="machine-picture" style={{ backgroundImage: `url(${machine.machine_image_attributes.attachment_url}), url('/default-image.png')` }} onClick={handleShowMachine} />
|
2021-07-01 12:34:10 +02:00
|
|
|
);
|
|
|
|
};
|
2021-06-16 18:03:06 +02:00
|
|
|
|
|
|
|
return (
|
2021-07-01 12:34:10 +02:00
|
|
|
<div className={`machine-card ${loading ? 'loading' : ''} ${machine.disabled ? 'disabled' : ''}`}>
|
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}
|
2021-07-01 12:34:10 +02:00
|
|
|
machineId={machine.id}
|
|
|
|
onLoadingStart={() => setLoading(true)}
|
|
|
|
onLoadingEnd={() => setLoading(false)}
|
|
|
|
onError={onError}
|
|
|
|
onSuccess={onSuccess}
|
|
|
|
onReserveMachine={handleReserveMachine}
|
|
|
|
onLoginRequested={onLoginRequested}
|
|
|
|
onEnrollRequested={onEnrollRequested}
|
2022-03-18 19:44:30 +01:00
|
|
|
canProposePacks={canProposePacks}
|
2021-07-01 12:34:10 +02:00
|
|
|
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-18 12:47:04 +02:00
|
|
|
<span>
|
|
|
|
<button onClick={handleShowMachine} className="show-button">
|
|
|
|
<i className="fas fa-eye" />
|
|
|
|
{t('app.public.machine_card.consult')}
|
|
|
|
</button>
|
|
|
|
</span>
|
2021-06-16 18:03:06 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-16 18:03:06 +02:00
|
|
|
|
2022-06-20 15:35:38 +02:00
|
|
|
const MachineCardWrapper: React.FC<MachineCardProps> = (props) => {
|
2021-06-15 17:34:12 +02:00
|
|
|
return (
|
2021-06-16 18:03:06 +02:00
|
|
|
<Loader>
|
2022-06-20 15:35:38 +02:00
|
|
|
<MachineCard {...props} />
|
2021-06-16 18:03:06 +02:00
|
|
|
</Loader>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2022-05-24 17:55:19 +02:00
|
|
|
|
|
|
|
export { MachineCardWrapper as MachineCard };
|