`;
listEl.appendChild(div);
return;
}
sortedStores.forEach((store) => {
const div = document.createElement("div");
const topwrapper = document.createElement("div");
const bottomwrapper = document.createElement("div");
topwrapper.className = "heading-container";
bottomwrapper.className = "bottom-container";
div.className = "store-item";
// Determine display values based on coming soon status
const displayAddress = store.comingsoon ? "Coming Soon" : store.address;
const buttonText = store.comingsoon ? "Notify Me" : "Order Now";
const storeDetails = store.comingsoon ? "" : `${store.phone}
${(store.dist / 1609.34).toFixed(2)} miles`;
div.innerHTML = `
${store.name}
${displayAddress}
${store.actions && store.actions[0] ? `${store.actions[0].label}` : ""}
${storeDetails}
${buttonText}
`;
listEl.appendChild(div);
});
}
function showLocationFallbackMessage() {
const listEl = document.getElementById("store-list");
const messageDiv = document.createElement("div");
messageDiv.id = "location-message";
messageDiv.style.cssText = "padding: 1rem; background-color: #f0f8ff; border: 1px solid #ccc; margin-bottom: 1rem; border-radius: 4px;";
messageDiv.innerHTML = `
Location access denied or unavailable
Please use the search box above to enter your address and find nearby stores.
`;
listEl.appendChild(messageDiv);
}
function geocodeAddress(address) {
if (!address.trim()) {
alert("Please enter an address");
return;
}
const geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, (results, status) => {
if (status === 'OK' && results[0]) {
const location = results[0].geometry.location;
handleUserLocation(location.lat(), location.lng());
} else {
alert("Address not found. Please try a different address.");
}
});
}
// Function to load stores from Firebase Realtime Database
async function loadStoresFromFirebase() {
try {
const storesRef = window.firebaseRef(window.firebaseDb, 'stores');
const snapshot = await window.firebaseGet(storesRef);
stores = [];
if (snapshot.exists()) {
const storesData = snapshot.val();
// Convert object to array and normalize field names
Object.keys(storesData).forEach((key) => {
const store = storesData[key];
stores.push({
id: key,
name: store.Name || store.Name || 'Unnamed Store',
address: store.address || '',
address1: store.address || '',
address2: store.address2 || '',
phone: store.phone || '',
coords: store.coords || { lat: 0, lng: 0 },
orderlink: store.orderlink || '',
comingsoon: store.comingsoon === 'TRUE' || store.comingsoon === true,
actions: store.actions || []
});
});
}
console.log(`Loaded ${stores.length} stores from Firebase`);
initMap();
} catch (error) {
console.error('Error loading stores from Firebase:', error);
showFirebaseError();
}
}
function showFirebaseError() {
const listEl = document.getElementById("store-list");
const errorDiv = document.createElement("div");
errorDiv.style.cssText = "padding: 1rem; background-color: #ffe6e6; border: 1px solid #ff9999; margin-bottom: 1rem; border-radius: 4px; color: #cc0000;";
errorDiv.innerHTML = `
Error loading store data
Unable to connect to Firebase. Please check your internet connection and Firebase configuration.
`;
listEl.appendChild(errorDiv);
}
window.onload = () => {
// Wait a moment for Firebase to initialize, then load stores
setTimeout(loadStoresFromFirebase, 100);
};
window.userLocation = userLocation;