Seleziona un tipo di tappeto
Failed to load data. Please try again.
var categoryList = document.getElementById('inspiration-category');
var filterSelect = document.getElementById('inspiration-filter');
var inspirationImages = document.getElementById('inspiration-images');
var loadingIndicator = document.getElementById('loading-indicator');
var errorMessage = document.getElementById('error-message');
function debounce(fn, delay) {
var timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(fn, delay);
};
}
function fetchLookBooks(url) {
toggleLoading(true);
inspirationImages.innerHTML = '';
errorMessage.style.display = 'none';
fetch(url)
.then(function (response) {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
})
.then(function (data) {
if(data.data.length <= 0) throw new Error('No data available');
displayLookBooks(data.data);
})
.catch(function (error) {
errorMessage.style.display = 'block';
console.error('Error fetching look books:', error);
})
.finally(function () {
toggleLoading(false);
});
}
function displayLookBooks(items) {
inspirationImages.innerHTML = '';
items.forEach(function (item) {
var inspirationItem = document.createElement('div');
inspirationItem.classList.add('inspiration-item');
inspirationItem.innerHTML =
'
' +
'
' +
' ' +
' ' +
'
' +
'
' +
'' +
'
' + item.category_name + '
' +
'
' +
' ';
inspirationImages.appendChild(inspirationItem);
});
}
function fetchCategories() {
toggleLoading(true);
filterSelect.innerHTML = '
Seleziona un tipo di tappeto ';
categoryList.innerHTML = '';
errorMessage.style.display = 'none';
fetch('https://www.morgenland-tappeto.it/rest/morgenland-category-groups')
.then(function (response) {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
})
.then(function (data) {
var uniqueFilterOptions = {};
data.data.forEach(function (group) {
var groupListItem = document.createElement('li');
var 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) {
if (!uniqueFilterOptions[category.name]) {
uniqueFilterOptions[category.name] = true;
var option = document.createElement('option');
option.value = category.id;
option.textContent = category.name;
filterSelect.appendChild(option);
}
});
});
filterSelect.addEventListener('change', handleFilterChange);
})
.catch(function (error) {
errorMessage.style.display = 'block';
console.error('Error fetching categories:', error);
})
.finally(function () {
toggleLoading(false);
});
}
var handleFilterChange = debounce(function () {
var selectedOption = filterSelect.value;
var url = 'https://www.morgenland-tappeto.it/rest/morgenland-look-books';
if (selectedOption) url += '?category=' + encodeURIComponent(selectedOption);
fetchLookBooks(url);
}, 300);
function toggleLoading(isLoading) {
loadingIndicator.style.display = isLoading ? 'block' : 'none';
filterSelect.disabled = isLoading;
}
window.addEventListener('load', function () {
fetchCategories();
var urlParams = new URLSearchParams(window.location.search);
var categoryGroup = urlParams.get('group');
var url = 'https://www.morgenland-tappeto.it/rest/morgenland-look-books';
if (categoryGroup) url += '?group=' + encodeURIComponent(categoryGroup);
fetchLookBooks(url);
});