The Svelte web app code and the Firebase JavaScript file you've provided indicate a setup for reactive data displays using Svelte stores. However, there are several issues and improvements that can be made to ensure that the app behaves as expected. Below, I'll provide an overview of the fixes and suggestions:
Problems and Solutions:
1. Firestore Data Fetching and Subscription:
In your .js file for the Firebase database, you set up an onSnapshot listener that updates the people store with the size of the snapshot. However, there's a misunderstanding here; snapshot.size provides the total number of documents, not the number of people detected if one document doesn't necessarily represent one individual detected.
You start with export let people = writable(0), which is fine to initialize the store, but then down in your snapshot listener, you're updating the people store with snapshot.size. What you probably want to do is have a listener that accumulates the count of people based on some property within your documents.
Moreover, your function getTotalNumberOfPeople() seems to be attempting an initial fetch of the data, but subsequently, it's not clear how the data is to be processed to get the number of detected people based on the Direction attribute ('up' or 'down') as your Svelte script expects.
2. Svelte Store Subscription:
Inside your Svelte component, you are subscribing to the Firestore data using several store subscriptions (maxOccupants, terminalCapacity, units, people), which look appropriate. However, the way people is being updated might not accord with your logic within the Svelte component since you're not processing directions ('up' or 'down') within the subscription handler in the Firebase file.
3. Inconsistent and Redundant Data:
Your Svelte script component has several data arrays that look redundant. For example, byhour and predbyhour contain repeated objects with the same text descriptions and the same images. It's important to clean up and remove any such redundant data to avoid confusion and ensure clarity of the code.
Making It Work:
To ensure that your website app displays the number of people detected and is reactive to changes in real-time, consider the following steps:
A. Update Your Firestore Listener to Correctly Process Detected People:
onSnapshot(collection(db, "Detections"), (snapshot) => {
let upCount = 0;
let downCount = 0;
snapshot.docs.forEach(doc => {
const data = doc.data();
if (data.Direction === "up") {
upCount++;
} else if (data.Direction === "down") {
downCount--;
}
});
const netPeopleCount = upCount + downCount;
people.set(netPeopleCount); // Update the store with the net count of people
}, error => {
console.error(error);
});
B. Remove getTotalNumberOfPeople Function:
Since you are using a real-time listener (onSnapshot), there's no need for the getTotalNumberOfPeople function. The listener itself should handle the initial data fetch and subsequent updates.
C. Ensure Svelte Store Reflects Correct Data:
Ensure that your Svelte store definitions and data transformations reflect the logic of how a "person detected" is being stored and updated in Firestore.
D. Cleanup and Refactor:
Go through your Svelte script component to remove any unused or redundant variables and to ensure that the logic aligns with the real-time updates coming from Firestore. Confirm that the store subscriptions update all necessary component state variables whenever changes are detected.
By following these steps, you should be able to achieve a reactive web app that dynamically displays the number of people detected in real-time based on the Firestore Detections collection updates.