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 { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faEllipsisVertical, faPen, faEye, faTrash, faPlus } from '@fortawesome/free-solid-svg-icons'; import { CellContext, ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, useReactTable } from '@tanstack/react-table'; import { Pilot } from './Pilot.interface'; import Alert from '../alert/Alert'; import { useOidc } from '../../auth/oidcConfig'; interface ActionsProps { id: string; } const Pilots: React.FC = () => { const [state, dispatch] = useReducer(reducer, initialState); const { userRole } = useUserRole(); const { screenSize } = useBreakpoints(); const { isUserLoggedIn } = useOidc(); const Actions = ({ id }: ActionsProps) => { return (
) } 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: 'info', message: 'No pilots found.' }}) dispatch({ type: 'SET_PILOTS', payload: [] }); } } catch (error) { const axiosError = error as AxiosError; dispatch({ type: 'SET_ALERT', payload: { severity: 'error', 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: 'error', 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: ColumnDef[]= [ { id: 'name', accessorKey: 'name', header: 'Name' }, { id: 'actions', header: 'Actions', meta: { align: 'text-center', headerAlign: 'text-center' }, cell: (info: CellContext) => { return ( ) } } ] const textAlignment = { center: 'text-center', left: 'text-start', right: 'text-end' }; const table = useReactTable({ data: state.pilots, columns: columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel() }); useEffect(() => { if (!state.isFormOpen) { getPilots(); } }, [state.isFormOpen]); return ( <>

Pilots

{!state.isLoading && userRole === UserRole.WRITE && }
{!state.isLoading && state.alert && (
dispatch({ type: 'SET_ALERT', payload: undefined }) } severity={state.alert.severity} > {state.alert.message}
)} {state.pilots.length > 0 && screenSize !== ScreenSize.SM &&
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( ); })} ))} <> {table.getRowModel().rows.map((row) => { return ( {row.getVisibleCells().map((cell) => { return ( ); })} ); })}
{header.isPlaceholder ? null : (
{flexRender( header.column.columnDef.header, header.getContext() )} {/* {header.column.getCanFilter() ? (
) : null} */}
)}
*]:z-1 [&>*]:relative outline-solid outline-transparent data-[focus-visible=true]:z-10 data-[focus-visible=true]:outline-2 data-[focus-visible=true]:outline-focus data-[focus-visible=true]:outline-offset-2 before:pointer-events-none before:content-[''] before:absolute before:z-0 before:inset-0 before:opacity-0 data-[selected=true]:before:opacity-100 group-data-[disabled=true]/tr:text-foreground-300 group-data-[disabled=true]/tr:cursor-not-allowed before:bg-default/60 data-[selected=true]:text-default-foreground first:before:rounded-s-lg last:before:rounded-e-lg text-start`} key={cell.id} > {flexRender( cell.column.columnDef.cell, cell.getContext() )}
} {state.pilots.length > 0 && screenSize === ScreenSize.SM &&
<> {table.getRowModel().rows.map((row) => { return (
<> {row.getVisibleCells().map((cell) => { return ( <> {cell.column.columnDef.header !== 'Actions' && <>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
} ) })}
) })}
}
{state.isFormOpen && ( onOpenClosePilotForm(mode)} pilotId={state.selectedPilotId} /> )} {state.isConfirmDialogOpen && ( )} ); }; export default Pilots;