From 83885ef81f8f33f6fe433b271505a3045b29f88a Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Sun, 22 Aug 2021 23:14:00 +0900 Subject: [PATCH] Notification system in progress --- src/App.js | 65 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/src/App.js b/src/App.js index b9bb689..3952d7b 100644 --- a/src/App.js +++ b/src/App.js @@ -98,6 +98,12 @@ function App() { const [lastModified,setLastModified] = useState(new Date()) const [contributor,setContributor] = useState("") + const [notifications,setNotifications] = useState([]) + const [notificationLastUpdate,setNotificationLastUpdate] = useState(new Date()) + + function LZ(digits,numb) { + return "0".repeat(digits-String(numb).length)+numb + } useEffect(()=>{ const interval = setInterval(()=>{ @@ -122,6 +128,36 @@ function App() { },1000) return ()=>clearInterval(interval) },[lastModified]) + + useEffect(()=>{ + const interval = setInterval(()=>{ + axios.get(BACKEND_URL+"/getNotifications?date="+encodeURIComponent(LZ(4,notificationLastUpdate.getUTCFullYear())+"-"+LZ(2,notificationLastUpdate.getUTCMonth()+1)+"-"+LZ(2,notificationLastUpdate.getUTCDate())+" "+LZ(2,notificationLastUpdate.getUTCHours())+":"+LZ(2,notificationLastUpdate.getUTCMinutes())+":"+LZ(2,notificationLastUpdate.getUTCSeconds())+"."+LZ(3,notificationLastUpdate.getUTCMilliseconds())+"+00")) + .then((data)=>{ + if (data.data.length>0) { + var newNotifications = [...notifications] + for (var dat of data.data) { + var exists=false + for (var not of newNotifications) { + if (not.id===dat.id) { + exists=true + break; + } + } + if (!exists) { + newNotifications.push(dat) + } + } + console.log("New notification array: "+JSON.stringify(newNotifications)) + setNotifications(newNotifications) + setNotificationLastUpdate(new Date()) + } + }) + .catch((err)=>{ + console.log(err.message) + }) + },1000) + return ()=>clearInterval(interval) + },[notificationLastUpdate]) const disabled=true @@ -197,6 +233,10 @@ function App() { downloadData(d,0,d.length) setTotal(d.length) },[fileData]) + + useEffect(()=>{ + console.log(notifications) + },[notifications]) return ( @@ -216,10 +256,10 @@ function App() { data.length>0? <> - - - - + + + + : !disabled&& @@ -246,12 +286,17 @@ function App() { }
- {console.log("closing")}} bg="primary"> - - Testing - 11 mins ago - - + {notifications.map((not)=>{ + return {var newArr = notifications.filter((no)=>no.id!==not.id); setNotifications(newArr)}} bg="primary"> + + + {not.username} + {not.operation==="FINISH"?" has finished collecting "+not.required+"/"+not.required+" "+not.item_name+"!": + not.operation==="INCREASE"?" has collected "+not.obtained+"/"+not.required+" "+not.item_name+" (+"+(not.obtained-not.previous_amt)+")" + :" has set "+not.item_name+" to "+not.obtained+"/"+not.required} + + + })}