All files / publisher/market categories.ts

100% Statements 46/46
93.75% Branches 15/16
100% Functions 7/7
100% Lines 45/45

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  6x 6x       3x   3x       1x       6x       15x     15x     15x     15x     15x       15x     15x       15x 12x 12x 12x 48x 48x 12x 12x 8x     36x         15x 11x 11x         15x 9x 9x 4x 4x 4x 4x     5x 5x 5x   9x     15x 3x 3x 3x     15x 3x 3x 3x          
function categories(form: any, state: any) {
  const categoriesList = [];
  if (
    form.elements["primary_category"] &&
    form.elements["primary_category"].value !== ""
  ) {
    categoriesList.push(form.elements["primary_category"].value);
 
    if (
      form.elements["secondary_category"] &&
      form.elements["secondary_category"].value !== ""
    ) {
      categoriesList.push(form.elements["secondary_category"].value);
    }
  }
 
  state.categories = categoriesList;
}
 
function initCategories() {
  const categoryHelpTextEl = document.querySelector(
    ".js-categories-category1-help-text",
  ) as HTMLElement;
  const categorySecondaryAddEl = document.querySelector(
    ".js-categories-category2-add",
  ) as HTMLElement;
  const categorySecondaryPickerEl = document.querySelector(
    ".js-categories-category2-picker",
  ) as HTMLElement;
  const categorySecondaryAddLink = document.querySelector(
    ".js-categories-category2-add-link",
  ) as HTMLElement;
  const secondaryCategoryRemove = document.querySelector(
    ".js-categories-category2-remove",
  ) as HTMLElement;
 
  const primaryCategorySelectEl = document.querySelector(
    "[name='primary_category']",
  ) as HTMLSelectElement;
  const secondaryCategorySelectEl = document.querySelector(
    "[name='secondary_category']",
  ) as HTMLSelectElement;
 
  const setSecondaryOptions = () => {
    const primaryValue = primaryCategorySelectEl.value;
    const secondaryValue = secondaryCategorySelectEl.value;
    for (let i = 0; i < secondaryCategorySelectEl.options.length; i++) {
      const option = secondaryCategorySelectEl.options[i];
      if (option.value === primaryValue) {
        option.setAttribute("disabled", "disabled");
        if (secondaryValue === primaryValue) {
          resetSecondaryCategory();
        }
      } else {
        option.removeAttribute("disabled");
      }
    }
  };
 
  const resetSecondaryCategory = () => {
    secondaryCategorySelectEl.value = "";
    secondaryCategorySelectEl.dispatchEvent(
      new Event("change", { bubbles: true }),
    );
  };
 
  primaryCategorySelectEl.addEventListener("change", () => {
    const value = primaryCategorySelectEl.value;
    if (value.trim() !== "") {
      categoryHelpTextEl.classList.add("u-hide");
      Eif (secondaryCategorySelectEl.value === "") {
        categorySecondaryAddEl.classList.remove("u-hide");
        categorySecondaryPickerEl.classList.add("u-hide");
      }
    } else {
      categoryHelpTextEl.classList.remove("u-hide");
      categorySecondaryAddEl.classList.add("u-hide");
      categorySecondaryPickerEl.classList.add("u-hide");
    }
    setSecondaryOptions();
  });
 
  categorySecondaryAddLink.addEventListener("click", () => {
    categorySecondaryAddEl.classList.add("u-hide");
    categorySecondaryPickerEl.classList.remove("u-hide");
    setSecondaryOptions();
  });
 
  secondaryCategoryRemove.addEventListener("click", () => {
    resetSecondaryCategory();
    categorySecondaryPickerEl.classList.add("u-hide");
    categorySecondaryAddEl.classList.remove("u-hide");
  });
}
 
export { categories, initCategories };