All files / src/components/ProfileMenu/modals/hooks useAlertsLogs.ts

78.04% Statements 32/41
63.63% Branches 14/22
72.72% Functions 8/11
80.55% Lines 29/36

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              8x         8x   8x 167x   167x   167x                     167x         20x 20x   20x   20x                 167x           20x   20x   20x 20x           167x         12x                 167x 11x   11x 22x 22x 22x   14x   14x           11x     167x                   40x  
import { useRef } from "react";
import { TAlertMethods } from "../types";
 
type PayloadAction = "subscribeList" | "unsubscribeList";
type LogsList = Record<PayloadAction, string[]>;
type AllLogs = Record<TAlertMethods, LogsList>;
 
const initialLoglist: LogsList = {
  subscribeList: [],
  unsubscribeList: [],
};
 
const inititialState = { email: initialLoglist, push: initialLoglist };
 
const useAlertsLogs = () => {
  const logs = useRef<AllLogs>(inititialState);
 
  const resetLogs = () => (logs.current = inititialState);
 
  const removeFromLogs = (id: string, method: TAlertMethods) => {
    const { subscribeList, unsubscribeList } = logs.current[method];
    logs.current = {
      ...logs.current,
      [method]: {
        subscribeList: subscribeList.filter((el) => el !== id),
        unsubscribeList: unsubscribeList.filter((el) => el !== id),
      },
    };
  };
 
  const addToList = (
    id: string,
    method: TAlertMethods,
    list: PayloadAction,
  ) => {
    const currentList = logs.current[method][list];
    const index = currentList.indexOf(id);
 
    Iif (index !== -1) return;
 
    logs.current = {
      ...logs.current,
      [method]: {
        ...logs.current[method],
        [list]: [...currentList, id],
      },
    };
  };
 
  const updateLog = (
    id: string,
    method: TAlertMethods,
    newValue?: boolean,
    originalValue?: boolean,
  ) => {
    Iif (!isBoolean(newValue) || !isBoolean(originalValue)) return;
 
    Iif (newValue === originalValue) {
      removeFromLogs(id, method);
    } else if (!originalValue && newValue) {
      addToList(id, method, "subscribeList");
    } else Eif (!newValue && originalValue) {
      addToList(id, method, "unsubscribeList");
    }
  };
 
  const logsBulkUpdate = (
    method: TAlertMethods,
    list: PayloadAction,
    ids: string[],
  ) => {
    logs.current = {
      ...logs.current,
      [method]: {
        ...logs.current[method],
        [list]: ids,
      },
    };
  };
 
  const generatePayload = () => {
    let payload = {};
 
    Object.keys(logs.current).forEach((method) => {
      const lists = logs.current[method as keyof AllLogs];
      const { subscribeList, unsubscribeList } = lists;
      if (subscribeList.length < 1 && unsubscribeList.length < 1) return;
 
      const key = method === "push" ? "pushNotification" : "email";
 
      payload = {
        ...payload,
        [key]: lists,
      };
    });
 
    return payload;
  };
 
  return {
    resetLogs,
    updateLog,
    generatePayload,
    logsBulkUpdate,
  };
};
 
export default useAlertsLogs;
 
const isBoolean = (value: any) => typeof value === "boolean";