feat(auth): add formData to the auth store; add corresponding setEmail and setPassword actions

This commit is contained in:
Ilia Mashkov
2026-03-24 11:15:23 +03:00
parent db42c87489
commit b871bf27de
3 changed files with 40 additions and 10 deletions

View File

@@ -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();

View File

@@ -5,6 +5,10 @@ import { callApi } from "shared/utils";
import { UNEXPECTED_ERROR_MESSAGE } from "shared/api";
export const defaultStoreState: Readonly<AuthStoreState> = {
formData: {
email: "",
password: "",
},
user: undefined,
status: "idle",
error: null,
@@ -12,7 +16,17 @@ export const defaultStoreState: Readonly<AuthStoreState> = {
export const useAuthStore = create<AuthStore>()((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 {