|
| 1 | +const { initializeApp } = require("firebase/app"); |
| 2 | +const { |
| 3 | + getProducts, |
| 4 | + getStripePayments, |
| 5 | +} = require("@invertase/firestore-stripe-payments"); |
| 6 | +// Firebase config |
| 7 | +const { firebaseConfig } = require("./firebase-config.js"); |
| 8 | + |
| 9 | +// Initialize Firebase |
| 10 | +const app = initializeApp(firebaseConfig); |
| 11 | + |
| 12 | +// Initialize Stripe Payments |
| 13 | +const stripePayments = getStripePayments(app, { |
| 14 | + customersCollection: "customers", |
| 15 | + productsCollection: "products", |
| 16 | +}); |
| 17 | + |
| 18 | +async function loadProducts() { |
| 19 | + try { |
| 20 | + const products = await getProducts(stripePayments); |
| 21 | + |
| 22 | + const productsList = |
| 23 | + products.length > 0 |
| 24 | + ? products |
| 25 | + .map( |
| 26 | + (product) => ` |
| 27 | + <div class="product-card"> |
| 28 | + <h3>${product.name}</h3> |
| 29 | + ${product.description ? `<p>${product.description}</p>` : ""} |
| 30 | + ${product.active ? '<span class="active">Active</span>' : ""} |
| 31 | + </div> |
| 32 | + ` |
| 33 | + ) |
| 34 | + .join("") |
| 35 | + : '<p class="no-products">No products found.</p>'; |
| 36 | + |
| 37 | + document.getElementById("app").innerHTML = ` |
| 38 | + <div class="container"> |
| 39 | + <h1>Stripe Products</h1> |
| 40 | + <div class="products-grid"> |
| 41 | + ${productsList} |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + `; |
| 45 | + } catch (error) { |
| 46 | + console.error("Error loading products:", error); |
| 47 | + |
| 48 | + document.getElementById("app").innerHTML = ` |
| 49 | + <div class="container"> |
| 50 | + <h1 class="error">Error</h1> |
| 51 | + <p>Failed to load products: ${error.message}</p> |
| 52 | + </div> |
| 53 | + `; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Load products when page is ready |
| 58 | +if (document.readyState === "loading") { |
| 59 | + document.addEventListener("DOMContentLoaded", loadProducts); |
| 60 | +} else { |
| 61 | + loadProducts(); |
| 62 | +} |
0 commit comments