{"size": 2302, "ext": "js", "max_stars_repo_path": "hebrew_quiz/src/components/Persistence/index.js", "max_stars_repo_name": "LukeMurphey/hebrew_quizzes", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "hebrew_quiz/src/components/Persistence/index.js", "max_issues_repo_name": "LukeMurphey/hebrew_quizzes", "max_issues_repo_issues_event_min_datetime": "2021-08-01T00:10:16.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-08T04:05:46.000Z", "max_forks_repo_path": "hebrew_quiz/src/components/Persistence/index.js", "max_forks_repo_name": "LukeMurphey/hebrew_quizzes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 32, "max_forks_count": null, "avg_line_length": 25.5777777778, "max_line_length": 80, "alphanum_fraction": 0.6702867072}
const QUIZ_STATUSES_NAME = "quizStatuses";

export function storageAvailable(type) {
  let storage;
  try {
    storage = window[type];
    const x = "__storage_test__";
    storage.setItem(x, x);
    storage.removeItem(x);
    return true;
  } catch (e) {
    return (
      e instanceof DOMException &&
      // everything except Firefox
      (e.code === 22 ||
        // Firefox
        e.code === 1014 ||
        // test name field too, because code might not be present
        // everything except Firefox
        e.name === "QuotaExceededError" ||
        // Firefox
        e.name === "NS_ERROR_DOM_QUOTA_REACHED") &&
      // acknowledge QuotaExceededError only if there's something already stored
      storage &&
      storage.length !== 0
    );
  }
}

export function getQuizStatus(quizID, storageOverride = null) {
  const quizStatuses = getQuizStatuses(storageOverride);

  if (quizStatuses !== null && quizID in quizStatuses) {
    return quizStatuses[quizID];
  }

  return null;
}

export function getQuizStatuses(storageOverride = null) {
  const storage = storageOverride || localStorage;

  // if (storageAvailable("localStorage")) {
  const quizStatuses = storage.getItem(QUIZ_STATUSES_NAME);

  if (quizStatuses) {
    try {
      const quizStatusesParsed = JSON.parse(quizStatuses);

      return quizStatusesParsed;
    } catch (err) {
      // eslint-disable-next-line no-console
      console.warn("The list of quiz statuses could not be loaded");
      return null; // The list could not be loaded
    }
  }

  return null;
}

export function setQuizStatuses(quizStatuses, storageOverride = null) {
  const storage = storageOverride || localStorage;

  storage.setItem(QUIZ_STATUSES_NAME, JSON.stringify(quizStatuses));
}

export function setQuizStatus(quizID, status, storageOverride = null) {
  const storage = storageOverride || localStorage;

  // if (storageAvailable("localStorage")) {
  let quizStatuses = getQuizStatuses(storage);

  // If it doesn't exist yet, then initialize it
  if (!quizStatuses) {
    quizStatuses = {};
  }

  // Set the quiz status
  quizStatuses[quizID] = {
    status,
    dateUpdated: Date.now(),
  };

  // Save it
  setQuizStatuses(quizStatuses, storageOverride);

  // Return true noting that we were able to save it
  return true;
}
