window.addEventListener('load', function () {
const categoryList = document.getElementById('inspiration-category');
const filterSelect = document.getElementById('inspiration-filter');
const inspirationImages = document.getElementById('inspiration-images');
function debounce(fn, delay) {
var timeout;
return function () {
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
fn.apply(undefined, args);
}, delay);
};
}
async function fetchCategories() {
try {
const response = await fetch('https://www.morgenland-taepper.com/rest/morgenland-category-groups');
const data = await response.json();
const uniqueFilterOptions = new Set();
data.data.forEach(function (group = '') {
const groupListItem = document.createElement('li');
const groupLink = document.createElement('a');
groupLink.href = '/lookbook?group=' + group.slug;
groupLink.textContent = group.name;
groupListItem.appendChild(groupLink);
categoryList.appendChild(groupListItem);
group.categories.forEach(function (category) {
const optionText = category.name;
if (!uniqueFilterOptions.has(optionText)) {
uniqueFilterOptions.add(optionText);
const option = document.createElement('option');
option.value = category.id;
option.textContent = optionText;
filterSelect.appendChild(option);
}
});
});
filterSelect.addEventListener('change', handleFilterChange);
} catch (error) {
console.error('Error fetching data:', error);
}
}
async function fetchLookBooks(url) {
try {
const response = await fetch(url);
const data = await response.json();
displayLookBooks(data.data);
} catch (error) {
console.error('Error fetching look books:', error);
}
}
async function initializePage(group = '') {
await fetchCategories();
let url = 'https://www.morgenland-taepper.com/rest/morgenland-look-books';
if (group) {
url += '?group=' + encodeURIComponent(group);
}
fetchLookBooks(url);
}
const handleFilterChange = debounce(async function () {
const selectedOption = filterSelect.options[filterSelect.selectedIndex];
let url = 'https://www.morgenland-taepper.com/rest/morgenland-look-books';
if (selectedOption.value) {
url += '?category=' + encodeURIComponent(selectedOption.value);
}
const urlParams = new URLSearchParams(window.location.search);
const categoryGroup = urlParams.get('group');
await initializePage(categoryGroup);
}, 300);
function displayLookBooks(items) {
inspirationImages.innerHTML = '';
items.forEach(function (item) {
const url = new URL(item.category_link);
const category_link = url.pathname;
const inspirationItem = document.createElement('div');
inspirationItem.classList.add('inspiration-item');
const overlay = document.createElement('div');
overlay.classList.add('overlay');
inspirationItem.appendChild(overlay);
const imgLink = document.createElement('a');
imgLink.classList.add('img-link');
imgLink.href = item.image_link;
imgLink.target = '_blank';
const externalLinkIcon = document.createElement('i');
externalLinkIcon.classList.add('fa', 'fa-external-link');
externalLinkIcon.setAttribute('aria-hidden', 'true');
imgLink.appendChild(externalLinkIcon);
inspirationItem.appendChild(imgLink);
const img = document.createElement('img');
img.src = item.image_link;
img.alt = item.category_name;
img.loading = 'lazy';
inspirationItem.appendChild(img);
const categoryLink = document.createElement('a');
categoryLink.href = category_link;
categoryLink.target = '_blank';
const content = document.createElement('div');
content.classList.add('content', 'text-center', 'd-flex', 'justify-content-center', 'align-items-center');
const h3 = document.createElement('p');
h3.classList.add('h3');
h3.textContent = item.category_name;
content.appendChild(h3);
categoryLink.appendChild(content);
inspirationItem.appendChild(categoryLink);
inspirationImages.appendChild(inspirationItem);
});
}
let url = 'https://www.morgenland-taepper.com/rest/morgenland-look-books';
fetchLookBooks(url);
});