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

refactor(coral): Replace hacky notification solution in ProfileDropdown #1824

Merged
merged 27 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
81cc04a
update: install @aivenio/aquarium 1.39.0
dotarjun Sep 29, 2023
bf9fef3
feat: dismiss toast programmatically in case of logout error
dotarjun Sep 29, 2023
c9bce5b
refactor: add comment describing logout toast being dismissed program…
dotarjun Sep 29, 2023
a9c43a6
refactor: update test to check for useToastContext
dotarjun Sep 29, 2023
aaef069
refactor: fix mockedUseToastContext
dotarjun Oct 3, 2023
a9f10eb
refactor: move dismiss toast inside "error is not 401" condition
dotarjun Oct 4, 2023
877bc62
feat: add test for dismiss toast
dotarjun Oct 4, 2023
87705ae
refactor: rename function for better readability
dotarjun Oct 4, 2023
d59b77d
Merge branch 'main' into fixes-issue-1810
Oct 4, 2023
410d9de
refactor: update snapshots
dotarjun Oct 5, 2023
46111d6
refactor: move useToastContext within provider
dotarjun Oct 5, 2023
2b44426
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 5, 2023
b254a50
bug: change useToastContext return type to array from object
dotarjun Oct 5, 2023
ef8558d
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 9, 2023
9ed34b4
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 18, 2023
10b1c02
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 23, 2023
f9d7a28
refactor: fix pnpm-lock file
dotarjun Oct 24, 2023
8d0a1b6
refactor: update snapshots
dotarjun Oct 24, 2023
5902969
Merge branch 'main' into fixes-issue-1810
Oct 24, 2023
5b163ef
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 24, 2023
04f6367
Merge branch 'main' into fixes-issue-1810
Oct 24, 2023
6400ea7
refactor: move toast variables out of onDropdownClick
dotarjun Oct 26, 2023
b2197d8
Merge branch 'main' into fixes-issue-1810
dotarjun Oct 26, 2023
e72407b
Merge branch 'main' into fixes-issue-1810
Oct 27, 2023
fbdc575
Merge branch 'main' into fixes-issue-1810
Oct 31, 2023
b2b9fd3
fix(coral): Fix tests which were mocking useToast instead of useToast…
Oct 31, 2023
09e101a
Merge branch 'main' into fixes-issue-1810
Oct 31, 2023
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
13 changes: 9 additions & 4 deletions coral/src/app/layout/header/ProfileDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ jest.mock("src/domain/auth-user/auth-user-api");

const mockLogoutUser = logoutUser as jest.MockedFunction<typeof logoutUser>;

const mockToast = jest.fn();
const mockDismiss = jest.fn();

const mockAuthUser = jest.fn();
jest.mock("src/app/context-provider/AuthProvider", () => ({
useAuthContext: () => mockAuthUser(),
}));

const mockedUseToast = jest.fn();
jest.mock("@aivenio/aquarium", () => ({
...jest.requireActual("@aivenio/aquarium"),
useToast: () => mockedUseToast,
useToastContext: () => [mockToast, mockDismiss],
}));

describe("ProfileDropdown", () => {
const testUser: AuthUser = {
teamname: "new team",
Expand Down Expand Up @@ -175,7 +178,7 @@ describe("ProfileDropdown", () => {
const logout = screen.getByRole("menuitem", { name: "Log out" });
await user.click(logout);

expect(mockedUseToast).toHaveBeenCalledWith(
expect(mockToast).toHaveBeenCalledWith(
expect.objectContaining({
message: "You are being logged out of Klaw...",
})
Expand Down Expand Up @@ -246,7 +249,9 @@ describe("ProfileDropdown", () => {
const logout = screen.getByRole("menuitem", { name: "Log out" });
await user.click(logout);

expect(mockedUseToast).toHaveBeenCalledWith(
expect(mockDismiss).toHaveBeenCalledWith("logout");

expect(mockToast).toHaveBeenCalledWith(
expect.objectContaining({
message:
"Something went wrong in the log out process. Please try again or contact your administrator.",
Expand Down
10 changes: 6 additions & 4 deletions coral/src/app/layout/header/ProfileDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DropdownMenu,
Typography,
Icon,
useToast,
useToastContext,
} from "@aivenio/aquarium";
import user from "@aivenio/aquarium/dist/src/icons/user";
import logOut from "@aivenio/aquarium/dist/src/icons/logOut";
Expand Down Expand Up @@ -34,21 +34,24 @@ const menuItems: MenuItem[] = [
const LOGOUT_KEY = "logout";
function ProfileDropdown() {
const authUser = useAuthContext();
const toast = useToast();

function navigateToAngular(path: string) {
window.location.assign(`${window.origin}${path}`);
}

function onDropdownClick(actionKey: string | number) {
const [toast, dismiss] = useToastContext();
Copy link
Contributor

@mathieu-anderson mathieu-anderson Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason to move this in the body of the onDropdownClick function?

Because in this state, clicking Logout throws an error and does nothing, as hooks can only be called in the body of a component:

Screen.Recording.2023-10-24.at.15.36.43.mov

I think moving it back where it was before (under const authUser = useAuthContext();) should fix the issue and still preserve the behaviour we want.

if (actionKey === LOGOUT_KEY) {
toast({
id: "logout",
message: "You are being logged out of Klaw...",
position: "bottom-left",
variant: "default",
duration: 30 * 1000,
});
logoutUser().catch((error) => {
// dismiss toast in case logout fails
dotarjun marked this conversation as resolved.
Show resolved Hide resolved
if (error.status !== 401) {
dismiss("logout");
toast({
message:
"Something went wrong in the log out process. Please try again or contact your administrator.",
Expand All @@ -60,7 +63,6 @@ function ProfileDropdown() {
window.location.assign(`${window.origin}/login`);
}
});

return;
} else {
const selectedItem = menuItems[actionKey as number];
Expand Down
Loading