// assets/js/categories.js
import { db, collection, getDocs } from "./firebase-config.js";
import { AppState, initAuthState } from "./state.js";
import { initNavbar, getPathPrefix } from "./navbar.js";
import { initDrawer } from "./drawer.js";
import "./pincode.js";
import "./modals.js";
/**
* Fetch and Render Full Categories Grid with Subcategories
*/
export const renderFullCategoriesPage = async () => {
const container = document.getElementById('full-categories-container');
if (!container) return;
try {
const snap = await getDocs(collection(db, "categories"));
const categories = [];
snap.forEach(d => {
const cat = d.data();
cat.id = d.id;
categories.push(cat);
});
AppState.allCategories = categories;
const rootCats = categories.filter(c => c.parentId === 'root');
const prefix = getPathPrefix();
if (rootCats.length === 0) {
container.innerHTML = `
No categories found.
`;
return;
}
let html = "";
rootCats.forEach(parentCat => {
const subCats = categories.filter(c => c.parentId === parentCat.id);
html += `
`;
});
container.innerHTML = html;
} catch (e) {
console.error("Categories Explorer error:", e);
container.innerHTML = `Failed to load categories.
`;
}
};
/**
* Filter Categories List by Search Input
* @param {string} val
*/
export const filterCategoriesList = (val) => {
const q = val.toLowerCase().trim();
document.querySelectorAll('.category-section-block').forEach(block => {
const name = block.getAttribute('data-cat-name') || '';
const text = block.innerText.toLowerCase();
if (name.includes(q) || text.includes(q)) {
block.style.display = 'block';
} else {
block.style.display = 'none';
}
});
};
// Global Window Bindings
window.filterCategoriesList = filterCategoriesList;
/**
* Shared Component Injector
*/
const loadComponent = async (url, containerId) => {
try {
const res = await fetch(url);
if (res.ok) {
const html = await res.text();
const container = document.getElementById(containerId);
if (container) container.innerHTML = html;
}
} catch (e) {
console.warn(`Failed to inject component: ${url}`, e);
}
};
document.addEventListener("DOMContentLoaded", async () => {
await loadComponent("../components/header.html", "header-component-container");
await loadComponent("../components/drawer.html", "drawer-component-container");
await loadComponent("../components/bottom-nav.html", "bottom-nav-container");
await loadComponent("../components/modals.html", "modals-container");
await initNavbar();
await initDrawer();
await renderFullCategoriesPage();
initAuthState((user) => {
const loginBtn = document.getElementById("navLoginBtn");
const profileDiv = document.getElementById("navProfileDiv");
const userName = document.getElementById("navUserName");
const drawerUserName = document.getElementById("drawerUserName");
const drawerLoginText = document.getElementById("drawerLoginText");
if (user) {
if (loginBtn) loginBtn.classList.add("hide");
if (profileDiv) profileDiv.classList.remove("hide");
const name = user.displayName || user.email.split('@')[0];
if (userName) userName.innerText = name;
if (drawerUserName) drawerUserName.innerText = name;
if (drawerLoginText) drawerLoginText.innerText = "My Account";
} else {
if (loginBtn) loginBtn.classList.remove("hide");
if (profileDiv) profileDiv.classList.add("hide");
if (drawerUserName) drawerUserName.innerText = "Guest User";
if (drawerLoginText) drawerLoginText.innerText = "Login & Signup";
}
});
});