Korleis kan vi hjelpe deg?
window.addEventListener('load', function () {
let queryString = window.location.search;
let searchParams = new URLSearchParams(queryString);
let categoryParam = searchParams.get('category');
let fetchJson = (url) => fetch(url).then(response => response.json());
const renderCategoryLinks = (categories) => {
const faqCategoriesContainer = document.querySelector('.faq-categories');
var categoryLinks = categories.map(function(category) {
return '
';
}).join('');
faqCategoriesContainer.innerHTML = categoryLinks;
};
const renderFaqs = (faqs) => {
const faqsContainer = document.querySelector('.faqs');
if (faqs.length === 0) {
faqsContainer.innerHTML = '
Ingen resultater funne
';
} else {
const faqItems = faqs.map(function (faq) {
return '
' +
'
' +
'
' + faq.question + ' ' +
'
' +
' ' +
' ' +
'
' +
'
' +
faq.answer +
'
' +
'
';
}).join('');
faqsContainer.innerHTML = faqItems;
const accordionTitles = document.querySelectorAll('.accordion-title');
accordionTitles.forEach(title => {
title.addEventListener('click', () => {
title.classList.toggle('active');
const content = title.nextElementSibling;
content.classList.toggle('collapse');
});
});
}
const faqSchema = document.querySelector('#faq-schema');
faqSchema.textContent = JSON.stringify({
'@context': 'https://schema.org',
'@type': 'FAQPage',
mainEntity: faqs.map((faq) => ({
'@type': 'Question',
name: faq.question,
acceptedAnswer: {
'@type': 'Answer',
text: faq.answer
}
}))
});
};
const fetchAndRenderData = async () => {
try {
const categoriesData = await fetchJson(window.location.origin + '/rest/morgenland-faq-categories');
if (categoriesData.success) {
renderCategoryLinks(categoriesData.data);
}
const faqsData = await fetchJson(window.location.origin + '/rest/morgenland-faqs' + (categoryParam ? '?category=' + categoryParam : ''));
if (faqsData.success) {
renderFaqs(faqsData.data);
}
} catch (error) {
console.error('Error fetching data:', error);
}
};
fetchAndRenderData();
});