|
|
|
@ -1,6 +1,6 @@ |
|
|
|
// Import the functions you need from the SDKs you need
|
|
|
|
// Import the functions you need from the SDKs you need
|
|
|
|
import { initializeApp } from "firebase/app"; |
|
|
|
import { initializeApp } from "firebase/app"; |
|
|
|
import { getFirestore, collection, doc, writeBatch, setDoc } from "firebase/firestore"; |
|
|
|
import { getFirestore, collection, doc, writeBatch, setDoc, onSnapshot, getDoc } from "firebase/firestore"; |
|
|
|
// TODO: Add SDKs for Firebase products that you want to use
|
|
|
|
// TODO: Add SDKs for Firebase products that you want to use
|
|
|
|
// https://firebase.google.com/docs/web/setup#available-libraries
|
|
|
|
// https://firebase.google.com/docs/web/setup#available-libraries
|
|
|
|
|
|
|
|
|
|
|
|
@ -22,31 +22,38 @@ const app = initializeApp(firebaseConfig); |
|
|
|
|
|
|
|
|
|
|
|
export function addKeywords(keywords) { |
|
|
|
export function addKeywords(keywords) { |
|
|
|
|
|
|
|
|
|
|
|
console.log('Adding keywords to Firestore:', keywords); |
|
|
|
// console.log('Adding keywords to Firestore:', keywords);
|
|
|
|
|
|
|
|
|
|
|
|
const sortedKeywords = [...keywords].sort((a, b) => a.id - b.id); |
|
|
|
// const sortedKeywords = [...keywords].sort((a, b) => a.id - b.id);
|
|
|
|
|
|
|
|
|
|
|
|
const db = getFirestore(app); |
|
|
|
// const db = getFirestore(app);
|
|
|
|
const batch = writeBatch(db); |
|
|
|
// const batch = writeBatch(db);
|
|
|
|
|
|
|
|
|
|
|
|
sortedKeywords.forEach((keyword) => { |
|
|
|
// sortedKeywords.forEach((keyword) => {
|
|
|
|
const keywordRef = doc(db, "keywords", keyword.id.toString()); |
|
|
|
// const keywordRef = doc(db, "keywords", keyword.id.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const keywordDoc = { |
|
|
|
// const keywordDoc = {
|
|
|
|
...keyword, |
|
|
|
// ...keyword,
|
|
|
|
updatedTime: new Date(), |
|
|
|
// updatedTime: new Date(),
|
|
|
|
}; |
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
batch.set(keywordRef, keywordDoc); |
|
|
|
// batch.set(keywordRef, keywordDoc);
|
|
|
|
}); |
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// batch.commit()
|
|
|
|
|
|
|
|
// .then(() => {
|
|
|
|
|
|
|
|
// console.log("Keywords added successfully!");
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// .catch((error) => {
|
|
|
|
|
|
|
|
// console.error("Error adding keywords: ", error);
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
batch.commit() |
|
|
|
|
|
|
|
.then(() => { |
|
|
|
setState({ |
|
|
|
console.log("Keywords added successfully!"); |
|
|
|
state: 'keywords', |
|
|
|
}) |
|
|
|
keywords: keywords, |
|
|
|
.catch((error) => { |
|
|
|
updatedTime: new Date(), |
|
|
|
console.error("Error adding keywords: ", error); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
@ -65,3 +72,47 @@ export function lookatKeyword(keyword) { |
|
|
|
updatedTime: new Date(), |
|
|
|
updatedTime: new Date(), |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setState(state) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('Setting state in Firestore:', state); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const db = getFirestore(app); |
|
|
|
|
|
|
|
const stateRef = doc(collection(db, "state"), 'latest_page'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setDoc(stateRef, { |
|
|
|
|
|
|
|
...state, |
|
|
|
|
|
|
|
updatedTime: new Date(), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function onStateChange(callback) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const db = getFirestore(app); |
|
|
|
|
|
|
|
const stateRef = doc(collection(db, "state"), 'latest_page'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const unsubscribe = onSnapshot(stateRef, (doc) => { |
|
|
|
|
|
|
|
if (doc.exists()) { |
|
|
|
|
|
|
|
callback(doc.data()); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
console.log("No such document!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return unsubscribe; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getState(){ |
|
|
|
|
|
|
|
const db = getFirestore(app); |
|
|
|
|
|
|
|
const stateRef = doc(collection(db, "state"), 'latest_page'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const state_doc=await getDoc(stateRef); |
|
|
|
|
|
|
|
if (state_doc.exists()) { |
|
|
|
|
|
|
|
return state_doc.data(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
console.log("No such document!"); |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|