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

87 lines
3.2 KiB
TypeScript
Raw Normal View History

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';
import { ReserveButton } from './reserve-button';
import { User } from '../../models/user';
2021-06-15 17:34:12 +02:00
interface MachineCardProps {
user?: User,
2021-06-16 18:03:06 +02:00
machine: Machine,
onShowMachine: (machine: Machine) => void,
onReserveMachine: (machine: Machine) => void,
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.
*/
const MachineCardComponent: React.FC<MachineCardProps> = ({ user, machine, onShowMachine, onReserveMachine, onError, onLoginRequested }) => {
2021-06-16 18:03:06 +02:00
const { t } = useTranslation('public');
// 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
/**
* 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 no-picture" />;
2021-06-16 18:03:06 +02:00
}
return (
<div className="machine-picture" style={{ backgroundImage: `url(${machine.machine_image})` }} onClick={handleShowMachine} />
)
}
return (
<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">
{!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')}
</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>
);
}
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>
<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
}