Refactoring

This commit is contained in:
2025-01-19 15:08:16 -06:00
parent d0dcf1b2c9
commit b0be61f224

View File

@@ -20,7 +20,7 @@ const SiteNav: React.FC<unknown> = () => {
const httpClient: AxiosInstance = useHttpClient(); const httpClient: AxiosInstance = useHttpClient();
const appContext = useAppContext(); const appContext = useAppContext();
const isAuthenticated = useIsAuthenticated(); const isAuthenticated = useIsAuthenticated();
const { getAccessToken } = useAccessToken(); // const { getAccessToken } = useAccessToken();
const { instance } = useMsal(); const { instance } = useMsal();
const navigate = useNavigate(); const navigate = useNavigate();
const pages = [ const pages = [
@@ -50,42 +50,42 @@ const SiteNav: React.FC<unknown> = () => {
window.location.reload(); window.location.reload();
} }
const getUserProfile = async (accessToken: string): Promise<User> => { // const getUserProfile = async (accessToken: string): Promise<User> => {
try { // try {
const response: AxiosResponse = await httpClient.get(`api/userProfile`, { // const response: AxiosResponse = await httpClient.get(`api/userProfile`, {
headers: { // headers: {
Authorization: accessToken // Authorization: accessToken
} // }
}); // });
const userProfile: User = response.data; // const userProfile: User = response.data;
return userProfile; // return userProfile;
} catch (error) { // } catch (error) {
throw new Error(); // throw new Error();
} // }
}; // };
const getUserPhoto = async (accessToken: string): Promise<string> => { // const getUserPhoto = async (accessToken: string): Promise<string> => {
try { // try {
const response: AxiosResponse = await httpClient.get(`api/userPhoto`, { // const response: AxiosResponse = await httpClient.get(`api/userPhoto`, {
headers: { // headers: {
Authorization: accessToken // Authorization: accessToken
}, // },
responseType: 'arraybuffer' // responseType: 'arraybuffer'
}); // });
const arrayBufferView = new Uint8Array(response.data); // const arrayBufferView = new Uint8Array(response.data);
const blob = new Blob([arrayBufferView], { type: 'image/png' }); // const blob = new Blob([arrayBufferView], { type: 'image/png' });
const imageUrl = window.URL.createObjectURL(blob); // const imageUrl = window.URL.createObjectURL(blob);
return imageUrl; // return imageUrl;
} catch (error) { // } catch (error) {
console.log(error); // console.log(error);
throw new Error(); // throw new Error();
} // }
}; // };
const handlePageClick = (url: string) => { // const handlePageClick = (url: string) => {
navigate(url); // navigate(url);
}; // };
const Settings = () => { const Settings = () => {
return ( return (
@@ -98,31 +98,31 @@ const SiteNav: React.FC<unknown> = () => {
); );
}; };
useEffect(() => { // useEffect(() => {
const setUserProfile = async () => { // const setUserProfile = async () => {
try { // try {
const accessToken: string = await getAccessToken(); // const accessToken: string = await getAccessToken();
const userProfile = await getUserProfile(accessToken); // const userProfile = await getUserProfile(accessToken);
const userPhoto = await getUserPhoto(accessToken); // const userPhoto = await getUserPhoto(accessToken);
setUserPhoto(userPhoto); // setUserPhoto(userPhoto);
appContext.dispatch({ // appContext.dispatch({
type: 'SET_USER_PROFILE', // type: 'SET_USER_PROFILE',
payload: userProfile // payload: userProfile
}); // });
} catch (error) { // } catch (error) {
console.log(error); // console.log(error);
} // }
}; // };
if ( // if (
isAuthenticated && // isAuthenticated &&
Object.keys(appContext.state.userProfile).length === 0 // Object.keys(appContext.state.userProfile).length === 0
) { // ) {
setUserProfile(); // setUserProfile();
} // }
}, [isAuthenticated]); // }, [isAuthenticated]);
return ( return (
<Navbar <Navbar