Skip to content

Commit

Permalink
task/WG-219: Adding GET hook for grabbing the username (#198)
Browse files Browse the repository at this point in the history
* task/WG-219: Adding GET hook for grabbinig the username

* added transform option to useGet hook to help with tranforming API responses

* refactoring transform param

---------

Co-authored-by: Taylor Grafft <[email protected]>
Co-authored-by: Taylor Grafft <[email protected]>
Co-authored-by: Taylor Grafft <[email protected]>
  • Loading branch information
4 people authored Jan 26, 2024
1 parent 9676baf commit 2643e39
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
17 changes: 17 additions & 0 deletions react/src/hooks/user/useAuthenticatedUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { UseQueryResult } from 'react-query';
import { ApiService, AuthenticatedUser } from '../../types';
import { useGet } from '../../requests';

const useAuthenticatedUser = (): UseQueryResult<AuthenticatedUser> => {
return useGet<AuthenticatedUser>({
endpoint: '/oauth2/userinfo?schema=openid',
key: ['username'],
apiService: ApiService.Tapis,
transform: (data) => ({
username: data.name,
email: data.email,
}),
});
};

export default useAuthenticatedUser;
23 changes: 15 additions & 8 deletions react/src/pages/MainMenu/MainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ import {
SectionHeader,
} from '../../core-components';
import { useProjects } from '../../hooks';
import useAuthenticatedUser from '../../hooks/user/useAuthenticatedUser';

function MainMenu() {
const { data, isLoading, error } = useProjects();
if (isLoading) {
const {
data: userData,
isLoading: isUserLoading,
error: userError,
} = useAuthenticatedUser();
if (isLoading || isUserLoading) {
return (
<>
<SectionHeader isNestedHeader>Main Menu</SectionHeader>
<LoadingSpinner />
</>
);
}
if (error) {
return (
<>
<SectionHeader isNestedHeader>Main Menu</SectionHeader>
<InlineMessage type="error">Unable to retrieve projects.</InlineMessage>
</>
);
if (error || userError) {
<>
<SectionHeader isNestedHeader>Main Menu</SectionHeader>
<InlineMessage type="error">Unable to retrieve projects.</InlineMessage>
</>;
}
return (
<>
<SectionHeader isNestedHeader>Main Menu</SectionHeader>
<InlineMessage type="success">
Welcome, {userData?.username || 'User'}
</InlineMessage>

<table>
<thead>Projects</thead>
Expand Down
2 changes: 1 addition & 1 deletion react/src/redux/authSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const authSlice = createSlice({
geoapi.endpoints.getGeoapiUserInfo.matchFulfilled,
(state, action: PayloadAction<any>) => {
const u: any = {
name: action.payload.name,
username: action.payload.name,
email: action.payload.email,
};
state.user = u;
Expand Down
18 changes: 7 additions & 11 deletions react/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type UseGetParams<ResponseType> = {
UseQueryOptions<ResponseType, AxiosError>,
'queryKey' | 'queryFn'
>;
transform?: (data: any) => ResponseType;
apiService?: ApiService;
};

Expand All @@ -62,26 +63,21 @@ export function useGet<ResponseType>({
key,
options = {},
apiService = ApiService.Geoapi,
transform,
}: UseGetParams<ResponseType>) {
const client = axios;
const state = store.getState();
const configuration = useAppConfiguration();

const baseUrl = getBaseApiUrl(apiService, configuration);
const headers = getHeaders(apiService, configuration, state.auth);

/* TODO_REACT Send analytics-related params to projects endpoint only (until we use headers
again in https://tacc-main.atlassian.net/browse/WG-192) */
again in https://tacc-main.atlassian.net/browse/WG-192) */

const getUtil = async () => {
const request = await client.get<ResponseType>(
`${baseUrl}${endpoint}`,

{
headers: headers,
}
);
return request.data;
const request = await client.get(`${baseUrl}${endpoint}`, { headers });
return transform ? transform(request.data) : request.data;
};
return useQuery<ResponseType, AxiosError>(key, () => getUtil(), options);

return useQuery<ResponseType, AxiosError>(key, getUtil, options);
}

0 comments on commit 2643e39

Please sign in to comment.