From 520fb58c3f9883e247754cb37eec2afaaac46906 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Sat, 11 Nov 2023 15:51:18 +0530 Subject: [PATCH] cached old count and make page fast --- src/App.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/App.js b/src/App.js index c134603..9f4a330 100644 --- a/src/App.js +++ b/src/App.js @@ -2,11 +2,17 @@ import './App.css'; import React, { useEffect, useState } from 'react'; import moment from 'moment'; -import { collection, onSnapshot, query } from "firebase/firestore"; +import { collection, onSnapshot, query, where } from "firebase/firestore"; import Card from './components/Card'; import db from './firebase'; function App() { + const registrationCached = 16329; + const malesCached = 6741; + const femalesCached = 9179; + const normalCached = 7153; + const traitsCached = 2312; + const diseasesCached = 365; const [totalScreened, setTotalScreened] = useState(0); const [males, setMales] = useState(0); const [females, setFemales] = useState(0); @@ -19,7 +25,8 @@ function App() { useEffect(() => { - const q = query(collection(db, "patientData")); + // const q = query(collection(db, "patientData")); // cached + const q = query(collection(db, "patientData"), where("testTime", ">=", moment().format("YYYY-MM-DD"))); const unsub = onSnapshot(q, (querySnapshot) => { const patients = []; @@ -36,9 +43,9 @@ function App() { femaleCount++; } }); - setTotalScreened(patients.length); - setMales(maleCount); - setFemales(femaleCount); + setTotalScreened(registrationCached + patients?.length); + setMales(malesCached + maleCount); + setFemales(femalesCached + femaleCount); setLastUpdate(moment()); }); @@ -46,7 +53,7 @@ function App() { }, []); useEffect(() => { - const q = query(collection(db, "testData")); + const q = query(collection(db, "testData"), where("testTime", ">=", moment().format("YYYY-MM-DD"))); const unsub = onSnapshot(q, (querySnapshot) => { const hposTests = []; @@ -68,10 +75,10 @@ function App() { diseaseCount++; } }); - setNormal(normalCount); + setNormal(normalCached + normalCount); // setPositives(traitCount + diseaseCount); - setTraits(traitCount); - setDiseases(diseaseCount); + setTraits(traitsCached + traitCount); + setDiseases(diseasesCached + diseaseCount); setLastUpdate(moment()); });