import { initializeApp } from "firebase/app"; import { getFirestore, collection, doc, deleteDoc, onSnapshot, setDoc } from "firebase/firestore"; const firebaseConfig = { apiKey: "AIzaSyD1VzUKXt0JskwyfjfIAbdROzPNB3fTIw0", authDomain: "uc-24070-thegreattipsy.firebaseapp.com", projectId: "uc-24070-thegreattipsy", storageBucket: "uc-24070-thegreattipsy.firebasestorage.app", messagingSenderId: "772804793020", appId: "1:772804793020:web:258003100900c20e0fb6b9", measurementId: "G-1CRHMJY4L9" }; // Initialize Firebase const app = initializeApp(firebaseConfig); const CollectionName="prints"; export function listenToPrints(callback) { // Listen for changes in the prints collection const db = getFirestore(app); const printsRef = collection(db, CollectionName); onSnapshot(printsRef, (snapshot) => { const prints = []; snapshot.forEach((doc) => { const data= doc.data(); if(data.print_url === undefined || data.print_url === null) { console.warn(`Document with id ${doc.id} has no print URL, skipping.`); return; } console.log("Print data:", { id: doc.id, ...data }); prints.push({ id: doc.id, ...data }); }); callback(prints); }); } export function clearPrints() { // Clear the prints collection const db = getFirestore(app); const printsRef = collection(db, CollectionName); onSnapshot(printsRef, (snapshot) => { snapshot.forEach((doc) => { // Assuming you have a delete function to remove documents deleteDoc(doc.ref); }); }); } export async function deletePrint(id) { // Delete a specific print by id const db = getFirestore(app); const printRef = doc(collection(db, CollectionName), id); try{ const result = await deleteDoc(printRef); console.log(`Print with id ${id} deleted successfully.`); }catch (error){ console.error(`Error deleting print with id ${id}:`, error); } } export function createTestFile(){ // Create a test print file const db = getFirestore(app); const testPrint = { url: "https://s3.ap-northeast-2.amazonaws.com/ultracombos.project/24070-%E5%BE%AE%E9%86%BA%E5%A4%A7%E9%A3%AF%E5%BA%97%E9%AB%98%E9%9B%84%E7%89%88/Postcard-01.png", createdAt: new Date().toISOString(), id: `test-${Date.now()}`, status: "pending" }; // Add the test print to the collection setDoc(doc(db, CollectionName, testPrint.id), testPrint) .then(() => { console.log("Test print created successfully."); }) .catch((error) => { console.error("Error creating test print:", error); }); }