1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-03 23:52:19 +01:00

81 lines
2.9 KiB
TypeScript
Raw Normal View History

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';
import { User } from '../../models/user';
2021-06-16 18:03:06 +02:00
declare var Application: IApplication;
interface MachinesListProps {
user?: User,
2021-06-16 18:03:06 +02:00
onError: (message: string) => void,
onShowMachine: (machine: Machine) => void,
onReserveMachine: (machine: Machine) => void,
onLoginRequested: () => Promise<User>,
2021-06-16 18:03:06 +02:00
}
/**
* This component shows a list of all machines and allows filtering on that list.
*/
const MachinesList: React.FC<MachinesListProps> = ({ onError, onShowMachine, onReserveMachine, onLoginRequested, 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);
}, [allMachines])
/**
* Callback triggered when the user changes the status filter.
* Set the 'machines' state to a filtered list, depending on the provided parameter.
*/
const handleFilterByStatus = (status: boolean): void => {
if (!allMachines) return;
if (status === null) return setMachines(allMachines);
setMachines(allMachines.filter(m => m.disabled === !status));
}
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 => {
return <MachineCard key={machine.id}
user={user}
machine={machine}
onShowMachine={onShowMachine}
onReserveMachine={onReserveMachine}
onError={onError}
onLoginRequested={onLoginRequested} />
2021-06-17 10:25:13 +02:00
})}
</div>
2021-06-16 18:03:06 +02:00
</div>
);
}
const MachinesListWrapper: React.FC<MachinesListProps> = ({ user, onError, onShowMachine, onReserveMachine, onLoginRequested }) => {
2021-06-16 18:03:06 +02:00
return (
<Loader>
<MachinesList user={user} onError={onError} onShowMachine={onShowMachine} onReserveMachine={onReserveMachine} onLoginRequested={onLoginRequested} />
2021-06-16 18:03:06 +02:00
</Loader>
);
}
Application.Components.component('machinesList', react2angular(MachinesListWrapper, ['user', 'onError', 'onShowMachine', 'onReserveMachine', 'onLoginRequested']));