From 9c2a811d04e8d18db74e859763a6308ea391a566 Mon Sep 17 00:00:00 2001 From: shahin-hq <132887516+shahin-hq@users.noreply.github.com> Date: Thu, 14 Nov 2024 19:45:13 +0400 Subject: [PATCH] refactor: navigate to dashboard after creating profile (#817) --- .../CreateProfile/CreateProfile.test.tsx | 22 +++++++++++++++++++ .../pages/CreateProfile/CreateProfile.tsx | 16 +++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/domains/profile/pages/CreateProfile/CreateProfile.test.tsx b/src/domains/profile/pages/CreateProfile/CreateProfile.test.tsx index 1010c46a7d..3cf5c42fcc 100755 --- a/src/domains/profile/pages/CreateProfile/CreateProfile.test.tsx +++ b/src/domains/profile/pages/CreateProfile/CreateProfile.test.tsx @@ -190,6 +190,28 @@ describe("CreateProfile", () => { expect(profile.usesPassword()).toBe(false); }); + it("should navigate to dashboard after creating profile", async () => { + const history = createHashHistory(); + + render(, { history }); + + await userEvent.type(nameInput(), "test profile 2"); + + await userEvent.click(screen.getByRole("checkbox")); + + await waitFor(() => expect(submitButton()).toBeEnabled()); + + const historySpy = vi.spyOn(history, "push"); + + await userEvent.click(submitButton()); + + const profile = env.profiles().last(); + + expect(historySpy).toHaveBeenCalledWith(`/profiles/${profile.id()}/dashboard`); + + historySpy.mockRestore(); + }); + it("should not be able to create new profile if name already exists", async () => { const profile = await env.profiles().create(profileName); diff --git a/src/domains/profile/pages/CreateProfile/CreateProfile.tsx b/src/domains/profile/pages/CreateProfile/CreateProfile.tsx index 6ff84dc1dd..02f662e2ae 100755 --- a/src/domains/profile/pages/CreateProfile/CreateProfile.tsx +++ b/src/domains/profile/pages/CreateProfile/CreateProfile.tsx @@ -6,10 +6,12 @@ import { useHistory } from "react-router-dom"; import { Header } from "@/app/components/Header"; import { Page, Section } from "@/app/components/Layout"; import { useEnvironmentContext } from "@/app/contexts"; -import { useLocaleCurrency, useProfileRestore, useTheme } from "@/app/hooks"; +import { useAccentColor, useLocaleCurrency, useProfileRestore, useTheme } from "@/app/hooks"; import { ProfileForm, ProfileFormState } from "@/domains/profile/components/ProfileForm"; import { ThemeIcon } from "@/app/components/Icon"; +import { generatePath } from "react-router-dom"; +import { ProfilePaths } from "@/router/paths"; export const CreateProfile = () => { const { env, persist } = useEnvironmentContext(); @@ -22,12 +24,11 @@ export const CreateProfile = () => { useLayoutEffect(() => { resetTheme(); - - return () => { - resetTheme(); - }; }, [resetTheme]); + const { setProfileTheme } = useTheme(); + const { setProfileAccentColor } = useAccentColor(); + const handleSubmit = async ({ name, password, currency, viewingMode }: ProfileFormState) => { const profile = await env.profiles().create(name.trim()); await env.profiles().restore(profile); @@ -42,7 +43,10 @@ export const CreateProfile = () => { restoreProfileConfig(profile); await persist(); - history.push("/"); + setProfileTheme(profile); + setProfileAccentColor(profile); + + history.push(generatePath(ProfilePaths.Dashboard, { profileId: profile.id() })); }; return (