Files
noahspan-flying/app/src/hooks/pilots/UsePilots.tsx
noahspannbauer 52b29163e6 adding new logbook entry (#24)
* adding new logbook entry

* adding new logbook entry
2024-11-17 07:56:01 -06:00

53 lines
1.3 KiB
TypeScript

import { useEffect, useState } from 'react';
import { AxiosInstance, AxiosResponse } from 'axios';
import { useHttpClient } from '../../hooks/httpClient/UseHttpClient';
import { useAccessToken } from '../../hooks/accessToken/UseAcessToken';
import { useIsAuthenticated } from '@azure/msal-react';
export const usePilots = () => {
const [pilots, setPilots] = useState<any[]>();
const httpClient: AxiosInstance = useHttpClient();
const { getAccessToken } = useAccessToken();
const isAuthenticated = useIsAuthenticated();
const getPilot = async (pilotId: string) => {
try {
const config = isAuthenticated
? { headers: { Authorization: await getAccessToken() } }
: {};
const response: AxiosResponse = await httpClient.get(
`api/pilots/${pilotId}`
);
return response.data;
} catch (error) {
return error;
}
};
useEffect(() => {
const getPilots = async () => {
try {
const config = isAuthenticated
? { headers: { Authorization: await getAccessToken() } }
: {};
const response: AxiosResponse = await httpClient.get(
`api/pilots`,
config
);
setPilots(response.data);
} catch (error) {
return error;
}
};
getPilots();
}, []);
return {
getPilot,
pilots
};
};