-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic proof of concept using axios api package
- Loading branch information
Showing
6 changed files
with
3,222 additions
and
1,825 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react" | ||
|
||
import { | ||
RiAccountCircleFill, | ||
} from "@remixicon/react" | ||
|
||
import { useUserMe } from "../hooks/user" | ||
|
||
export default function UserMenu() { | ||
const { data: user } = useUserMe() | ||
|
||
return ( | ||
<div className="dropdown"> | ||
<button | ||
className="btn btn-link dropdown-toggle" | ||
type="button" | ||
id="userMenu" | ||
data-toggle="dropdown" | ||
aria-expanded="false" | ||
> | ||
<RiAccountCircleFill size={24} /> | ||
</button> | ||
<ul className="dropdown-menu" aria-labelledby="userMenu"> | ||
{user?.is_authenticated ? ( | ||
<> | ||
<li> | ||
{user.first_name} {user.last_name} | ||
</li> | ||
<li> | ||
<a className="dropdown-item" href="https://learn.dev.c4103.com/dashboard" target="_blank"> | ||
Check failure on line 30 in www/assets/js/components/UserMenu.tsx
|
||
Dashboard | ||
</a> | ||
</li> | ||
<li> | ||
<a className="dropdown-item" href="https://learn.dev.c4103.com/logout" target="_blank"> | ||
Check failure on line 35 in www/assets/js/components/UserMenu.tsx
|
||
Logout | ||
</a> | ||
</li> | ||
</> | ||
) : ( | ||
<li> | ||
<a className="dropdown-item" href="https://learn.dev.c4103.com/login" target="_blank"> | ||
Check failure on line 42 in www/assets/js/components/UserMenu.tsx
|
||
Login | ||
</a> | ||
</li> | ||
)} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useQuery } from "@tanstack/react-query" | ||
import { User as UserApi, UsersApi } from "@mitodl/open-api-axios/v0" | ||
|
||
interface User extends Partial<UserApi> { | ||
is_authenticated: boolean | ||
} | ||
|
||
const useUserMe = () => | ||
useQuery({ | ||
queryKey: ["userMe"], | ||
queryFn: async (): Promise<User> => { | ||
try { | ||
const usersApi = new UsersApi(undefined, "https://api.learn.dev.c4103.com/".replace(/\/+$/, ""), ) | ||
usersApi.axios.defaults.withCredentials = true | ||
const response = await usersApi.usersMeRetrieve() | ||
return { | ||
is_authenticated: true, | ||
...response.data, | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error: any) { | ||
if (error.response?.status === 403) { | ||
return { | ||
is_authenticated: false, | ||
} | ||
} | ||
throw error | ||
} | ||
}, | ||
}) | ||
|
||
export { useUserMe } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.