All files / src/redux localStorageUtils.ts

0% Statements 0/50
0% Branches 0/22
0% Functions 0/6
0% Lines 0/48

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                                                                                                                                                                                                                       
import { ImportMetaData } from "../apps/dictionaries";
 
export function createLocalStorageObject(name: string) {
  try {
    const existing = localStorage.getItem(name);
    if (!existing) {
      setLocalStorageObject("notification", "inProgressList", []);
      setLocalStorageObject("notification", "loadingList", []);
      setLocalStorageObject("notification", "erroredList", []);
      setLocalStorageObject("notification", "successList", []);
      setLocalStorageObject("notification", "index", -1);
      setLocalStorageObject("notification", "indexList", []);
      setLocalStorageObject("notification", "importMetaDataList", []);
    }
  } catch (error) {
    console.log(error);
  }
}
 
function setLocalStorageObject(name: string, key: string, value: any) {
  const existing = localStorage.getItem(name);
  let existingObject = existing ? JSON.parse(existing) : {};
  existingObject[key] = value;
  localStorage.setItem(name, JSON.stringify(existingObject));
}
 
export function addToLocalStorageObject(
  name: string,
  key: string,
  value: string | boolean | null | ImportMetaData
) {
  try {
    const existing = localStorage.getItem(name);
    let existingObject = existing ? JSON.parse(existing) : {};
    if (existingObject[key]) {
      if (existingObject[key].length >= 10) existingObject[key].shift();
      existingObject[key].push(value);
    }
    if (key === "inProgressList") {
      const index = existingObject["index"] + 1;
      existingObject["index"] = index;
      if (existingObject["indexList"].length >= 10)
        existingObject["indexList"].shift();
      existingObject["indexList"].push(index);
    }
    localStorage.setItem(name, JSON.stringify(existingObject));
    return existingObject["index"];
  } catch (error) {
    console.log(error);
  }
}
 
export function updateLocalStorageArray({
  name,
  key,
  value,
  index
}: {
  name: string;
  key: string;
  value: any;
  index: number;
}) {
  try {
    const retrievedData = localStorage.getItem(name);
    let retrievedDataJsonObject = retrievedData
      ? JSON.parse(retrievedData)
      : {};
 
    if (!retrievedDataJsonObject[key]) {
      return;
    }
 
    const getActualIndex = (element: number) => element === index;
    const actualIndex = retrievedDataJsonObject["indexList"].findIndex(
      getActualIndex
    );
    retrievedDataJsonObject[key][actualIndex] = value;
    localStorage.setItem(name, JSON.stringify(retrievedDataJsonObject));
  } catch (error) {
    console.log(error);
  }
}
 
export function getLocalStorageObject({
  name,
  key,
  value
}: {
  name: string;
  key: string;
  value: any;
}) {
  try {
    const retrievedData = localStorage.getItem(name);
    let retrievedDataJsonObject = retrievedData
      ? JSON.parse(retrievedData)
      : {};
    if (!retrievedDataJsonObject[key]) {
      return value;
    }
    return retrievedDataJsonObject[key];
  } catch (error) {
    console.log(error);
    return value;
  }
}