function createCard(data) { var card = document.createElement('div'); card.className = 'card-mietersuche-ms10'; card.innerHTML = `

${data.title}

${data.street} ${data.no_street}, ${data.postcode} ${data.city}
seit ${Math.floor((Date.now() - data.created_at) / (1000 * 60 * 60 * 24))} Tagen auf Suche nach einem Mieter
`; return card; } // Tab navigation functionality function initializeTabs() { const ctaAll = document.getElementById('cta-all'); const ctaListings = document.getElementById('cta-listings'); const ctaPolls = document.getElementById('cta-polls'); if (ctaAll) { ctaAll.addEventListener('click', () => showAllItems()); } if (ctaListings) { ctaListings.addEventListener('click', () => showListingsOnly()); } if (ctaPolls) { ctaPolls.addEventListener('click', () => showPollsOnly()); } } // Function to format unix timestamp to readable date function formatDate(unixTimestamp) { const date = new Date(unixTimestamp); const now = new Date(); const diffTime = Math.abs(now - date); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); if (diffDays === 1) { return 'created 1 day ago'; } else if (diffDays < 30) { return `created ${diffDays} days ago`; } else if (diffDays < 60) { return 'created 1 month ago'; } else { const months = Math.floor(diffDays / 30); return `created ${months} months ago`; } } // Function to create result items function createResultItem(item, type) { const resultItem = document.createElement('div'); resultItem.className = 'item-results-overview-properties-provider'; resultItem.dataset.type = type; // Add data attribute for filtering const thumbnailStyle = item.image ? `style="background-image: url('${item.image}')"` : ''; resultItem.innerHTML = `
${item.title}
${formatDate(item.created_at)}
`; // Make item clickable resultItem.style.cursor = 'pointer'; resultItem.addEventListener('click', () => { if (item.link) { window.location.href = item.link; } }); return resultItem; } // Function to populate results container async function fetchAndPopulateResults() { try { const response = await fetch("https://api.hems-app.com/api:xnyf9bWQ/10/properties", { method: 'GET', credentials: 'include', mode: 'cors' }); if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } const data = await response.json(); const resultsContainer = document.getElementById('results-cont'); if (!resultsContainer) { console.error('results-cont element not found'); return; } // Clear existing content resultsContainer.innerHTML = ''; // Collect all items and sort by created_at (latest first) let allItems = []; // Add listings if they exist if (data.listings) { data.listings.forEach(item => { allItems.push({...item, type: 'listing'}); }); } // Add polls if they exist if (data.polls) { data.polls.forEach(item => { allItems.push({...item, type: 'poll'}); }); } // Sort by created_at (latest first) allItems.sort((a, b) => b.created_at - a.created_at); // Create and append sorted items allItems.forEach(item => { const resultItem = createResultItem(item, item.type); resultsContainer.appendChild(resultItem); }); } catch (error) { console.error('Error fetching results:', error); } } // Filter functions function showAllItems() { const items = document.querySelectorAll('.item-results-overview-properties-provider'); items.forEach(item => { item.classList.remove('hidden'); }); updateActiveTab('cta-all'); } function showListingsOnly() { const items = document.querySelectorAll('.item-results-overview-properties-provider'); items.forEach(item => { if (item.dataset.type === 'listing') { item.classList.remove('hidden'); } else { item.classList.add('hidden'); } }); updateActiveTab('cta-listings'); } function showPollsOnly() { const items = document.querySelectorAll('.item-results-overview-properties-provider'); items.forEach(item => { if (item.dataset.type === 'poll') { item.classList.remove('hidden'); } else { item.classList.add('hidden'); } }); updateActiveTab('cta-polls'); } // Update active tab styling function updateActiveTab(activeId) { const tabs = ['cta-all', 'cta-listings', 'cta-polls']; tabs.forEach(tabId => { const tab = document.getElementById(tabId); if (tab) { if (tabId === activeId) { tab.classList.add('active'); } else { tab.classList.remove('active'); } } }); } // Add CSS for hidden class const style = document.createElement('style'); style.textContent = ` .item-results-overview-properties-provider.hidden { display: none !important; } `; document.head.appendChild(style); // Initialize everything when page loads window.onload = () => { // Authentication and profile picture logic const fetchData = (url, callback) => { fetch(url, { method: 'GET', credentials: 'include', mode: 'cors' }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => callback(data)) .catch(error => { console.error('Error:', error); window.location.href = '/anmelden'; }); }; fetchData('https://api.hems-app.com/api:xnyf9bWQ/auth', data => { if (!data.isAuthenticated) { window.location.href = '/anmelden'; } }); fetchData('https://api.hems-app.com/api:xnyf9bWQ/profilepic', data => { if(data.profilepicture) { let imageProfilePicture = document.createElement('img'); imageProfilePicture.src = data.profilepicture; imageProfilePicture.classList.add('image-profilepicture-landlord'); document.getElementById("profilepicture").appendChild(imageProfilePicture); } }); // Initialize functionality fetchAndPopulateResults(); // Add new results functionality initializeTabs(); // Initialize tab navigation };