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: navigate to dashboard after creating profile #817

Merged
merged 7 commits into from
Nov 14, 2024
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
Loading