diff --git a/src/features/auth/model/stores/authStore/authStore.spec.ts b/src/features/auth/model/stores/authStore/authStore.spec.ts index 1398b53..eba0cc9 100644 --- a/src/features/auth/model/stores/authStore/authStore.spec.ts +++ b/src/features/auth/model/stores/authStore/authStore.spec.ts @@ -22,6 +22,20 @@ describe("authStore", () => { }); afterAll(() => server.close()); + describe("setEmail", () => { + it("should set the email in formData", () => { + useAuthStore.getState().setEmail(MOCK_NEW_EMAIL); + expect(useAuthStore.getState().formData.email).toBe(MOCK_NEW_EMAIL); + }); + }); + + describe("setPassword", () => { + it("should set the password in formData", () => { + useAuthStore.getState().setPassword(MOCK_PASSWORD); + expect(useAuthStore.getState().formData.password).toBe(MOCK_PASSWORD); + }); + }); + describe("reset", () => { it("should reset the store to default state", () => { useAuthStore.getState().reset(); diff --git a/src/features/auth/model/stores/authStore/authStore.ts b/src/features/auth/model/stores/authStore/authStore.ts index 6b6d36f..ccda162 100644 --- a/src/features/auth/model/stores/authStore/authStore.ts +++ b/src/features/auth/model/stores/authStore/authStore.ts @@ -5,6 +5,10 @@ import { callApi } from "shared/utils"; import { UNEXPECTED_ERROR_MESSAGE } from "shared/api"; export const defaultStoreState: Readonly = { + formData: { + email: "", + password: "", + }, user: undefined, status: "idle", error: null, @@ -12,7 +16,17 @@ export const defaultStoreState: Readonly = { export const useAuthStore = create()((set) => ({ ...defaultStoreState, - reset: () => set(defaultStoreState), + + setEmail: (email: string) => { + set((state) => ({ formData: { ...state.formData, email } })); + }, + setPassword: (password: string) => { + set((state) => ({ formData: { ...state.formData, password } })); + }, + reset: () => { + set(defaultStoreState); + }, + login: async (loginData) => { set({ status: "loading" }); try { diff --git a/src/features/auth/model/types/store.ts b/src/features/auth/model/types/store.ts index e6e356a..5dc9606 100644 --- a/src/features/auth/model/types/store.ts +++ b/src/features/auth/model/types/store.ts @@ -3,6 +3,10 @@ import type { AuthData, AuthStatus } from "./service"; import type { ApiError } from "shared/utils"; export interface AuthStoreState { + /** + * Form data for login/register forms + */ + formData: AuthData; /** * User's credentials */ @@ -17,16 +21,14 @@ export interface AuthStoreState { error: ApiError | Error | null; } -export type ResetAction = () => void; -export type LoginAction = (data: AuthData) => Promise; -export type RegisterAction = (data: AuthData) => Promise; -export type LogoutAction = () => Promise; - export interface AuthStoreActions { - reset: ResetAction; - login: LoginAction; - register: RegisterAction; - logout: LogoutAction; + setEmail: (email: string) => void; + setPassword: (password: string) => void; + reset: () => void; + + login: (data: AuthData) => Promise; + register: (data: AuthData) => Promise; + logout: () => Promise; } export type AuthStore = AuthStoreState & AuthStoreActions;