All files / src/redux/slices/customers index.ts

26.47% Statements 9/34
0% Branches 0/4
7.69% Functions 1/13
25.8% Lines 8/31

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                540x                                                 540x                                                                                                                                         540x   540x 540x   1061x 540x   540x      
import { RootState } from "@redux/types/store";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { replaceValues, withAppendedKey } from "@services/filtering";
import { AmountKeys } from "@services/filtering/filtering.types";
 
import createDateFilter, { TDate } from "@common/Filter/utils/createDateFilter";
import { CustomersState } from "./types";
 
const initialState: CustomersState = {
  sorting: "-createdAt",
  query: {
    joined: "",
    recurrence: "",
    amount: "",
  },
  names: {
    amount: [],
    recurrence: [],
    joined: [],
  },
  filteredTableValues: [],
};
 
type Amount = {
  title: "amount";
  values: {
    modifier: string;
    amount: number;
    amountOne: number;
    amountTwo: number;
  };
};
 
const customersSlice = createSlice({
  name: "customers",
  initialState,
  reducers: {
    updateSorting: (state: CustomersState, action: PayloadAction<string>) => {
      state.sorting = action.payload;
    },
    addDateFilter: (state: CustomersState, action: PayloadAction<TDate>) => {
      const { name, query } = createDateFilter(action.payload);
      state.names.joined = name;
      state.query.joined = query;
    },
    disableDateFilter: (state: CustomersState) => {
      state.names.joined = [];
      state.query.joined = "";
    },
    addAmountFilter: (state: CustomersState, action: PayloadAction<Amount>) => {
      const { modifier, amount, amountOne, amountTwo } = action.payload.values;
      state.names[action.payload.title] = [amountOne, amountTwo];
 
      if (modifier.includes("greater than") || modifier.includes("less than")) {
        state.names[action.payload.title] = [modifier, amount];
        state.query[action.payload.title] = replaceValues(
          withAppendedKey.amount[modifier as AmountKeys],
          amount,
        );
      } else {
        state.names[action.payload.title] = [amountOne, amountTwo];
        state.query[action.payload.title] = replaceValues(
          withAppendedKey.amount[modifier as AmountKeys],
          amountOne,
          amountTwo,
        );
      }
    },
    disableAmountFilter: (
      state: CustomersState,
      action: PayloadAction<"amount">,
    ) => {
      state.names[action.payload] = [];
      state.query.amount = "";
    },
    addRecurrenceFilter: (state: CustomersState) => {
      state.names.recurrence = ["true"];
      state.query.recurrence = replaceValues(
        withAppendedKey.recurrence.default,
        "true",
      );
    },
    disableRecurrenceFilter: (state: CustomersState) => {
      state.names.recurrence = [];
      state.query.recurrence = "";
    },
    clearFilters: (state: CustomersState) => {
      state.query = initialState.query;
      state.names = initialState.names;
    },
  },
});
 
export const {
  addDateFilter,
  disableDateFilter,
  addAmountFilter,
  disableAmountFilter,
  updateSorting,
  clearFilters,
  addRecurrenceFilter,
  disableRecurrenceFilter,
} = customersSlice.actions;
 
export const selectFilters = (state: RootState) => state.customers.names;
export const selectRecurrenceFilter = (state: RootState) =>
  state.customers.names.recurrence;
export const selectQueryFilters = (state: RootState) => state.customers.query;
export const selectTableValues = (state: RootState) =>
  state.customers.filteredTableValues;
export const sortingCustomers = (state: RootState) => state.customers.sorting;
 
export default customersSlice.reducer;