import LogbookCard from "../logbookCard/LogbookCard"; import { useEffect, useReducer } from "react"; import { useLogs } from "../../hooks/logs/UseLogs"; import { LogbookEntry } from "../logbook/LogbookEntry.interface"; import { initialState, reducer } from "./reducer"; import { useOidc } from "../../auth/oidcConfig"; import Alert from "../alert/Alert"; import { AxiosError, AxiosResponse } from "axios"; import httpClient from "../../httpClient/httpClient"; import { useBreakpoints } from "../../hooks/useBreakpoints/UseBreakpoints"; import { ScreenSize } from "../../enums/screenSize"; const Flights = () => { const [state, dispatch] = useReducer(reducer, initialState); const { isUserLoggedIn } = useOidc(); const { screenSize } = useBreakpoints(); const getFlights = async (pageIndex: number, pageSize: number) => { try { const response: AxiosResponse = await httpClient.get(`api/logs/flights`, { params: { skip: pageIndex, take: pageSize } }); const flights: LogbookEntry[] = response.data.entities; const hasMore: boolean = response.data.hasNextPage; const total: number = response.data.total; if (flights.length > 0) { const newFlights: LogbookEntry[] = [...state.flights, ...flights] const newPageIndex: number = state.pageIndex + flights.length; dispatch({ type: 'SET_FLIGHTS', payload: { flights: newFlights, hasMoreFlights: hasMore, pageIndex: newPageIndex, totalFlights: total }}) if (!isUserLoggedIn && flights.length >= 5) { dispatch({ type: 'SET_ALERT', payload: { severity: 'info', message: 'A limited number of flights displayed. Sign in to view all flights.'}}) } else { dispatch({ type: 'SET_ALERT', payload: undefined }) } } else { dispatch({ type: 'SET_ALERT', payload: { severity: 'info', message: 'No flights found' }}) } } catch (error) { const axiosError = error as AxiosError; dispatch({ type: 'SET_ALERT', payload: { severity: 'error', message: `Loading of flights failed with the following message: ${axiosError.message}`} }); } finally { dispatch({ type: 'SET_IS_LOADING', payload: false }) } } const loadMore = () => { getFlights(state.pageIndex, state.pageSize) } useEffect(() => { getFlights(state.pageIndex, state.pageSize); }, []) return (