import { useEffect, useReducer } from 'react'; import PilotForm from '../pilotForm/PilotForm'; import { AxiosError, AxiosResponse } from 'axios'; import { FormMode } from '../../enums/formMode'; import { initialState, reducer } from './reducer'; import ConfirmationDialog from '../confirmationDialog/ConfirmationDialog'; import PilotCard from '../pilotCard/PilotCard'; import { useUserRole } from '../../hooks/userRole/UseUserRole'; import { UserRole } from '../../enums/userRole'; import httpClient from '../../httpClient/httpClient' import { ScreenSize } from '../../enums/screenSize'; import { useBreakpoints } from '../../hooks/useBreakpoints/UseBreakpoints'; import { Alert, Button, Dropdown, DropdownItem, DropdownSection, Table, TableHeader, TableBody, TableColumn, TableRow, TableCell, DropdownTrigger, DropdownMenu } from '@heroui/react' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faEllipsisVertical, faPen, faEye, faTrash, faPlus } from '@fortawesome/free-solid-svg-icons' const Pilots: React.FC = () => { const [state, dispatch] = useReducer(reducer, initialState); const { userRole } = useUserRole(); const { screenSize } = useBreakpoints() const getPilots = async () => { try { let response: AxiosResponse; response = await httpClient.get( `api/pilots` ); if (response.data.length > 0) { dispatch({ type: 'SET_PILOTS', payload: response.data }); if (state.alert) { dispatch({ type: 'SET_ALERT', payload: undefined }) } } else { dispatch({ type: 'SET_ALERT', payload: { severity: 'default', message: 'No pilots found.' }}) dispatch({ type: 'SET_PILOTS', payload: [] }); } } catch (error) { const axiosError = error as AxiosError; dispatch({ type: 'SET_ALERT', payload: { severity: 'danger', message: `Loading of pilots failed with the following message: ${axiosError.message}`} }) } finally { dispatch({ type: 'SET_IS_LOADING', payload: false }) } }; const onOpenClosePilotForm = async (mode: FormMode, pilotId?: string) => { switch (mode) { case FormMode.ADD: case FormMode.EDIT: case FormMode.VIEW: dispatch({ type: 'SET_OPEN_CLOSE_ENTRY_FORM', payload: { formMode: mode, selectedPilotId: pilotId, isFormOpen: true } }) break; case FormMode.CANCEL: dispatch({ type: 'SET_OPEN_CLOSE_ENTRY_FORM', payload: { formMode: mode, selectedPilotId: undefined, isFormOpen: false } }) break; } }; const onDeletePilot = (pilotId: string) => { dispatch({ type: 'SET_DELETE', payload: { isConfirmDialogOpen: true, selectedPilotId: pilotId } }); }; const onConfirmationDialogConfirm = async () => { try { dispatch({ type: 'SET_IS_CONFIRMATION_DIALOG_LOADING', payload: true }); await httpClient.delete(`api/pilots/${state.selectedPilotId}`); dispatch({ type: 'SET_DELETE', payload: { isConfirmDialogOpen: false, selectedPilotId: undefined } }); await getPilots(); } catch (error) { const axiosError = error as AxiosError; dispatch({ type: 'SET_ALERT', payload: { severity: 'danger', message: `Loading of logbook entries failed with the following message: ${axiosError.message}`} }); } finally { dispatch({ type: 'SET_IS_CONFIRMATION_DIALOG_LOADING', payload: false }); } }; const onConfirmationDialogCancel = () => { dispatch({ type: 'SET_DELETE', payload: { isConfirmDialogOpen: false, selectedPilotId: undefined } }); }; const columns = [ { id: 'name', name: 'Name' }, { id: 'actions', name: 'Actions' } ] const renderCell = (pilot: any, columnKey: any) => { const cellValue = pilot[columnKey] switch (columnKey) { case 'actions': { return ( onOpenClosePilotForm(FormMode.EDIT, pilot.id)} startContent={} > Edit onOpenClosePilotForm(FormMode.VIEW, pilot.id)} startContent={} > View onDeletePilot(pilot.id)} startContent={} > Delete ) } default: { return cellValue } } } useEffect(() => { if (!state.isFormOpen) { getPilots(); } }, [state.isFormOpen]); return ( <>

Pilots

{userRole === UserRole.WRITE && }
{!state.isLoading && state.alert && (
dispatch({ type: 'SET_ALERT', payload: undefined }) } color={state.alert.severity} title={state.alert.message} />
)}
{state.pilots.length > 0 && screenSize !== ScreenSize.SM && {(column) => ( {column.name} )} {(item) => ( {(columnKey => ( {renderCell(item, columnKey)} ))} )}
} {state.pilots.length > 0 && screenSize === ScreenSize.SM && }
{state.isFormOpen && ( onOpenClosePilotForm(mode)} pilotId={state.selectedPilotId} /> )} {state.isConfirmDialogOpen && ( )} ); }; export default Pilots;