Skip to content

Commit

Permalink
refactor: navigate to dashboard after creating profile (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahin-hq authored Nov 14, 2024
1 parent 64a2a1c commit 9c2a811
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/domains/profile/pages/CreateProfile/CreateProfile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ describe("CreateProfile", () => {
expect(profile.usesPassword()).toBe(false);
});

it("should navigate to dashboard after creating profile", async () => {
const history = createHashHistory();

render(<CreateProfile />, { 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);

Expand Down
16 changes: 10 additions & 6 deletions src/domains/profile/pages/CreateProfile/CreateProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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 (
Expand Down

0 comments on commit 9c2a811

Please sign in to comment.