function createCard(data) {
var card = document.createElement('div');
card.className = 'card-mietersuche-ms10';
card.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
};