All files / src/redux/slices/auth auth.ts

75% Statements 12/16
100% Branches 0/0
57.14% Functions 4/7
71.42% Lines 10/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72        538x                                         538x               1157x                             29932x 538x   538x   538x   538x       538x     95x                  
import { RootState } from "@redux/types/store";
import { createAction, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitialState, TAuthUser } from "./types";
 
const initialState: InitialState = {
  isAuthenticated: false,
  user: {
    userAccID: 0,
    id: 0,
    img: "",
    name: "",
    email: "",
    role: "",
    globalName: {
      phoneNumber: "",
      firstName: "",
      lastName: "",
    },
    currency: "",
    language: "",
    timezone: "",
  },
  passwordStatus: "",
};
 
export const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    updatePartialUser: (
      state: InitialState,
      action: PayloadAction<Partial<TAuthUser>>,
    ) => {
      state.user = {
        ...state.user,
        ...action.payload,
      };
    },
    login: (state: InitialState, action: PayloadAction<TAuthUser>) => {
      state.isAuthenticated = true;
      state.user = action.payload;
    },
    setPasswordStatus: (state: InitialState, action: PayloadAction<string>) => {
      state.passwordStatus = action.payload;
    },
  },
});
 
export const selectUser = (state: RootState) => state.auth?.user;
export const selectPasswordStatus = (state: RootState) =>
  state.auth?.passwordStatus;
export const selectAuth = (state: RootState) => state.auth?.isAuthenticated;
export const { login, setPasswordStatus, updatePartialUser } =
  authSlice.actions;
 
export const logoutAction = createAction("LOGOUT");
 
export default authSlice.reducer;
 
export const generateMockedAuthState = (
  customState?: Partial<InitialState>,
) => {
  return {
    ...initialState,
    ...customState,
    user: {
      ...initialState.user,
      ...customState?.user,
    },
  };
};