import { useEffect, useState } from 'react'; import { useForm, Controller, FormProvider } from 'react-hook-form'; import { Button, Drawer, Grid, IconButton, PeoplePicker, SaveIcon, StateSelect, TextField, Typography, XmarkIcon } from '@noahspan/noahspan-components'; import { IPilotFormProps } from './IPilotFormProps'; import { AxiosError, 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 { Person } from '@microsoft/microsoft-graph-types'; const PilotForm: React.FC = ({ pilotId, isDrawerOpen, mode, onOpenClose }: IPilotFormProps) => { const httpClient: AxiosInstance = useHttpClient(); const [peoplePickerResults, setPeoplePickerResults] = useState([]); const [isPeoplePickerLoading, setIsPeoplePickerLoading] = useState(false); const [selectedPerson, setSelectedPerson] = useState({ userPrincipalName: '', displayName: '' }); const [isLoading, setIsLoading] = useState(false); const { getAccessToken } = useAccessToken(); const isAuthenticated = useIsAuthenticated(); const defaultValues = { partitionKey: 'pilot', rowKey: 'noah@noahspannbauer.com', id: '', name: '', address: '', city: '', state: '', postalCode: '', email: '', phone: '' }; const methods = useForm({ defaultValues: defaultValues }); const [isDisabled, setIsDisabled] = useState(false); const [isError, setIsError] = useState(false); const onPeoplePickerSearch = async ( _event: React.SyntheticEvent, value: string ) => { setIsPeoplePickerLoading(true); try { if (value !== '') { const searchString: string = value; const accessToken: string = await getAccessToken(); const response: AxiosResponse = await httpClient.get( `api/user/search?search=${searchString}`, { headers: { Authorization: accessToken } } ); setPeoplePickerResults(response.data); } else { setPeoplePickerResults([]); } } catch (error) { console.log(error); } finally { setIsPeoplePickerLoading(false); } }; const onPeoplePickerSelectionChange = ( _event: React.SyntheticEvent, value: Person, _reason: string ) => { methods.setValue('id', value.userPrincipalName!.toString()); methods.setValue('name', value.displayName!.toString()); setSelectedPerson(value); }; const onCancel = () => { methods.reset(defaultValues); onOpenClose(FormMode.CANCEL); setSelectedPerson({ userPrincipalName: '', displayName: '' }); setIsDisabled(false) }; const onSubmit = async (data: unknown) => { try { setIsLoading(true); const accessToken: string = await getAccessToken(); if (!pilotId) { await httpClient.post(`api/pilots`, data, { headers: { Authorization: accessToken } }); } else { await httpClient.put(`api/pilots/pilot/${pilotId}`, data, { headers: { Authorization: accessToken } }) } 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 config = isAuthenticated ? { headers: { Authorization: await getAccessToken() } } : {}; const response: AxiosResponse = await httpClient.get( `api/pilots/pilot/${pilotId}`, config ); const pilot = response.data; setSelectedPerson({ userPrincipalName: pilot.id, displayName: pilot.name }); methods.reset(pilot); } catch (error) { console.log(error); } finally { setIsLoading(false); } }; if (pilotId) { getPilot(); } }, [pilotId]); return (
{`${mode.toString().toLowerCase().charAt(0).toUpperCase() + mode.toString().slice(1).toLowerCase()} Pilot`} Name * Address * ( )} /> City * ( )} /> State * ( )} /> Postal Code * ( )} /> Email ( )} /> Phone Number ( )} /> {mode.toString() !== FormMode.VIEW && ( )} {/* {pilotId && ( <>
Last Review
{ return ( { methods.setValue('lastReview', date); }} inputProps={{ value: field.value }} /> ); }} />
)} */} {/* {pilotId && ( <>
Medical
Class
{ return ( ); }} />
Expiration Date
{ return ( { methods.setValue('medicalExpiration', date); }} inputProps={{ value: field.value }} /> ); }} />
)} {pilotId && ( <>
Certificates
)} {pilotId && ( <>
Endorsements
)} */}
); }; export default PilotForm;