kirche-im-netz.Startodon-Hub/src/utils/starterkit-utils.js

70 lines
2.5 KiB
JavaScript
Raw Normal View History

import { createRecommendationPopup, populateAuthorsContainer } from './recommendations-utils.js';
import { checkConsent, showConsentPopup, setUserConsent } from '../components/consentManager.js';
// StarterKit-Element erstellen
export function createStarterKitElement(kit) {
const template = document.getElementById('starterkit-template').content.cloneNode(true);
2024-12-12 22:30:51 +01:00
// Titel und Beschreibung
const kitElement = template.querySelector('.starterkit');
kitElement.querySelector('h3').textContent = kit.name;
kitElement.querySelector('p').textContent = kit.description;
// Profilanzahl
const profileCount = kitElement.querySelector('.profile-count');
2024-12-14 22:10:17 +01:00
profileCount.querySelector('.profile-line2').textContent = `${kit.accounts.length}`;
// Autoren hinzufügen
2024-12-19 20:40:34 +01:00
const authorsContainer = kitElement.querySelector('.authors-container');
if (!authorsContainer) {
console.error('Fehlendes Element: .authors-container im Template. Bitte prüfen!');
return kitElement;
2024-12-19 20:40:34 +01:00
}
populateAuthorsContainer(authorsContainer, kit.authors);
kitElement.dataset.accounts = JSON.stringify(kit.accounts);
return kitElement;
}
// StarterKits mit Event-Listenern versehen
export function enhanceStarterKits() {
const starterkits = document.querySelectorAll('.starterkit');
starterkits.forEach(kit => {
kit.addEventListener('click', function () {
handleStarterKitClick(kit);
});
});
}
2024-12-14 22:10:17 +01:00
// StarterKit-Klick behandeln
function handleStarterKitClick(kit) {
if (checkConsent()) {
// Zustimmung vorhanden StarterKit-Popup öffnen
loadStarterKitProfiles(kit);
} else {
// Keine Zustimmung Consent-Popup anzeigen
showConsentPopup(
() => {
setUserConsent(true); // Zustimmung speichern
loadStarterKitProfiles(kit); // Nachträglich StarterKit-Popup öffnen
},
() => {
console.log('Consent wurde abgelehnt. StarterKit-Popup wird nicht geöffnet.');
}
);
}
}
// StarterKit-Profile laden
2024-12-14 22:10:17 +01:00
function loadStarterKitProfiles(kit) {
const title = kit.querySelector('h3').textContent;
const description = kit.querySelector('p').textContent;
2024-12-14 22:10:17 +01:00
const accounts = JSON.parse(kit.dataset.accounts || '[]');
const authors = Array.from(kit.querySelectorAll('.authors-container .account')).map(author => author.href);
const popup = createRecommendationPopup(accounts, title, authors, description);
2024-12-14 22:10:17 +01:00
document.body.appendChild(popup);
}