Skip to content

Commit

Permalink
Merge pull request #4644 from beyondessential/waitp-1205-logout
Browse files Browse the repository at this point in the history
WAITP-1205: Logout button
  • Loading branch information
tcaiger authored Jun 16, 2023
2 parents 3ffd22b + 907e92d commit 3cf3581
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/tupaia-web/src/api/mutations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*
*/

export * from './useLogin';
export * from './useOneTimeLogin';
export { useLogin } from './useLogin';
export { useLogout } from './useLogout';
export * from './useRegister';
export * from './useResendVerificationEmail';
export * from './useResetPassword';
Expand Down
17 changes: 17 additions & 0 deletions packages/tupaia-web/src/api/mutations/useLogout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

import { useMutation, useQueryClient } from 'react-query';
import { get } from '../api';

export const useLogout = () => {
const queryClient = useQueryClient();

return useMutation('logout', () => get('logout'), {
onSuccess: () => {
queryClient.invalidateQueries();
},
});
};
2 changes: 1 addition & 1 deletion packages/tupaia-web/src/api/queries/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const useUser = () => {

return {
...userResponse,
isLoggedIn: data?.email ? true : false, // when the name is public
isLoggedIn: !!data?.email,
};
};
14 changes: 12 additions & 2 deletions packages/tupaia-web/src/layout/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Button, useTheme } from '@material-ui/core';
import styled from 'styled-components';
import { useParams } from 'react-router';
import { useLandingPage, useUser } from '../../api/queries';
import { useLogout } from '../../api/mutations';
import { PopoverMenu } from './PopoverMenu';
import { DrawerMenu } from './DrawerMenu';
import { MenuItem } from './MenuList';
Expand Down Expand Up @@ -37,6 +38,7 @@ const MenuIcon = styled(MuiMenuIcon)`
`;

export const UserMenu = () => {
const { mutate: logout } = useLogout();
const [menuOpen, setMenuOpen] = useState(false);
const toggleUserMenu = () => {
setMenuOpen(!menuOpen);
Expand Down Expand Up @@ -68,6 +70,12 @@ export const UserMenu = () => {
</BaseMenuItem>
);

const Logout = (
<BaseMenuItem key="logout" onClick={logout}>
Logout
</BaseMenuItem>
);

const ViewProjects = (
<BaseMenuItem key="projects" modal={MODAL_ROUTES.PROJECTS}>
View projects
Expand All @@ -78,9 +86,11 @@ export const UserMenu = () => {
<BaseMenuItem modal={MODAL_ROUTES.RESET_PASSWORD}>Change password</BaseMenuItem>
);
// The custom landing pages need different menu items to the other views
const customLandingPageMenuItems = isLoggedIn ? [VisitMainSite, ChangePassword] : [VisitMainSite];
const customLandingPageMenuItems = isLoggedIn
? [VisitMainSite, ChangePassword, Logout]
: [VisitMainSite];

const baseMenuItems = isLoggedIn ? [ViewProjects, ChangePassword] : [ViewProjects];
const baseMenuItems = isLoggedIn ? [ViewProjects, ChangePassword, Logout] : [ViewProjects];

const menuItems = isLandingPage ? customLandingPageMenuItems : baseMenuItems;

Expand Down

0 comments on commit 3cf3581

Please sign in to comment.