1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-11 05:54:15 +01:00

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-06-16 18:03:06 +02:00
import React, { ReactNode } 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-15 17:34:12 +02:00
interface MachineCardProps {
2021-06-16 18:03:06 +02:00
machine: Machine,
onShowMachine: (machine: Machine) => void,
onReserveMachine: (machine: Machine) => 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-16 18:03:06 +02:00
const MachineCardComponent: React.FC<MachineCardProps> = ({ machine, onShowMachine, onReserveMachine }) => {
const { t } = useTranslation('public');
2021-06-17 10:25:13 +02:00
/**
* Callback triggered when the user clicks on the 'reserve' button.
* We handle the training verification process here, before redirecting the user to the reservation calendar.
*/
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:&#xf03e;/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 (
<div className="machine-card">
{machinePicture()}
<div className="machine-name">
{machine.name}
</div>
<div className="machine-actions">
{!machine.disabled && <button onClick={handleReserveMachine} className="reserve-button">
<i className="fas fa-bookmark" />
{t('app.public.machine_card.book')}
</button>}
<button onClick={handleShowMachine} className={`show-button ${machine.disabled ? 'single-button': ''}`}>
<i className="fas fa-eye" />
{t('app.public.machine_card.consult')}
</button>
</div>
</div>
);
}
export const MachineCard: React.FC<MachineCardProps> = ({ machine, onShowMachine, onReserveMachine }) => {
2021-06-15 17:34:12 +02:00
return (
2021-06-16 18:03:06 +02:00
<Loader>
<MachineCardComponent machine={machine} onShowMachine={onShowMachine} onReserveMachine={onReserveMachine} />
</Loader>
);
2021-06-15 17:34:12 +02:00
}