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

81.25% Statements 13/16
50% Branches 1/2
80% Functions 4/5
81.25% Lines 13/16

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 73 74 75 76 77 78 79 80 81 82              545x                   545x           545x                                   1x   1x 1x       1x           1x               545x 13713x   545x       545x     58x                  
import { RootState } from "@redux/types/store";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { TAccounts } from "@customTypes/accounts.types";
import { AccountsState, TSelectedAccount } from "./types";
 
type TSelectedAccountMod = TSelectedAccount & { saveInReduxOnly?: boolean };
 
const initialSelectedAccount: TSelectedAccount = {
  id: null,
  userAccID: null,
  userRole: "owner",
  name: "",
  userEmail: "",
  merchType: "",
  img: "",
};
 
const initialState: AccountsState = {
  total: 0,
  data: [],
  selectedAccount: initialSelectedAccount,
};
 
export const accountsSlice = createSlice({
  name: "accounts",
  initialState,
  reducers: {
    addAccounts: (
      state: AccountsState,
      action: PayloadAction<{
        total: number;
        data: TAccounts[];
      }>,
    ) => {
      state.total = action.payload.total;
      state.data = action.payload.data;
    },
    setSelectedAccount: (
      state: AccountsState,
      action: PayloadAction<TSelectedAccountMod>,
    ) => {
      const selectedAccount = { ...action.payload };
 
      if (selectedAccount?.saveInReduxOnly) {
        delete selectedAccount.saveInReduxOnly;
      } else E{
        localStorage.setItem("selected-account", String(action.payload.id));
      }
      state.selectedAccount = selectedAccount;
    },
    updatePartialAccount: (
      state: AccountsState,
      action: PayloadAction<any>,
    ) => {
      state.selectedAccount = {
        ...state.selectedAccount,
        ...action.payload,
      };
    },
  },
});
 
export const selectSelectedAccount = (state: RootState) =>
  state.accounts.selectedAccount;
export const { addAccounts, setSelectedAccount, updatePartialAccount } =
  accountsSlice.actions;
 
export default accountsSlice.reducer;
 
export const generateMockedSliceState = (
  customState?: Partial<AccountsState>,
) => {
  return {
    ...initialState,
    ...customState,
    selectedAccount: {
      ...initialState.selectedAccount,
      ...customState?.selectedAccount,
    },
  };
};