Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task/WG-219: Adding GET hook for grabbing the username #198

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
nathanfranklin marked this conversation as resolved.
Show resolved Hide resolved
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);
}
Loading