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

Logout confirmation modal #325

Merged
merged 1 commit into from
Dec 10, 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
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
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,17 +12,19 @@
authenticate: (user: User) => void,
updateUser: (user: User) => void,
logout: () => void,
showLogoutModal: () => void,
loadSession: () => void,
isAuthenticated: boolean,
isSessionLoaded: boolean,
currentUser: User | undefined,
}

export const AuthenticationContext = createContext<IAuthenticationContext>({

Check warning on line 22 in canopeum_frontend/src/components/context/AuthenticationContext.tsx

View workflow job for this annotation

GitHub Actions / Lint-Autofixes

Fast refresh only works when a file only exports components. Move your React context(s) to a separate file
initAuth: () => new Promise(() => {/* empty */}),
authenticate: (_: User) => {/* empty */},
updateUser: (_: User) => {/* empty */},
logout: () => {/* empty */},
showLogoutModal: () => {/* empty */},
loadSession: () => {/* empty */},
isAuthenticated: false,
isSessionLoaded: false,
Expand All @@ -32,10 +36,16 @@
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 @@
setUser(undefined)
}, [setUser])

const showLogoutModal = () => setLogoutModalShown(true)

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

Expand All @@ -98,6 +111,12 @@
value={context}
>
{props.children}
<ConfirmationDialog
actions={['ok', 'cancel']}
onClose={handleLogoutModalClose}
open={logoutModalShown}
title={t('auth.log-out-confirmation')}
/>
</AuthenticationContext.Provider>
)
},
Expand Down
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
Loading