From 1abec709b0c8a2dfe0570ec98ca1310d6b2de585 Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Wed, 8 Mar 2023 16:28:29 +0700 Subject: [PATCH 1/9] [#19] Add header element and pass user instead of context for Outlet in router --- src/components/Header/index.tsx | 16 ++++++++++++++++ src/components/PrivateRoute/index.tsx | 22 ++++++++++++---------- src/screens/Home/index.tsx | 6 ------ 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/components/Header/index.tsx diff --git a/src/components/Header/index.tsx b/src/components/Header/index.tsx new file mode 100644 index 0000000..ef1f11f --- /dev/null +++ b/src/components/Header/index.tsx @@ -0,0 +1,16 @@ +import { User } from 'types/User'; + +type HeaderProps = { + user: User; +}; + +function Header({ user }: HeaderProps) { + return ( +
+
Test logo for {user.email}
+
Test image for {user.name}
+
+ ); +} + +export default Header; diff --git a/src/components/PrivateRoute/index.tsx b/src/components/PrivateRoute/index.tsx index 52c7c33..f2d783e 100644 --- a/src/components/PrivateRoute/index.tsx +++ b/src/components/PrivateRoute/index.tsx @@ -1,11 +1,10 @@ import { useState, useEffect } from 'react'; -import { Navigate, Outlet, useOutletContext } from 'react-router-dom'; +import { Navigate, Outlet } from 'react-router-dom'; +import Header from 'components/Header'; import { getItem } from 'helpers/localStorage'; import type { User } from 'types/User'; -type ContextType = User; - import { LOGIN_URL } from '../../constants'; function PrivateRoute() { @@ -16,7 +15,7 @@ function PrivateRoute() { const fetchCurrentUser = async () => { const userProfile = getItem('UserProfile'); if (userProfile?.user) { - setUser({ ...userProfile.user }); + setUser({ name: userProfile.user.name, email: userProfile.user.email, avatarUrl: userProfile.user.avatar_url }); } setLoading(false); @@ -28,11 +27,14 @@ function PrivateRoute() { return

Loading...

; } - return user ? : ; + return user ? ( + <> +
+ + + ) : ( + + ); } -export default PrivateRoute; - -export function useUser() { - return useOutletContext(); -} +export default PrivateRoutes; diff --git a/src/screens/Home/index.tsx b/src/screens/Home/index.tsx index f15062e..c87cb3e 100644 --- a/src/screens/Home/index.tsx +++ b/src/screens/Home/index.tsx @@ -1,15 +1,9 @@ -import { useUser } from 'components/PrivateRoute'; - const HomeScreen = (): JSX.Element => { - const user = useUser(); - return ( <>
Home Screen
- {/* TODO: Remove when header implemented in #19 */} -
{`${user?.name} - ${user?.email} - ${user?.avatarUrl}`}
); }; From 5ce8751aa207f9a7c1e875902c408654bd892a4d Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Thu, 9 Mar 2023 09:46:42 +0700 Subject: [PATCH 2/9] [#19] Display user information in header and add sidebar for logging the user out --- package-lock.json | 2 +- src/adapters/authAdapter.ts | 11 ++++++ src/components/Header/index.tsx | 22 ++++++++++-- src/components/Sidebar/Sidebar.module.scss | 5 +++ src/components/Sidebar/index.tsx | 39 ++++++++++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/components/Sidebar/Sidebar.module.scss create mode 100644 src/components/Sidebar/index.tsx diff --git a/package-lock.json b/package-lock.json index 3a221b0..b121a2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,7 +38,7 @@ "danger": "10.9.0", "danger-plugin-istanbul-coverage": "1.6.2", "eslint": "8.11.0", - "jest-localstorage-mock": "^2.4.26", + "jest-localstorage-mock": "2.4.26", "nock": "13.3.0", "postcss": "8.4.21", "postcss-import": "14.1.0", diff --git a/src/adapters/authAdapter.ts b/src/adapters/authAdapter.ts index e377e0e..3ba83b0 100644 --- a/src/adapters/authAdapter.ts +++ b/src/adapters/authAdapter.ts @@ -36,6 +36,17 @@ class AuthAdapter extends BaseAdapter { return this.prototype.postRequest('oauth/token', { data: requestParams }); } + static logout(accessToken: string) { + /* eslint-disable camelcase */ + const requestParams = { + ...commonParams, + token: accessToken, + }; + /* eslint-enable camelcase */ + + return this.prototype.postRequest('oauth/revoke', { data: requestParams }); + } + static getUser() { return this.prototype.getRequest('me', {}); } diff --git a/src/components/Header/index.tsx b/src/components/Header/index.tsx index ef1f11f..1e0bf4a 100644 --- a/src/components/Header/index.tsx +++ b/src/components/Header/index.tsx @@ -1,3 +1,7 @@ +import { useState } from 'react'; + +import logo from 'assets/images/logo.svg'; +import Sidebar from 'components/Sidebar'; import { User } from 'types/User'; type HeaderProps = { @@ -5,10 +9,22 @@ type HeaderProps = { }; function Header({ user }: HeaderProps) { + const [sidebarVisible, setSidebarVisible] = useState(false); + return ( -
-
Test logo for {user.email}
-
Test image for {user.name}
+
+
+ logo +
+
setSidebarVisible(!sidebarVisible)} role="presentation"> + {sidebarVisible ? ( + + ) : ( +
+ profile +
+ )} +
); } diff --git a/src/components/Sidebar/Sidebar.module.scss b/src/components/Sidebar/Sidebar.module.scss new file mode 100644 index 0000000..99fcc90 --- /dev/null +++ b/src/components/Sidebar/Sidebar.module.scss @@ -0,0 +1,5 @@ +.sidebar { + border: 5px bla solid; + + background: rgba(30, 30, 30, 0.9); +} diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx new file mode 100644 index 0000000..f6f1a86 --- /dev/null +++ b/src/components/Sidebar/index.tsx @@ -0,0 +1,39 @@ +import { useNavigate } from 'react-router-dom'; + +import AuthAdapter from 'adapters/authAdapter'; +import { getItem, clearItem } from 'helpers/localStorage'; +import { User } from 'types/User'; + +import styles from './Sidebar.module.scss'; +import { LOGIN_URL } from '../../constants'; + +type SidebarProps = { + user: User; +}; + +function Sidebar({ user }: SidebarProps) { + const navigate = useNavigate(); + + const performLogout = async (e: React.SyntheticEvent) => { + e.stopPropagation(); + + const accessToken = getItem('UserProfile')?.auth.access_token; + await AuthAdapter.logout(accessToken); + clearItem('UserProfile'); + navigate(LOGIN_URL); + }; + + return ( + + ); +} + +export default Sidebar; From 78b5295bfaef9e66d5f023a8cd4b79ec23bcd6ad Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Mon, 13 Mar 2023 12:11:15 +0700 Subject: [PATCH 3/9] [#19] Fix Logout button positioning to be left aligned in sidebar --- src/components/Sidebar/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index f6f1a86..d14ff81 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -29,7 +29,7 @@ function Sidebar({ user }: SidebarProps) { {user.name} profile - From d2bf060ae98ad929d3c273673da3f56c88a1b39e Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Mon, 20 Mar 2023 17:46:52 +0700 Subject: [PATCH 4/9] [#19] Add some tests to Sidebar component and auth logout --- src/adapters/authAdapter.test.ts | 18 ++++++++++ src/components/Sidebar/index.test.tsx | 52 +++++++++++++++++++++++++++ src/components/Sidebar/index.tsx | 13 +++++-- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/components/Sidebar/index.test.tsx diff --git a/src/adapters/authAdapter.test.ts b/src/adapters/authAdapter.test.ts index a676ea5..5448c81 100644 --- a/src/adapters/authAdapter.test.ts +++ b/src/adapters/authAdapter.test.ts @@ -81,4 +81,22 @@ describe('AuthAdapter', () => { expect(scope.isDone()).toBe(true); }); }); + + describe('logout', () => { + test('The logout endpoint is called', async () => { + const token = 'access_token'; + + const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) + .defaultReplyHeaders({ + 'access-control-allow-origin': '*', + 'access-control-allow-credentials': 'true', + }) + .post('/oauth/revoke') + .reply(200); + + expect(scope.isDone()).toBe(false); + expect(await AuthAdapter.logout(token)); + expect(scope.isDone()).toBe(true); + }); + }); }); diff --git a/src/components/Sidebar/index.test.tsx b/src/components/Sidebar/index.test.tsx new file mode 100644 index 0000000..175bfbf --- /dev/null +++ b/src/components/Sidebar/index.test.tsx @@ -0,0 +1,52 @@ +/* eslint-disable camelcase */ +import { BrowserRouter } from 'react-router-dom'; + +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import AuthAdapter from 'adapters/authAdapter'; +import { User } from 'types/User'; + +import Sidebar from '.'; +import { setItem } from '../../helpers/localStorage'; + +const mockTokenData = { + access_token: 'test_access_token', + refresh_token: 'test_refresh_token', + token_type: 'Bearer', + expires_in: 7200, + created_at: 1677045997, +}; + +const mockUserProfileData = { + email: 'testemail@gmail.com', + name: 'TestName', + avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d', +}; + +describe('Sidebar', () => { + const user: User = { name: 'Test User', email: 'testemail@email.com', avatarUrl: 'an-avatar-url' }; + + test("renders a sidebar on the page with the user's name, profile image", () => { + render(, { wrapper: BrowserRouter }); + + expect(screen.getByTestId('username')).toHaveTextContent(user.name); + + const profileImage = screen.getByAltText('profile') as HTMLImageElement; + expect(profileImage.src).toContain('an-avatar-url'); + }); + + test('renders a sidebar on the page with a logout button that when clicked, calls Logout adapter', async () => { + setItem('UserProfile', { auth: mockTokenData, user: mockUserProfileData }); + + const mockLogout = jest.spyOn(AuthAdapter, 'logout'); + + render(, { wrapper: BrowserRouter }); + + const submitButton = screen.getByRole('button', { name: 'Logout' }); + + await userEvent.click(submitButton); + + expect(mockLogout).toHaveBeenCalled(); + }); +}); diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index d14ff81..f3cd33c 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -26,8 +26,17 @@ function Sidebar({ user }: SidebarProps) { return (