2021-06-16 18:03:06 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Machine } from '../../models/machine';
|
|
|
|
import { IApplication } from '../../models/application';
|
|
|
|
import { react2angular } from 'react2angular';
|
|
|
|
import { Loader } from '../base/loader';
|
|
|
|
import MachineAPI from '../../api/machine';
|
|
|
|
import { MachineCard } from './machine-card';
|
2021-06-17 10:25:13 +02:00
|
|
|
import { MachinesFilters } from './machines-filters';
|
2021-06-17 17:08:22 +02:00
|
|
|
import { User } from '../../models/user';
|
2021-06-16 18:03:06 +02:00
|
|
|
|
2021-07-01 12:34:10 +02:00
|
|
|
declare const Application: IApplication;
|
2021-06-16 18:03:06 +02:00
|
|
|
|
|
|
|
interface MachinesListProps {
|
2021-06-17 17:08:22 +02:00
|
|
|
user?: User,
|
2021-06-16 18:03:06 +02:00
|
|
|
onError: (message: string) => void,
|
2021-06-28 18:17:11 +02:00
|
|
|
onSuccess: (message: string) => void,
|
2021-06-16 18:03:06 +02:00
|
|
|
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-16 18:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component shows a list of all machines and allows filtering on that list.
|
|
|
|
*/
|
2021-06-28 18:17:11 +02:00
|
|
|
const MachinesList: React.FC<MachinesListProps> = ({ onError, onSuccess, onShowMachine, onReserveMachine, onLoginRequested, onEnrollRequested, user }) => {
|
2021-06-17 10:25:13 +02:00
|
|
|
// shown machines
|
2021-06-16 18:03:06 +02:00
|
|
|
const [machines, setMachines] = useState<Array<Machine>>(null);
|
2021-06-17 10:25:13 +02:00
|
|
|
// we keep the full list of machines, for filtering
|
|
|
|
const [allMachines, setAllMachines] = useState<Array<Machine>>(null);
|
2021-06-16 18:03:06 +02:00
|
|
|
|
2021-06-17 10:25:13 +02:00
|
|
|
// retrieve the full list of machines on component mount
|
2021-06-16 18:03:06 +02:00
|
|
|
useEffect(() => {
|
|
|
|
MachineAPI.index()
|
2021-06-17 10:25:13 +02:00
|
|
|
.then(data => setAllMachines(data))
|
2021-06-16 18:03:06 +02:00
|
|
|
.catch(e => onError(e));
|
|
|
|
}, []);
|
|
|
|
|
2021-06-17 10:25:13 +02:00
|
|
|
// filter the machines shown when the full list was retrieved
|
|
|
|
useEffect(() => {
|
|
|
|
handleFilterByStatus(true);
|
2021-07-01 12:34:10 +02:00
|
|
|
}, [allMachines]);
|
2021-06-17 10:25:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback triggered when the user changes the status filter.
|
|
|
|
* Set the 'machines' state to a filtered list, depending on the provided parameter.
|
2021-06-18 11:51:26 +02:00
|
|
|
* @param status, true = enabled machines, false = disabled machines, null = all machines
|
2021-06-17 10:25:13 +02:00
|
|
|
*/
|
|
|
|
const handleFilterByStatus = (status: boolean): void => {
|
|
|
|
if (!allMachines) return;
|
|
|
|
if (status === null) return setMachines(allMachines);
|
|
|
|
|
2021-06-18 11:51:26 +02:00
|
|
|
// enabled machines may have the m.disabled property null (for never disabled machines)
|
|
|
|
// or false (for re-enabled machines)
|
|
|
|
setMachines(allMachines.filter(m => !!m.disabled === !status));
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-17 10:25:13 +02:00
|
|
|
|
2021-06-16 18:03:06 +02:00
|
|
|
return (
|
|
|
|
<div className="machines-list">
|
2021-06-17 10:25:13 +02:00
|
|
|
<MachinesFilters onStatusSelected={handleFilterByStatus} />
|
|
|
|
<div className="all-machines">
|
|
|
|
{machines && machines.map(machine => {
|
2021-06-17 17:08:22 +02:00
|
|
|
return <MachineCard key={machine.id}
|
2021-07-01 12:34:10 +02:00
|
|
|
user={user}
|
|
|
|
machine={machine}
|
|
|
|
onShowMachine={onShowMachine}
|
|
|
|
onReserveMachine={onReserveMachine}
|
|
|
|
onError={onError}
|
|
|
|
onSuccess={onSuccess}
|
|
|
|
onLoginRequested={onLoginRequested}
|
|
|
|
onEnrollRequested={onEnrollRequested} />;
|
2021-06-17 10:25:13 +02:00
|
|
|
})}
|
|
|
|
</div>
|
2021-06-16 18:03:06 +02:00
|
|
|
</div>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-16 18:03:06 +02:00
|
|
|
|
2021-06-28 18:17:11 +02:00
|
|
|
const MachinesListWrapper: React.FC<MachinesListProps> = ({ user, onError, onSuccess, onShowMachine, onReserveMachine, onLoginRequested, onEnrollRequested }) => {
|
2021-06-16 18:03:06 +02:00
|
|
|
return (
|
|
|
|
<Loader>
|
2021-06-28 18:17:11 +02:00
|
|
|
<MachinesList user={user} onError={onError} onSuccess={onSuccess} onShowMachine={onShowMachine} onReserveMachine={onReserveMachine} onLoginRequested={onLoginRequested} onEnrollRequested={onEnrollRequested} />
|
2021-06-16 18:03:06 +02:00
|
|
|
</Loader>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-16 18:03:06 +02:00
|
|
|
|
2021-06-28 18:17:11 +02:00
|
|
|
Application.Components.component('machinesList', react2angular(MachinesListWrapper, ['user', 'onError', 'onSuccess', 'onShowMachine', 'onReserveMachine', 'onLoginRequested', 'onEnrollRequested']));
|