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

72.94% Statements 62/85
55.55% Branches 15/27
68.96% Functions 20/29
72.15% Lines 57/79

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                      538x           538x                           8x 8x 8x               228x 228x 228x 228x 22x 1x 1x     228x           173x 173x     173x   173x 33x 13x     173x             173x 25x       25x 25x 25x     25x 25x 49x   25x   25x 49x 37x                                                                                                   538x   538x 10650x 10650x   173x                       9x     229x         538x 538x 407x 538x   538x 97x 97x     97x 97x 133x 97x 133x 133x       97x     538x 1763x 1762x   686x        
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { RootState } from "@redux/types/store";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { useMemo } from "react";
import {
  SetUploadProgressParams,
  UploadDocumentTypes,
  UploadProgress,
  UploadProgressItems,
} from "./types";
 
const initialState: UploadProgress = {
  items: {},
  hidden: [],
  documentTypes: [],
};
 
const uploadProgressSlice = createSlice({
  name: "uploadProgress",
  initialState,
  reducers: {
    setUploadDocumentType: (
      state: UploadProgress,
      action: PayloadAction<{ type: UploadDocumentTypes }>,
    ) => {
      state.documentTypes = [...state.documentTypes, action.payload.type];
    },
    removeUploadDocumentType: (
      state: UploadProgress,
      action: PayloadAction<{ type: UploadDocumentTypes }>,
    ) => {
      const type = action.payload.type;
      const index = state.documentTypes.indexOf(type);
      Iif (index !== -1) {
        state.documentTypes.splice(index, 1);
      }
    },
    removeAllConvertingUploads: (
      state: UploadProgress,
      action: PayloadAction<{ type?: UploadDocumentTypes }>,
    ) => {
      state.documentTypes = [];
      const convertingItemKeys = [];
      const type = action.payload.type;
      for (const [key, value] of Object.entries(state.items)) {
        if (value?.converting && value?.documentType === type) {
          convertingItemKeys.push(key);
          state.hidden.push(key);
        }
      }
      convertingItemKeys.forEach((key) => delete state.items[key]);
    },
    setUploadProgress: (
      state: UploadProgress,
      action: PayloadAction<SetUploadProgressParams>,
    ) => {
      const progress = action.payload;
      Iif (state.hidden.includes(progress.key)) {
        return;
      }
      const isNewItem = !state.items[progress.key];
      // push everything up to make space for the new item
      if (isNewItem) {
        Object.values(state.items).forEach((item) => {
          item.index += 1;
        });
      }
      state.items[progress.key] = {
        //@ts-expect-error: it thinks we are duplicating
        index: 0,
        ...state.items[progress.key],
        ...progress.data,
      };
      // if status is success, we put the item at the top, changing the index
      if (progress.data.success) {
        pushToTop(state, progress.key);
      }
 
      function pushToTop(state: UploadProgress, action = "") {
        const key = action;
        const item = state.items[key];
        Iif (!item) {
          return;
        }
        const currentIndex = item.index;
        const maxIndex = Math.max(
          ...Object.values(state.items).map((i) => i.index),
        );
        item.index = maxIndex + 1;
        // now decrement all the other indexes next to the current index
        Object.values(state.items).forEach((i) => {
          if (i.index > currentIndex) {
            i.index -= 1;
          }
        });
      }
    },
    deleteUploadProgress: (
      state: UploadProgress,
      action: PayloadAction<string>, // key
    ) => {
      const currentUIIndex = state.items[action.payload]?.index;
      if (currentUIIndex === undefined) {
        return;
      }
      delete state.items[action.payload];
 
      // now the index of the items in which the index is greater than the deleted item's index should be decremented by 1
      Object.values(state.items).forEach((item) => {
        if (item.index > currentUIIndex) {
          item.index -= 1;
        }
      });
    },
    hideUploadProgress: (
      state: UploadProgress,
      action: PayloadAction<string>,
    ) => {
      const currentIndex = state.items[action.payload]?.index;
      if (currentIndex === undefined) {
        return;
      }
      state.hidden.push(action.payload);
      // decrement all the other indexes above the current index
      Object.values(state.items).forEach((i) => {
        if (i.index > currentIndex) {
          i.index -= 1;
        }
      });
      // delete the item from the items
      delete state.items[action.payload];
    },
  },
});
 
const {
  setUploadProgress,
  deleteUploadProgress,
  hideUploadProgress,
  setUploadDocumentType,
  removeUploadDocumentType,
  removeAllConvertingUploads,
} = uploadProgressSlice.actions;
 
export const useUploadProgress = () => {
  const dispatch = useAppDispatch();
  return {
    setUploadProgress: (params: SetUploadProgressParams) => {
      dispatch(setUploadProgress(params));
    },
    hideUploadProgress: (key: string) => {
      dispatch(hideUploadProgress(key));
    },
    deleteUploadProgress: (key: string) => {
      dispatch(deleteUploadProgress(key));
    },
    setUploadDocumentType: (type: UploadDocumentTypes) => {
      dispatch(setUploadDocumentType({ type }));
    },
    removeUploadDocumentType: (type: UploadDocumentTypes) => {
      dispatch(removeUploadDocumentType({ type }));
    },
    removeAllConvertingUploads: (type?: UploadDocumentTypes) => {
      dispatch(removeAllConvertingUploads({ type }));
    },
  };
};
 
const selectUploadProgress = (state: RootState) => state.uploadProgress.items;
const selectUploadDocumentTypes = (state: RootState) =>
  state.uploadProgress.documentTypes;
const selectHiddenKeys = (state: RootState) => state.uploadProgress.hidden;
 
export const useGetUploadProgress = () => {
  const allItems = useAppSelector(selectUploadProgress);
  const hiddenKeys = useAppSelector(selectHiddenKeys);
  // filter out the hidden items
 
  const filteredItems = useMemo(() => {
    const items = Object.keys(allItems);
    const remainingKeys = items.filter((key) => !hiddenKeys.includes(key));
    return remainingKeys.reduce((acc, key) => {
      acc[key] = allItems[key];
      return acc;
    }, {} as UploadProgressItems);
  }, [allItems, hiddenKeys]);
 
  return filteredItems;
};
 
export const useCheckDocumentProcessing = (type: UploadDocumentTypes) => {
  const documentTypes = useAppSelector(selectUploadDocumentTypes);
  if (!Array.isArray(documentTypes)) return false;
 
  return documentTypes.some((docType) => docType === type);
};
 
export default uploadProgressSlice.reducer;