All files / public/store-details topics.js

75.89% Statements 85/112
62.9% Branches 39/62
70.37% Functions 19/27
76.57% Lines 85/111

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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261          8x 8x 8x 8x   8x 7x 7x     8x   8x   8x       8x 8x 8x   8x 1x 1x 1x 1x     8x 7x   1x             10x 10x 10x 10x               8x   8x 1x 1x 1x       8x 8x     8x 7x     8x       8x   8x       8x       8x       8x       8x             8x 8x 16x 1x 1x             1x 1x 1x 1x                     9x       9x 3x 3x 3x 3x   3x             10x 10x 2x   8x                   16x 16x 8x   8x                   2x 2x 2x                       9x 9x 7x   2x                   9x 8x   1x       1x 1x           1x                                                   3x       3x 3x 3x 2x 2x 1x                        
import buildTopicCard from "./buildTopicCard";
 
/** Store page filters */
class initTopics {
  constructor() {
    this.selectElements();
    this.togglePlaceholderContainer(true);
    this.searchCache = window.location.search;
    this._filters = this.getUrlFilters();
 
    if (this._filters.q.length === 0 && this._filters.filter.length === 0) {
      this.togglePlaceholderContainer();
      this.toggleFeaturedContainer(true);
    }
 
    this.fetchTopicList()
      .then((data) => {
        this.allTopics = data.topics;
 
        Iif (!this.allTopics) {
          return;
        }
 
        this.groupAllTopics();
        this.filterTopics();
        this.handleFilterClick();
 
        if (this._filters.q.length > 0 || this._filters.filter.length > 0) {
          this.toggleFeaturedContainer(false);
          this.renderTopics();
          this.toggleTopicsContainer(true);
          this.togglePlaceholderContainer();
        }
 
        if (this.topics.length === 0) {
          this.toggleTopicsSection(false);
        } else {
          this.toggleTopicsSection(true);
        }
      })
      .catch((e) => console.error(e));
  }
 
  fetchTopicList() {
    if (this._filters.q) {
      const queryUrl = this._filters.q.join(",");
      return fetch(`/topics.json?q=${queryUrl}`).then((result) =>
        result.json()
      );
    } else E{
      return fetch("/topics.json").then((result) => result.json());
    }
  }
 
  getUrlFilters() {
    const filters = {};
 
    if (window.location.search) {
      const searchParams = new URLSearchParams(window.location.search);
      for (const [filterType, filterValue] of searchParams) {
        filters[filterType] = filterValue.split(",");
      }
    }
 
    Eif (!filters.filter) {
      filters.filter = [];
    }
 
    if (!filters.q) {
      filters.q = [];
    }
 
    return filters;
  }
 
  selectElements() {
    this.domEl = {};
 
    this.domEl.categoryFilters = {
      el: document.querySelectorAll(".category-filter"),
      selector: ".category-filter",
    };
    this.domEl.topicsSection = {
      el: document.querySelector("[data-js='topics-section']"),
      selector: "[data-js='topics-section']",
    };
    this.domEl.placeholderContainer = {
      el: document.querySelector("[data-js='topics-placeholder-container']"),
      selector: "[data-js='topics-placeholder-container']",
    };
    this.domEl.topicsContainer = {
      el: document.querySelector("[data-js='topics-container']"),
      selector: "[data-js='topics-container']",
    };
    this.domEl.featuredContainer = {
      el: document.querySelector("[data-js='featured-topics-container']"),
      selector: "[data-js='featured-topics-container']",
    };
  }
 
  handleFilterClick() {
    if (this.domEl.categoryFilters.el) {
      this.domEl.categoryFilters.el.forEach((categoryFilter) => {
        categoryFilter.addEventListener("click", () => {
          if (categoryFilter.checked) {
            this._filters.filter.push(categoryFilter.value);
          } else E{
            this._filters.filter = this._filters.filter.filter(
              (el) => el !== categoryFilter.value
            );
          }
 
          this.filterTopics();
          this.renderTopics();
          this.toggleFeaturedContainer();
          this.toggleTopicsContainer(true);
        });
      });
    } else E{
      throw new Error(
        `There are no elements containing ${this.domEl.categoryFilters.selector} selector.`
      );
    }
  }
 
