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

126 lines
4.2 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react';
import * as React from 'react';
2022-12-16 18:43:38 +01:00
import { Machine, MachineListFilter } from '../../models/machine';
2021-06-16 18:03:06 +02:00
import { IApplication } from '../../models/application';
import { react2angular } from 'react2angular';
import { Loader } from '../base/loader';
import MachineAPI from '../../api/machine';
2022-12-16 18:43:38 +01:00
import MachineCategoryAPI from '../../api/machine-category';
import { MachineCategory } from '../../models/machine-category';
2021-06-16 18:03:06 +02:00
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
2021-07-01 12:34:10 +02:00
declare const Application: IApplication;
2021-06-16 18:03:06 +02:00
interface MachinesListProps {
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,
onLoginRequested: () => Promise<User>,
2021-06-18 16:05:36 +02:00
onEnrollRequested: (trainingId: number) => void,
canProposePacks: boolean,
2021-06-16 18:03:06 +02:00
}
/**
* This component shows a list of all machines and allows filtering on that list.
*/
export const MachinesList: React.FC<MachinesListProps> = ({ onError, onSuccess, onShowMachine, onReserveMachine, onLoginRequested, onEnrollRequested, user, canProposePacks }) => {
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);
2022-12-16 18:43:38 +01:00
// shown machine categories
const [machineCategories, setMachineCategories] = useState<Array<MachineCategory>>([]);
// machine list filter
const [filter, setFilter] = useState<MachineListFilter>({
status: true,
category: null
});
2021-06-16 18:03:06 +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));
2022-12-16 18:43:38 +01:00
MachineCategoryAPI.index()
.then(data => setMachineCategories(data))
.catch(e => onError(e));
2021-06-16 18:03:06 +02:00
}, []);
2021-06-17 10:25:13 +02:00
// filter the machines shown when the full list was retrieved
useEffect(() => {
2022-12-16 18:43:38 +01:00
handleFilter();
2021-07-01 12:34:10 +02:00
}, [allMachines]);
2021-06-17 10:25:13 +02:00
2022-12-16 18:43:38 +01:00
// filter the machines shown when the filter was changed
useEffect(() => {
handleFilter();
}, [filter]);
2021-06-17 10:25:13 +02:00
/**
2022-12-16 18:43:38 +01:00
* Callback triggered when the user changes the filter.
* filter the machines shown when the filter was changed.
2021-06-17 10:25:13 +02:00
*/
2022-12-16 18:43:38 +01:00
const handleFilter = (): void => {
let machinesFiltered = [];
if (allMachines) {
if (filter.status === null) {
machinesFiltered = allMachines;
} else {
// enabled machines may have the m.disabled property null (for never disabled machines)
// or false (for re-enabled machines)
machinesFiltered = allMachines.filter(m => !!m.disabled === !filter.status);
}
if (filter.category !== null) {
machinesFiltered = machinesFiltered.filter(m => m.machine_category_id === filter.category);
}
}
setMachines(machinesFiltered);
};
2021-06-17 10:25:13 +02:00
2022-12-16 18:43:38 +01:00
/**
* Callback triggered when the user changes the filter.
* @param type, status, category
* @param value, status and category value
*/
const handleFilterChangedBy = (type: string, value: number | boolean | void) => {
setFilter({
...filter,
[type]: value
});
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">
2022-12-16 18:43:38 +01:00
<MachinesFilters onFilterChangedBy={handleFilterChangedBy} machineCategories={machineCategories}/>
2021-06-17 10:25:13 +02:00
<div className="all-machines">
{machines && machines.map(machine => {
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}
canProposePacks={canProposePacks}/>;
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
2022-10-28 17:13:41 +02:00
const MachinesListWrapper: React.FC<MachinesListProps> = (props) => {
2021-06-16 18:03:06 +02:00
return (
<Loader>
2022-10-28 17:13:41 +02:00
<MachinesList {...props} />
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
Application.Components.component('machinesList', react2angular(MachinesListWrapper, ['user', 'onError', 'onSuccess', 'onShowMachine', 'onReserveMachine', 'onLoginRequested', 'onEnrollRequested', 'canProposePacks']));