diff --git a/modules/mongointerface.js b/modules/mongointerface.js index 16a5a06..b48b296 100644 --- a/modules/mongointerface.js +++ b/modules/mongointerface.js @@ -18,6 +18,8 @@ const doMongo = async function(cmd, options) { await client.connect() if (cmd === 'getdata') { erg = await getAPIdataOne(client, options) + } else if (cmd === 'getlastdata') { + erg = await getAPIlastDataOne(client, options) } else if (cmd === 'putdata') { erg = await putAPIdataOne(client, options) } else if (cmd === 'deldata') { @@ -53,6 +55,18 @@ async function getAPIdataOne(client, options) { return erg } +async function getAPIlastDataOne(client, options) { + let erg = {err: null} + try { + erg.data = await client.db(MONGOBASE).collection(COLLECTION).findOne({},{ projection:{_id: 0}, sort: {$natural: -1}}) + } catch + (e) { + console.error(e) + erg.err = e + } + return erg +} + async function putAPIdataOne(client, options) { let erg = {err: null} const query = {curdate: options.data.curdate} diff --git a/public/javascripts/script.js b/public/javascripts/script.js index 8b46a83..5e69ea4 100644 --- a/public/javascripts/script.js +++ b/public/javascripts/script.js @@ -1,14 +1,16 @@ document.addEventListener('DOMContentLoaded', async function () { const DateTime = luxon.DateTime; - const filename = 'data/data.json' let curdate = '' const dt = DateTime.now() const dt1 = dt.plus({month:1}) - curdate = `${dt.toFormat('LLLL')}/${dt1.toFormat('LLLL y')}` - document.querySelector('#curmon').innerHTML = curdate + curdate = `${dt.toFormat('LLLL')}/${dt1.toFormat('LLLL_y')}` + document.querySelector('#curmon').innerHTML = curdate.replace('_',' ') - let ret = await getData(filename) + +// initSchema('2023-05-01') + + let ret = await getData() fillSchema(ret.data) document.querySelector('#sptab').addEventListener('click', markField); @@ -17,9 +19,7 @@ document.addEventListener('DOMContentLoaded', async function () { let field = e.target; field.setAttribute('aria-label','x'); field.setAttribute('disabled','disabled'); - let lastDate = parseInt(field.innerHTML) - let month = curdate.split('/')[0] - let data = {curdate: curdate.slice(0, -5), last: lastDate} + let data = {curdate: curdate} let setArray = [] let fields = document.querySelectorAll('#sptab button') for (i = 0; i < fields.length; i++) { @@ -31,22 +31,24 @@ document.addEventListener('DOMContentLoaded', async function () { } data.data = setArray storeData(data) + if (e.srcElement.id === 'bt35') { + showNextMonth(e.srcElement.innerHTML) + } } function fillSchema(data) { const setArray = data.data - for(let i = 1, k = 1; i < 36; i++, k++) { - if (i === 18) { - i++ - } - let sel = '#bt'+i - if(setArray[0].day === 'undefined') { - document.querySelector(sel).innerHTML = k + for(let i = 0; i < 35; i++) { + let sel = '#bt'+(i+1) + let day = DateTime.fromISO(setArray[i].day).toFormat('d') + document.querySelector(sel).setAttribute('innerHTML', day) + if (setArray[i].day !== '') { + if (setArray[i].status) { + document.querySelector(sel).setAttribute('aria-label', 'x') + document.querySelector(sel).setAttribute('disabled', 'disabled') + } } else { - document.querySelector(sel).innerHTML = setArray[i-1].day - } - if(setArray[i-1].status) { - document.querySelector(sel).setAttribute('aria-label', 'x') + document.querySelector(sel).setAttribute('disabled', 'disabled') } } } @@ -67,4 +69,30 @@ document.addEventListener('DOMContentLoaded', async function () { return response.json() } + function showNextMonth() { + const dt = DateTime.now() + const dt1 = dt.plus({month:1}) + curdate = `${dt.toFormat('LLLL')}/${dt1.toFormat('LLLL_y')}` + document.querySelector('#curmon').innerHTML = curdate.replace('_',' ') + + fillSchema(ret.data) + } + + function initSchema(startdate) { + let setArray = [] + let ld0 = DateTime.fromISO(startdate) + for(let i = 0; i < 35; i++) { + let elem = {status: false} + if (i === 17) { + elem.day = '' + } else { + let ld = ld0.plus({day: i}) + elem.day = ld.toFormat('y-LL-dd') + } + setArray.push(elem) + } + let data = {curdate: startdate, data: setArray} + storeData(data) + } + }); diff --git a/routes/index.js b/routes/index.js index e87a9bf..cc5c5a5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,7 +10,7 @@ router.get('/', function(req, res, next) { router.get('/data', async function(req, res, next) { const options = {} options.curdate = req.query.curdate - let erg = await domongo.doMongo('getdata', options) + let erg = await domongo.doMongo('getlastdata', options) res.json(erg) })