import { useEffect, useState } from 'react'; import { useForm, Controller, FormProvider } from 'react-hook-form'; import { Button, Drawer, DrawerBody, DrawerHeader, DrawerFooter, Input, PeoplePicker, SaveIcon, StateSelect, Typography, XmarkIcon } from '@noahspan/noahspan-components'; import { IPilotFormProps } from './IPilotFormProps'; import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { useHttpClient } from '../../hooks/httpClient/UseHttpClient'; import { useAccessToken } from '../../hooks/accessToken/UseAcessToken'; import { useIsAuthenticated } from '@azure/msal-react'; export enum PilotFormMode { ADD = 'ADD', EDIT = 'EDIT', VIEW = 'VIEW', CANCEL = 'CANCEL' } const PilotForm: React.FC = ({ pilotId, isDrawerOpen, mode, onOpenClose }: IPilotFormProps) => { const httpClient: AxiosInstance = useHttpClient(); const [peoplePickerResults, setPeoplePickerResults] = useState([]); const [isPeoplePickerLoading, setIsPeoplePickerLoading] = useState(false); const [isLoading, setIsLoading] = useState(false); const { getAccessToken } = useAccessToken(); const isAuthenticated = useIsAuthenticated(); const defaultValues = { partitionKey: '', rowKey: '', id: '', name: '', address: '', city: '', state: '', postalCode: '', email: '', phone: '' }; const methods = useForm({ defaultValues: defaultValues }); const [isDisabled, setIsDisabled] = useState(false); const handlePeoplePickerOnClick = ( event: React.MouseEvent ) => { const divElement: HTMLDivElement = event.target as HTMLDivElement; methods.setValue('id', divElement.id); methods.setValue('name', divElement.textContent!); setPeoplePickerResults([]); }; const handlePeoplePickerOnChange = async ( event: React.ChangeEvent ) => { setIsPeoplePickerLoading(true); try { methods.setValue('name', event.target.value); const searchString: string = event.target.value; const accessToken: string = await getAccessToken(); const response: AxiosResponse = await httpClient.get( `api/personSearch?search=${searchString}`, { headers: { Authorization: accessToken } } ); console.log(response); setPeoplePickerResults(response.data); } catch (error) { console.log(error); } finally { setIsPeoplePickerLoading(false); } }; const onCancel = () => { methods.reset(defaultValues); onOpenClose(PilotFormMode.CANCEL); }; const onSubmit = async (data: unknown) => { try { setIsLoading(true); const accessToken: string = await getAccessToken(); const response: AxiosResponse = await httpClient.post( `api/pilots`, data, { headers: { Authorization: accessToken } } ); if (response) console.log(response); } catch (error) { if (axios.isAxiosError(error)) { const errResp = error.response; console.log(errResp?.data.message); } else { } } finally { setIsLoading(false); } }; useEffect(() => { if (mode === PilotFormMode.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/${pilotId}`, config ); const pilot = response.data; console.log(pilot); // methods.setValue('blah', pilot.value) methods.reset(pilot); console.log(methods.getValues()); } catch (error) { } finally { setIsLoading(false); } }; if (pilotId) { getPilot(); } }, [pilotId]); return (
Name *
( handlePeoplePickerOnChange(event), error: methods.formState.errors.name ? true : false, helperText: methods.formState.errors.name ? methods.formState.errors.name.message?.toString() : undefined, value: value }} listItemProps={{ children: null, onClick: handlePeoplePickerOnClick }} loading={isPeoplePickerLoading} data-testid="pilot-form-people-picker" /> )} />
{isAuthenticated && ( <>
Address *
( )} />
)} {isAuthenticated && ( <>
City *
( )} />
)} {isAuthenticated && ( <>
State *
( )} />
)} {isAuthenticated && ( <>
Postal Code *
( )} />
)} {isAuthenticated && ( <>
Email
( )} />
)} {isAuthenticated && ( <>
Phone Number
( )} />
)} {/* {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
)} */}
<> {mode !== PilotFormMode.VIEW && (
)}
); }; export default PilotForm;