  groupAllTopics() {
    this.groupedTopics = {
      categories: {},
    };
 
    this.allTopics.forEach((entity) => {
      Eif (entity.categories) {
        entity.categories.forEach((cat) => {
          Eif (!this.groupedTopics.categories[cat]) {
            this.groupedTopics.categories[cat] = [];
          }
          this.groupedTopics.categories[cat].push(entity);
        });
      }
    });
  }
 
  toggleTopicsSection(visibility) {
    if (this.domEl.topicsSection.el) {
      if (visibility) {
        this.domEl.topicsSection.el.classList.remove("u-hide");
      } else {
        this.domEl.topicsSection.el.classList.add("u-hide");
      }
    } else E{
      throw new Error(
        `There is no element containing ${this.domEl.topicsSection.selector} selector.`
      );
    }
  }
 
  togglePlaceholderContainer(visibility) {
    if (this.domEl.placeholderContainer.el) {
      if (visibility) {
        this.domEl.placeholderContainer.el.classList.remove("u-hide");
      } else {
        this.domEl.placeholderContainer.el.classList.add("u-hide");
      }
    } else E{
      throw new Error(
        `There is no element containing ${this.domEl.placeholderContainer.selector} selector.`
      );
    }
  }
 
  toggleTopicsContainer(visibility) {
    if (this.domEl.topicsContainer.el) {
      if (visibility) {
        this.domEl.topicsContainer.el.classList.remove("u-hide");
      } else E{
        this.domEl.topicsContainer.el.classList.add("u-hide");
      }
    } else E{
      throw new Error(
        `There is no element containing ${this.domEl.topicsContainer.selector} selector.`
      );
    }
  }
 
  toggleFeaturedContainer(visibility) {
    if (this.domEl.featuredContainer.el) {
      if (visibility) {
        this.domEl.featuredContainer.el.classList.remove("u-hide");
      } else {
        this.domEl.featuredContainer.el.classList.add("u-hide");
      }
    } else E{
      throw new Error(
        `There is no element containing ${this.domEl.featuredContainer.selector} selector.`
      );
    }
  }
 
  filterTopics() {
    if (this._filters.filter.length === 0) {
      this.topics = this.allTopics;
    } else {
      this.topics = this.allTopics.filter((entity) =>
        this.filterTopicsByCategory(entity)
      );
 
      Eif (this.topics.length < 3) {
        const bucketFillers = this.allTopics.reduce((acc, topic) => {
          if (!this.topics.includes(topic)) {
            acc.push(topic);
          }
          return acc;
        }, []);
        this.topics = this.topics.concat(bucketFillers);
      }
    }
  }
 
  filterTopicsByCategory(entity) {
    let packageCategories = [];
 
    if (entity.categories) {
      packageCategories = entity.categories.map((cat) => {
        return cat;
      });
    }
 
    const cats = this._filters.filter.filter((cat) => {
      if (packageCategories.includes(cat)) {
        return cat;
      }
    });
 
    if (cats.length) {
      return entity;
    }
  }
 
  renderTopics() {
    Iif (!("content" in document.createElement("template"))) {
      return;
    }
 
    if (this.domEl.topicsContainer.el) {
      this.domEl.topicsContainer.el.innerHTML = "";
      this.topics.slice(0, 3).forEach((entity) => {
        const topicCard = buildTopicCard(entity);
        if (topicCard) {
          this.domEl.topicsContainer.el.appendChild(topicCard);
        }
      });
    } else E{
      throw new Error(
        `There is no element containing ${this.domEl.topicsContainer.selector} selector.`
      );
    }
  }
}
 
export { initTopics };