From 5ecdf3adb772597489d7174bc5fe3d8212c66dc8 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 3 Oct 2024 09:37:04 +0200 Subject: [PATCH] enable data saving --- api/data/batu.json | 2 +- api/src/app.ts | 23 ++++++++++++++++------- eslint.config.mjs | 6 ++---- ui/src/components/formView.vue | 25 ++++++++++++++++++++++++- ui/src/components/statsView.vue | 1 + 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/api/data/batu.json b/api/data/batu.json index 9e26dfe..8ee082f 100644 --- a/api/data/batu.json +++ b/api/data/batu.json @@ -1 +1 @@ -{} \ No newline at end of file +{"2024-10-03T07:36:00.000Z":{"day":"2024-10-03T07:36:00.000Z","count":0,"difficulty":3,"sleep":7,"rest":3,"freeTime":2,"stress":3}} \ No newline at end of file diff --git a/api/src/app.ts b/api/src/app.ts index c1ef419..12bb0b6 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -11,9 +11,7 @@ if ( typeof __dirname === 'undefined' ) { } declare module 'express-session' { - interface SessionData { - 'isAuth': boolean; - } + interface SessionData { 'isAuth': boolean; } } const run = () => { @@ -30,7 +28,6 @@ const run = () => { } ) ); app.set( 'trust proxy', 1 ); - app.use( bodyParser.urlencoded( { 'extended': false } ) ); app.use( bodyParser.json() ); @@ -62,6 +59,7 @@ const run = () => { let data = {}; try { data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) ); + // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch ( err ) { /* empty */ } response.send( data ); } else { @@ -83,12 +81,23 @@ const run = () => { stressLevel: number; */ - if ( body.count && body.day && body.difficulty ) { + if ( body.count !== undefined && body.day !== undefined && body.difficulty !== undefined ) { let data = {}; try { data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) ); - } catch ( err ) { /* empty */ } - data[ body.day ] = ''; + } catch ( err ) { + console.error( err ); + response.status( 500 ).send( 'ERR_STORING' ); + return; + } + data[ body.day ] = body; + try { + fs.writeFileSync( path.join( __dirname + '/data/batu.json' ), JSON.stringify( data ) ); + } catch ( err ) { + console.error( err ); + response.status( 500 ).send( 'ERR_STORING' ); + return; + } response.send( 'stored' ); } else { response.status( 400 ).send( 'ERR_INVALID_REQ' ); diff --git a/eslint.config.mjs b/eslint.config.mjs index 9bbadeb..9f9fab8 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -7,9 +7,7 @@ import stylistic from '@stylistic/eslint-plugin'; export default tseslint.config( eslint.configs.recommended, ...tseslint.configs.recommended, - { - 'ignores': [ '**/dist/', '**/*.cjs' ], - }, + { 'ignores': [ '**/dist/', '**/*.cjs' ], }, { 'plugins': { '@stylistic/js': stylistic, @@ -23,7 +21,7 @@ export default tseslint.config( '@stylistic/js/arrow-parens': [ 'error', 'always' ], '@stylistic/js/arrow-spacing': [ 'error', { 'before': true, 'after': true } ], '@stylistic/js/block-spacing': [ 'error', 'always' ], - '@stylistic/js/brace-style': [ 'error', '1tbs' ], + // '@stylistic/js/brace-style': [ 'error', '1tbs' ], '@stylistic/js/comma-spacing': [ 'error', { 'before': false, 'after': true } ], '@stylistic/js/comma-style': [ 'error', 'last' ], '@stylistic/js/dot-location': [ 'error', 'property' ], diff --git a/ui/src/components/formView.vue b/ui/src/components/formView.vue index d29374b..b0bb8e1 100644 --- a/ui/src/components/formView.vue +++ b/ui/src/components/formView.vue @@ -118,7 +118,30 @@ } const sendForm = () => { - alert( 'Data submitted. Remember: Stop smoking! Smoking hurts your health!' ); + fetch( localStorage.getItem( 'url' ) + '/update', { + method: 'post', + body: JSON.stringify( { + day: date.value?.toISOString(), + count: cigaretCount.value, + difficulty: difficulty.value, + sleep: sleepHours.value, + rest: rest.value, + freeTime: freeTime.value, + stress: stress.value + } ), + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + 'charset': 'utf-8' + } + } ).then( res => { + if ( res.status === 200 ) { + alert( 'Data submitted. Remember: Stop smoking! Smoking hurts your health!' ); + } else { + alert( `Failed to save data (${ res.status })` ); + } + } ); + } const dateUpdatedHandler = () => { diff --git a/ui/src/components/statsView.vue b/ui/src/components/statsView.vue index 4329558..653a057 100644 --- a/ui/src/components/statsView.vue +++ b/ui/src/components/statsView.vue @@ -43,6 +43,7 @@ transform-origin: center; transform: scale(0); transition: all 0.5s; + z-index: 50; } .stats-view-shown {