feat(auth): add formData to the auth store; add corresponding setEmail and setPassword actions
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user