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

82.92% Statements 68/82
48.14% Branches 13/27
91.66% Functions 22/24
86.84% Lines 66/76

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202            540x               540x               42x 42x     46x 46x 46x 46x                                                 47x             94x                       540x   540x 3729x 3729x 3729x   14x 14x 14x   14x   14x               14x 14x 56x 56x   56x     14x           3729x     540x 302x   540x 449x   540x 6x   540x 6x   540x 6027x   540x 5786x       540x                 540x 4050x 48x 48x     540x 4050x   4050x   4067x   4050x   4050x   4073x   4050x   4050x     4050x 46x   46x 46x 46x     46x       46x       4050x 2x     4050x 1x     4050x   4050x                      
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { RootState } from "@redux/types/store";
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { isArray, isEmpty } from "lodash";
import { FilterDate, TableFilterState } from "./types";
 
const initialState: TableFilterState = {
  queryParams: undefined,
  queryString: "",
  filterDates: {},
  quickFilter: null,
  queryStringQuickFilter: "",
};
 
const tableFiltersSlice = createSlice({
  name: "tableFilters",
  initialState,
  reducers: {
    setFilters: (
      state: TableFilterState,
      action: PayloadAction<TableFilterState>,
    ) => {
      state.queryParams = action.payload.queryParams;
      state.queryString = action.payload.queryString;
    },
    resetFilters: (state: TableFilterState) => {
      state.queryParams = undefined;
      state.queryString = "";
      state.customTotalProcessing = undefined;
      state.filterDates = {};
    },
    setCustomTotalProcessing: (
      state: TableFilterState,
      action: PayloadAction<{ from?: number; to?: number }>,
    ) => {
      state.customTotalProcessing = {
        from: action.payload.from,
        to: action.payload.to,
      };
    },
    setFilterDates: (
      state: TableFilterState,
      action: PayloadAction<FilterDate>,
    ) => {
      const { dateName } = action.payload;
      if (!state.filterDates) {
        state.filterDates = {};
      }
      state.filterDates[dateName] = action.payload;
    },
    setQuickFilter: (
      state: TableFilterState,
      action: PayloadAction<string | null>,
    ) => {
      state.quickFilter = action.payload;
    },
 
    setQueryStringQuickFilter: (
      state: TableFilterState,
      action: PayloadAction<string>,
    ) => {
      state.queryStringQuickFilter = action.payload;
    },
  },
});
 
export const {
  setFilters,
  resetFilters,
  setCustomTotalProcessing,
  setFilterDates,
  setQuickFilter,
  setQueryStringQuickFilter,
} = tableFiltersSlice.actions;
 
export const getSelectedFiltersAmount = (state: RootState) => {
  const { queryParams } = state.tableFilters;
  let filtersAmount = 0;
  if (queryParams) {
    // Step 1: Count regular filters (excluding key pairs)
    filtersAmount += Object.entries(queryParams).filter(([key, value]) => {
      const isPartOfKeyPair = keyPairsToCheckSeparately.some((pair) =>
        pair.includes(key),
      );
      Iif (isPartOfKeyPair) return false;
 
      Eif (isArray(value)) return value.length > 0;
      if (typeof value === "boolean") return true;
      if (typeof value === "string") return value.trim().length > 0;
 
      return false;
    }).length;
 
    // Step 2: Count key pairs if at least one key in the pair exists with a value
    keyPairsToCheckSeparately.forEach((keyPair) => {
      const hasAnyValue = keyPair.some((key) => {
        const val = queryParams[key];
        Iif (isArray(val)) return val.length > 0;
 
        return typeof val === "boolean";
      });
 
      Iif (hasAnyValue) {
        filtersAmount += 1;
      }
    });
  }
 
  return filtersAmount;
};
 
export const selectedQueries = (state: RootState) =>
  state.tableFilters.queryParams;
 
export const selectedQueryString = (state: RootState) =>
  state.tableFilters.queryString;
 
export const customTotalProcessing = (state: RootState) =>
  state.tableFilters.customTotalProcessing;
 
export const customFilterDates = (state: RootState) =>
  state.tableFilters.filterDates;
 
export const selectedQuickFilter = (state: RootState) =>
  state.tableFilters.quickFilter;
 
export const selectedQueryStringQuickFilter = (state: RootState) =>
  state.tableFilters.queryStringQuickFilter;
 
export default tableFiltersSlice.reducer;
 
const keyPairsToCheckSeparately = [
  [
    "ownerMembershipStatus",
    "statusDisplayName",
    "isInOnboardingSection",
    "stateName",
  ],
];
 
const extractQuickFilterKey = (queryString?: string): string | null => {
  if (!queryString) return null;
  const match = queryString.match(/^\(([^:]+):/);
  return match ? match[1] : null;
};
 
export const useTableFilters = () => {
  const dispatch = useAppDispatch();
 
  const quickFilterSelector = createSelector(
    selectedQuickFilter,
    (quickFilter) => quickFilter,
  );
  const quickFilter = useAppSelector(quickFilterSelector);
 
  const queryStringQuickFilterSelector = createSelector(
    selectedQueryStringQuickFilter,
    (queryStringQuickFilter) => queryStringQuickFilter,
  );
  const queryStringQuickFilter = useAppSelector(queryStringQuickFilterSelector);
  const queryKey =
    extractQuickFilterKey(queryStringQuickFilter) ??
    "riskLevelLabelDisplayName";
 
  const resetTableFilters = (all = false) => {
    dispatch(resetFilters());
 
    Eif (all) {
      dispatch(setQuickFilter(null));
      dispatch(setQueryStringQuickFilter(""));
    }
 
    Iif (quickFilter) {
 
      dispatch(setQueryStringQuickFilter(`(${queryKey}:"${quickFilter}")`));
    } else {
      dispatch(setQueryStringQuickFilter(""));
    }
  };
 
  const setQueryStringWithQuickFilter = (str: string) => {
    dispatch(setQueryStringQuickFilter(str));
  };
 
  const setQuickFilterValue = (str: string | null) => {
    dispatch(setQuickFilter(str));
  };
 
  const filtersAmount = useAppSelector(getSelectedFiltersAmount);
 
  return {
    quickFilter,
    queryStringQuickFilter,
    resetTableFilters,
    setQueryStringWithQuickFilter,
    setQuickFilterValue,
    filtersAmount,
    queryKey,
    hasQuickFilter: !!quickFilter,
  };
};