import React, { useEffect, useState } from 'react'; import { Accordion, AccordionDetails, AccordionSummary, Button, ChevronDownIcon, DatePicker, Drawer, Grid, IconButton, SaveIcon, Select, TextField, Typography, XmarkIcon } from '@noahspan/noahspan-components'; import { useForm, Controller, SubmitHandler, FormProvider } from 'react-hook-form'; import { ILogbookEntryFormProps } from './ILogbookEntryFormProps'; import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { useHttpClient } from '../../hooks/httpClient/UseHttpClient'; import { useAccessToken } from '../../hooks/accessToken/UseAcessToken'; import { useIsAuthenticated } from '@azure/msal-react'; import { FormMode } from '../../enums/formMode'; import { usePilots } from '../../hooks/pilots/UsePilots'; const LogbookEntryForm: React.FC = ({ entryId, isDrawerOpen, mode, onOpenClose }) => { const httpClient: AxiosInstance = useHttpClient(); const [isLoading, setIsLoading] = useState(false); const [selectedEntry, setSelectedEntry] = useState(); const [isDisabled, setIsDisabled] = useState(false); const [pilotOptions, setPilotOptions] = useState< { label: string; value: string }[] >([]); const { getAccessToken } = useAccessToken(); const isAuthenticated = useIsAuthenticated(); const defaultValues = { partitionKey: '', rowKey: '', pilotId: '', pilotName: '', date: null, aircraftMakeModel: '', aircraftIdentity: '', routeFrom: '', routeTo: '', durationOfFlight: null, singleEngineLand: null, simulatorAtd: null, landingsDay: null, landingsNight: null, groundTrainingReceived: null, flightTrainingReceived: null, crossCountry: null, night: null, solo: null, pilotInCommand: null, instrumentActual: null, instrumentSimulated: null, instrumentApproaches: null, instrumentHolds: null, instrumentNavTrack: null, notes: '' }; const methods = useForm(); const { pilots } = usePilots(); const onCancel = () => { methods.reset(defaultValues); onOpenClose(FormMode.CANCEL); }; const onSubmit = async (data: unknown) => { try { setIsLoading(true); const accessToken: string = await getAccessToken(); await httpClient.post(`api/logbook`, data, { headers: { Authorization: accessToken } }); methods.reset(defaultValues); onOpenClose(FormMode.CANCEL); } catch (error) { if (axios.isAxiosError(error)) { const errResp = error.response; console.log(errResp?.data.message); } else { } } finally { setIsLoading(false); } }; useEffect(() => { if (mode === FormMode.VIEW) { setIsDisabled(true); } }, [mode]); useEffect(() => { const getEntry = async () => { try { setIsLoading(true); const config = isAuthenticated ? { headers: { Authorization: await getAccessToken() } } : {}; const response: AxiosResponse = await httpClient.get( `api/logbook/${entryId}`, config ); const entry = response.data; methods.reset(entry); } catch (error) { console.log(error); } finally { setIsLoading(false); } }; if (entryId) { getEntry(); } }, [entryId]); useEffect(() => { if (pilots) { const newPilotsOptions = pilots.map((pilot) => { return { label: pilot.name, value: pilot.id }; }); setPilotOptions(newPilotsOptions); } }, [pilots]); return (
{`${mode.toString().toLowerCase().charAt(0).toUpperCase() + mode.toString().slice(1).toLowerCase()} Pilot`} Pilot * { return (