import { useEffect, useState } from 'react'; import { useForm, Controller, FormProvider } from 'react-hook-form'; import { IPilotFormProps } from './IPilotFormProps'; import { AxiosError, AxiosResponse } from 'axios'; import { FormMode } from '../../enums/formMode'; import { Person } from '@microsoft/microsoft-graph-types'; // import PilotFormCertificates from '../pilotFormCertificates/PilotFormCertificates'; // import PilotFormEndorsements from '../pilotFormEndorsements/PilotFormEndorsements'; // import PilotFormMedical from '../pilotFormMedical/PilotFormMedical'; import { useOidc } from '../../auth/oidcConfig'; import httpClient from '../../httpClient/httpClient'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSave, faXmark } from '@fortawesome/free-solid-svg-icons'; import { states } from './states'; import { useBreakpoints } from '../../hooks/useBreakpoints/UseBreakpoints'; import { ScreenSize } from '../../enums/screenSize'; const PilotForm: React.FC = ({ pilotId, isDrawerOpen, mode, onOpenClose }: IPilotFormProps) => { const [peoplePickerValue, setPeoplePickerValue] = useState(''); const [peoplePickerResults, setPeoplePickerResults] = useState([]); const [isPeoplePickerLoading, setIsPeoplePickerLoading] = useState(false); const [selectedPerson, setSelectedPerson] = useState({ displayName: '' }); const [isLoading, setIsLoading] = useState(false); const { isUserLoggedIn } = useOidc(); const defaultValues = { name: '', address: '', city: '', state: '', postalCode: '', email: '', phone: '', userId: '' }; const methods = useForm({ defaultValues: defaultValues }); const [isDisabled, setIsDisabled] = useState(false); const [isError, setIsError] = useState(false); const { screenSize } = useBreakpoints(); const onCancel = () => { methods.reset(defaultValues); onOpenClose(FormMode.CANCEL); setSelectedPerson({ userPrincipalName: '', displayName: '' }); setIsDisabled(false) }; const onSubmit = async (data: unknown) => { try { setIsLoading(true); if (!pilotId) { await httpClient.post(`api/pilots`, data); } else { await httpClient.put(`api/pilots/pilot/${pilotId}`, data) } methods.reset(defaultValues) onOpenClose(FormMode.CANCEL); } catch (error) { const axiosError = error as AxiosError; const responseData = axiosError.response?.data as any; console.log(responseData.message); } finally { setIsLoading(false); } }; useEffect(() => { if (mode === FormMode.VIEW) { setIsDisabled(true); } }, [mode]); useEffect(() => { const getPilot = async () => { try { setIsLoading(true); const response: AxiosResponse = await httpClient.get( `api/pilots/${pilotId}` ); const pilot = response.data; setPeoplePickerValue(pilot.name); methods.reset(pilot); } catch (error) { console.log(error); } finally { setIsLoading(false); } }; if (pilotId) { getPilot(); } }, [pilotId]); return (
{}} checked={isDrawerOpen} />

{`${mode.toString().toLowerCase().charAt(0).toUpperCase() + mode.toString().slice(1).toLowerCase()} Pilot`}

Name *
( )} />
{isUserLoggedIn && <>
Address *
( )} />
City *
( )} />
State *
( )} />
Postal Code *
( )} />
Email
( )} />
Phone Number
( )} />
} {/* {isAuthenticated && } */} {/*
*/}
{mode.toString() !== FormMode.VIEW && ( )}
); }; export default PilotForm;