Skip to content

Commit

Permalink
Logout confirmation modal (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel-Therrien-Beslogic authored Dec 10, 2024
1 parent 487fdb6 commit 4dd13fc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/canopeum_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ on:
jobs:
GetDateTime:
runs-on: ubuntu-latest
if: ${{ inputs.deploy_frontend && inputs.deploy_backend }}
if: ${{ github.event_name == 'push' || (inputs.deploy_frontend && inputs.deploy_backend) }}
outputs:
DATETIME: ${{ steps.datetime.outputs.DATETIME }}
steps:
Expand All @@ -39,7 +39,7 @@ jobs:
BuildFrontend:
name: Build & Deploy Frontend
runs-on: ubuntu-latest
if: ${{ inputs.deploy_frontend }}
if: ${{ github.event_name == 'push' || inputs.deploy_frontend }}
needs: [GetDateTime]
defaults:
run:
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
BuildBackend:
name: Build & Deploy Backend
runs-on: ubuntu-latest
if: ${{ inputs.deploy_backend }}
if: ${{ github.event_name == 'push' || inputs.deploy_backend }}
needs: [GetDateTime]
defaults:
run:
Expand Down
4 changes: 2 additions & 2 deletions canopeum_frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Navbar = () => {
void changeLanguage(newLanguage)
}

const { isAuthenticated, logout } = useContext(AuthenticationContext)
const { isAuthenticated, showLogoutModal } = useContext(AuthenticationContext)

return (
<nav className='navbar sticky-top navbar-expand-lg navbar-dark bg-primary'>
Expand Down Expand Up @@ -127,7 +127,7 @@ const Navbar = () => {
? (
<button
className='nav-item btn btn-primary'
onClick={logout}
onClick={showLogoutModal}
type='button'
>
{translate('auth.log-out')}
Expand Down
23 changes: 21 additions & 2 deletions canopeum_frontend/src/components/context/AuthenticationContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { FunctionComponent, ReactNode } from 'react'
import { createContext, memo, useCallback, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'

import ConfirmationDialog from '@components/dialogs/ConfirmationDialog'
import useApiClient from '@hooks/ApiClientHook'
import type { User } from '@services/api'
import { STORAGE_ACCESS_TOKEN_KEY, STORAGE_REFRESH_TOKEN_KEY } from '@utils/auth.utils'
Expand All @@ -10,6 +12,7 @@ type IAuthenticationContext = {
authenticate: (user: User) => void,
updateUser: (user: User) => void,
logout: () => void,
showLogoutModal: () => void,
loadSession: () => void,
isAuthenticated: boolean,
isSessionLoaded: boolean,
Expand All @@ -21,6 +24,7 @@ export const AuthenticationContext = createContext<IAuthenticationContext>({
authenticate: (_: User) => {/* empty */},
updateUser: (_: User) => {/* empty */},
logout: () => {/* empty */},
showLogoutModal: () => {/* empty */},
loadSession: () => {/* empty */},
isAuthenticated: false,
isSessionLoaded: false,
Expand All @@ -32,10 +36,16 @@ const AuthenticationContextProvider: FunctionComponent<{ readonly children?: Rea
props => {
const [user, setUser] = useState<User>()
const [isSessionLoaded, setIsSessionLoaded] = useState(false)
const [isInitiated, setIsInitiated] = useState<boolean>(false)
const [isInitiated, setIsInitiated] = useState(false)
const [logoutModalShown, setLogoutModalShown] = useState(false)
const isInitiatedRef = useRef(isInitiated)

const { getApiClient } = useApiClient()
const { t } = useTranslation()

const handleLogoutModalClose = (proceed: boolean) => {
if (proceed) logout()
setLogoutModalShown(false)
}

const loadSession = useCallback(() => setIsSessionLoaded(true), [setIsSessionLoaded])

Expand Down Expand Up @@ -80,6 +90,8 @@ const AuthenticationContextProvider: FunctionComponent<{ readonly children?: Rea
setUser(undefined)
}, [setUser])

const showLogoutModal = () => setLogoutModalShown(true)

const context = useMemo<IAuthenticationContext>(() => (
{
currentUser: user,
Expand All @@ -90,6 +102,7 @@ const AuthenticationContextProvider: FunctionComponent<{ readonly children?: Rea
updateUser,
loadSession,
logout,
showLogoutModal,
}
), [initAuth, authenticate, updateUser, loadSession, user, logout, isSessionLoaded])

Expand All @@ -98,6 +111,12 @@ const AuthenticationContextProvider: FunctionComponent<{ readonly children?: Rea
value={context}
>
{props.children}
<ConfirmationDialog
actions={['ok', 'cancel']}
onClose={handleLogoutModalClose}
open={logoutModalShown}
title={t('auth.log-out-confirmation')}
/>
</AuthenticationContext.Provider>
)
},
Expand Down
12 changes: 4 additions & 8 deletions canopeum_frontend/src/components/dialogs/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ConfirmationAction = 'cancel' | 'delete' | 'ok'

type Props = {
readonly actions: ConfirmationAction[],
readonly children: ReactNode,
readonly children?: ReactNode,
readonly onClose: (proceed: boolean) => void,
readonly open: boolean,
readonly title: string,
Expand Down Expand Up @@ -57,14 +57,10 @@ const ConfirmationDialog = ({ actions, children, onClose, open, title }: Props)
}

return (
<Dialog fullWidth maxWidth='xs' onClose={() => onClose(false)} open={open}>
<Dialog onClose={() => onClose(false)} open={open}>
<DialogTitle>{title}</DialogTitle>
<DialogContent>
{children}
</DialogContent>
<DialogActions>
{actions.map(action => renderActionButton(action))}
</DialogActions>
<DialogContent>{children}</DialogContent>
<DialogActions>{actions.map(action => renderActionButton(action))}</DialogActions>
</Dialog>
)
}
Expand Down
1 change: 1 addition & 0 deletions canopeum_frontend/src/locale/en/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
'password-error-must-match': 'Passwords do not match',
'log-in': 'Log In',
'log-out': 'Log Out',
'log-out-confirmation': 'Are you sure you want to log out?',
'sign-up': 'Sign Up',
'create-account': 'Create Account',
'already-have-an-account': 'Already have an account?',
Expand Down
1 change: 1 addition & 0 deletions canopeum_frontend/src/locale/fr/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
'password-error-must-match': 'Passwords do not match',
'log-in': 'Se Connecter',
'log-out': 'Se Déconnecter',
'log-out-confirmation': 'Est-vous certain de vouloir vous déconnecter?',
'sign-up': "S'inscrire",
'create-account': 'Créer mon compte',
'already-have-an-account': 'Vous avez déjà un compte?',
Expand Down
4 changes: 2 additions & 2 deletions canopeum_frontend/src/pages/UserManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const tabs: { type: UserManagementTab, translationKey: string, roles?: RoleEnum[

const UserManagement = () => {
const { t: translate } = useTranslation()
const { logout, currentUser } = useContext(AuthenticationContext)
const { showLogoutModal, currentUser } = useContext(AuthenticationContext)
const navigate = useNavigate()
const location = useLocation()

const onTabClick = (tabPath: UserManagementTab) => {
if (tabPath === 'logout') {
logout()
showLogoutModal()

return
}
Expand Down

0 comments on commit 4dd13fc

Please sign in to comment.