enable data saving

This commit is contained in:
2024-10-03 09:37:04 +02:00
parent 9f2a51b911
commit 5ecdf3adb7
5 changed files with 44 additions and 13 deletions

View File

@@ -1 +1 @@
{} {"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}}

View File

@@ -11,9 +11,7 @@ if ( typeof __dirname === 'undefined' ) {
} }
declare module 'express-session' { declare module 'express-session' {
interface SessionData { interface SessionData { 'isAuth': boolean; }
'isAuth': boolean;
}
} }
const run = () => { const run = () => {
@@ -30,7 +28,6 @@ const run = () => {
} ) ); } ) );
app.set( 'trust proxy', 1 ); app.set( 'trust proxy', 1 );
app.use( bodyParser.urlencoded( { 'extended': false } ) );
app.use( bodyParser.json() ); app.use( bodyParser.json() );
@@ -62,6 +59,7 @@ const run = () => {
let data = {}; let data = {};
try { try {
data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) ); data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) );
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch ( err ) { /* empty */ } } catch ( err ) { /* empty */ }
response.send( data ); response.send( data );
} else { } else {
@@ -83,12 +81,23 @@ const run = () => {
stressLevel: number; stressLevel: number;
*/ */
if ( body.count && body.day && body.difficulty ) { if ( body.count !== undefined && body.day !== undefined && body.difficulty !== undefined ) {
let data = {}; let data = {};
try { try {
data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) ); data = JSON.parse( '' + fs.readFileSync( path.join( __dirname + '/data/batu.json' ) ) );
} catch ( err ) { /* empty */ } } catch ( err ) {
data[ body.day ] = ''; 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' ); response.send( 'stored' );
} else { } else {
response.status( 400 ).send( 'ERR_INVALID_REQ' ); response.status( 400 ).send( 'ERR_INVALID_REQ' );

View File

@@ -7,9 +7,7 @@ import stylistic from '@stylistic/eslint-plugin';
export default tseslint.config( export default tseslint.config(
eslint.configs.recommended, eslint.configs.recommended,
...tseslint.configs.recommended, ...tseslint.configs.recommended,
{ { 'ignores': [ '**/dist/', '**/*.cjs' ], },
'ignores': [ '**/dist/', '**/*.cjs' ],
},
{ {
'plugins': { 'plugins': {
'@stylistic/js': stylistic, '@stylistic/js': stylistic,
@@ -23,7 +21,7 @@ export default tseslint.config(
'@stylistic/js/arrow-parens': [ 'error', 'always' ], '@stylistic/js/arrow-parens': [ 'error', 'always' ],
'@stylistic/js/arrow-spacing': [ 'error', { 'before': true, 'after': true } ], '@stylistic/js/arrow-spacing': [ 'error', { 'before': true, 'after': true } ],
'@stylistic/js/block-spacing': [ 'error', 'always' ], '@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-spacing': [ 'error', { 'before': false, 'after': true } ],
'@stylistic/js/comma-style': [ 'error', 'last' ], '@stylistic/js/comma-style': [ 'error', 'last' ],
'@stylistic/js/dot-location': [ 'error', 'property' ], '@stylistic/js/dot-location': [ 'error', 'property' ],

View File

@@ -118,7 +118,30 @@
} }
const sendForm = () => { const sendForm = () => {
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!' ); alert( 'Data submitted. Remember: Stop smoking! Smoking hurts your health!' );
} else {
alert( `Failed to save data (${ res.status })` );
}
} );
} }
const dateUpdatedHandler = () => { const dateUpdatedHandler = () => {

View File

@@ -43,6 +43,7 @@
transform-origin: center; transform-origin: center;
transform: scale(0); transform: scale(0);
transition: all 0.5s; transition: all 0.5s;
z-index: 50;
} }
.stats-view-shown { .stats-view-shown {