From 7d3ffe70e5209d3cfac9a6ea2e12e96e52a6441f Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Fri, 20 Aug 2021 16:28:56 +0900 Subject: [PATCH] Change updates to only occur during onBlur or keydown enter events. --- src/App.js | 63 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/App.js b/src/App.js index cc81a69..62ceeca 100644 --- a/src/App.js +++ b/src/App.js @@ -43,6 +43,38 @@ function ItemGroup(p) { } } } + + function submit(f,item,i){ + var correctedVal=Math.min(item.required,f.currentTarget.value); if (f.currentTarget.value>=item.required) {f.currentTarget.blur()} + axios.get(BACKEND_URL+"/lastUpdate") + .then(async(data)=>{ + if (new Date(data.data[0].last_modified)>lastModified) { + console.log("Updating entries... "+[lastModified,data.data[0].last_modified]) + setLastModified(new Date()) + return await axios.get("https://projectdivar.com:4505/getData") + .then((data)=>{ + //setData(data.data) + setData1(data.data.slice(dataSplitters[0],dataSplitters[1])) + setData2(data.data.slice(dataSplitters[1],dataSplitters[2])) + setData3(data.data.slice(dataSplitters[2],dataSplitters[3])) + setData4(data.data.slice(dataSplitters[3],data.data.length)) + }) + } + }) + .then((data)=>{ + axios.post(BACKEND_URL+"/updateItem",{obtained:correctedVal,id:item.id,last_modified:new Date()}); + var newData=[...displayData];newData[i].obtained=correctedVal; + setLastModified(new Date()); + setDisplayData(newData.sort((a,b)=>{ + if (b.required===b.obtained&&a.required!==a.obtained) {return -1} + if (b.required===b.obtained&&a.required===a.obtained) {return a.id-b.id} + if (b.required!==b.obtained&&a.required!==a.obtained) {return a.id-b.id} + })); + }) + .catch((err)=>{ + console.log(err.message) + }) + } return {p.name} @@ -52,36 +84,7 @@ function ItemGroup(p) { {item.name} - {var correctedVal=Math.min(item.required,f.currentTarget.value); if (f.currentTarget.value>=item.required) {f.currentTarget.blur()} - axios.get(BACKEND_URL+"/lastUpdate") - .then(async(data)=>{ - if (new Date(data.data[0].last_modified)>lastModified) { - console.log("Updating entries... "+[lastModified,data.data[0].last_modified]) - setLastModified(new Date()) - return await axios.get("https://projectdivar.com:4505/getData") - .then((data)=>{ - //setData(data.data) - setData1(data.data.slice(dataSplitters[0],dataSplitters[1])) - setData2(data.data.slice(dataSplitters[1],dataSplitters[2])) - setData3(data.data.slice(dataSplitters[2],dataSplitters[3])) - setData4(data.data.slice(dataSplitters[3],data.data.length)) - }) - } - }) - .then((data)=>{ - axios.post(BACKEND_URL+"/updateItem",{obtained:correctedVal,id:item.id,last_modified:new Date()}); - var newData=[...displayData];newData[i].obtained=correctedVal; - setLastModified(new Date()); - setDisplayData(newData.sort((a,b)=>{ - if (b.required===b.obtained&&a.required!==a.obtained) {return -1} - if (b.required===b.obtained&&a.required===a.obtained) {return a.id-b.id} - if (b.required!==b.obtained&&a.required!==a.obtained) {return a.id-b.id} - })); - }) - .catch((err)=>{ - console.log(err.message) - }) - }} type="number" min="0" max={item.required}/> / {item.required} {item.required!==item.obtained&&{ + {if (k.key==='Enter') {submit({currentTarget:document.getElementById("input_"+item.id)},item,i)}}} onBlur={(f)=>{submit(f,item,i)}} type="number" min="0" max={item.required}/> / {item.required} {item.required!==item.obtained&&{ var correctedVal=item.required; if (f.currentTarget.value>=item.required) {f.currentTarget.blur()} axios.post(BACKEND_URL+"/updateItem",{obtained:correctedVal,id:item.id,last_modified:new Date()}); var newData=[...displayData];newData[i].obtained=correctedVal;p.setLastModified(new Date());setDisplayData(newData.sort((a,b)=>{ if (b.required===b.obtained&&a.required!==a.obtained) {return -1} if (b.required===b.obtained&&a.required===a.obtained) {return a.id-b.id}