2021-04-12 21:20:09 +02:00
var spage = document . getElementById ( 'startpage' )
var mform = document . getElementById ( 'mainform' )
2021-06-08 00:49:42 +02:00
if ( qp . action ) {
2021-04-12 21:20:09 +02:00
spage . style . display = 'none'
mform . style . display = 'block'
}
2021-10-30 20:45:45 +02:00
// Get the name field if it was successfully entered before
2021-04-12 21:20:09 +02:00
var savedName = localStorage . getItem ( 'name' )
2021-10-30 20:45:45 +02:00
if ( qp && qp . name ) {
// Forced Admin checkout - prefill qp name and auto-agree
document . getElementById ( 'name' ) . value = qp . name . replace ( /-/g , ' ' ) . toUpperCase ( ) ;
document . getElementById ( 'agree' ) . checked = true
document . getElementById ( 'agree' ) . parentElement . style . display = 'none'
} else if ( savedName && qp ) {
// Prefill the client's locally saved name
2021-04-12 21:20:09 +02:00
document . getElementById ( 'name' ) . value = savedName
2021-10-30 20:45:45 +02:00
}
2021-04-12 21:20:09 +02:00
// 2nd script, server API communication
2021-06-08 00:49:42 +02:00
var name , datetime , agreed , tested
2021-04-12 21:20:09 +02:00
mform . onsubmit = function ( e ) {
e . preventDefault ( )
2021-06-08 00:49:42 +02:00
var i = 0 ;
name = e . srcElement [ i ++ ] . value
if ( qp . edittime && qp . edittime == 1 ) {
var value = e . srcElement [ i ++ ] . value
datetime = new Date ( value ) . toISOString ( )
}
agreed = e . srcElement [ i ++ ] . checked
if ( qp . action && qp . action == 'arrival' )
tested = e . srcElement [ i ++ ] . checked
2021-04-12 21:20:09 +02:00
sendMainData ( )
2021-06-08 00:49:42 +02:00
initPush ( name )
2021-04-12 21:20:09 +02:00
}
function sendMainData ( ) {
// POST JSON. See docs/API.md
var payload = ( qp . action == 'arrival' ) ?
{
'room' : qp . room ,
'name' : name ,
2021-06-08 00:49:42 +02:00
'arrival' : datetime ,
2021-04-12 21:20:09 +02:00
'agreetoguidelines' : agreed ,
2021-10-14 13:45:00 +02:00
'tested' : tested // = 3G
2021-04-12 21:20:09 +02:00
} :
{
'name' : name ,
2021-06-08 00:49:42 +02:00
'departure' : datetime ,
2021-04-12 21:20:09 +02:00
'cleanedworkspace' : agreed
}
post ( "/" + qp . action , payload )
}
function post ( url , payload ) {
console . log ( "Sending payload:" , payload )
return fetch ( url , {
method : "POST" ,
2021-05-27 16:26:25 +02:00
headers : { 'Content-Type' : 'application/json' } ,
2021-04-12 21:20:09 +02:00
body : JSON . stringify ( payload )
} ) . then ( res => {
handleResponse ( res )
} )
}
function handleResponse ( res ) {
console . log ( "Request complete! response:" , res )
if ( Math . floor ( res . status / 100 ) == 2 ) {
// Success
mform = document . getElementById ( 'mainform' )
mform . innerHTML = "<h2>Done. Thanks!</h2>"
localStorage . setItem ( 'name' , name )
} else if ( res . status == 409 ) {
// Conflict, more data requested
handleRequest ( res )
} else {
// Any other generic error
res . text ( ) . then ( function ( text ) {
alert ( text )
} )
}
}
function handleRequestSubmit ( e , json ) {
e . preventDefault ( )
var input = e . srcElement [ 0 ] . value
var iso = new Date ( input ) . toISOString ( )
2021-05-27 16:26:25 +02:00
if ( e . srcElement . length > 1 )
2021-10-14 13:45:00 +02:00
tested = e . srcElement [ 1 ] . checked // = 3G
2021-05-27 16:26:25 +02:00
2021-04-12 21:20:09 +02:00
// POST JSON. See docs/API.md
var payload = ( json . request == 'arrival' ) ?
{
'room' : qp . room ,
'name' : name ,
'arrival' : iso ,
2021-05-27 16:26:25 +02:00
'agreetoguidelines' : agreed ,
2021-10-14 13:45:00 +02:00
'tested' : tested // = 3G
2021-04-12 21:20:09 +02:00
} :
{
'name' : name ,
'departure' : iso ,
'cleanedworkspace' : agreed
}
return post ( "/" + json . request , payload )
}
function localISOTimeMinutes ( date ) {
var tzoffset = date . getTimezoneOffset ( ) * 60000 ; //offset in milliseconds
var localISOTime = ( new Date ( date - tzoffset ) ) . toISOString ( ) . slice ( 0 , - 1 ) ;
return localISOTime . split ( ':' ) . slice ( 0 , 2 ) . join ( ':' )
}
function handleRequest ( res ) {
var reqt = {
'arrival' : 'You probably forgot to sign in when you arrived. Please enter your arrival time now:' ,
'departure' : 'You probably forgot to sign out when you left. Please enter your departure time now:'
}
mform . innerHTML = "<h2>Processing Request...</h2>"
res . json ( ) . then ( function ( json ) {
var aInfo = ''
var minD = ''
var doubleT = ''
2021-05-27 16:26:25 +02:00
var testCheck = ''
2021-04-12 21:20:09 +02:00
if ( json . request == 'departure' ) {
var d = new Date ( json . arrival . time )
var dInfo = d . toString ( 'en-GB' ) . split ( ' ' ) . slice ( 0 , 5 ) . join ( ' ' )
aInfo = ` Your last arrival was on <b> ${ dInfo } </b> in room <b> ${ json . arrival . room } </b>. `
minD = ` min=" ${ localISOTimeMinutes ( d ) } " `
if ( new Date ( ) - d < 3 * 60 * 1000 ) {
doubleT = '<b style="color:red">Your last sign in was less than 3 minutes ago. You might be accidentally trying to sign in twice. If you don\'t intend to log 2 arrivals within the last 3 minutes, please abort below.</b>'
}
2021-05-27 16:26:25 +02:00
} else if ( json . request == 'arrival' ) {
testCheck = testCheckBox . replace ( 'have' , 'had then' ) ;
2021-04-12 21:20:09 +02:00
}
var now = localISOTimeMinutes ( new Date ( ) )
document . body . innerHTML +=
` <div class="request">
< h1 > $ { json . request } missing ! < / h 1 >
< form id = "reqform" >
< label >
$ { reqt [ json . request ] }
2021-06-08 00:49:42 +02:00
< input type = "datetime-local" $ { minD } max = "${now}" required >
2021-04-12 21:20:09 +02:00
$ { aInfo }
< / l a b e l >
$ { doubleT }
2021-05-27 16:26:25 +02:00
$ { testCheck }
2021-04-12 21:20:09 +02:00
< input type = "submit" >
< input type = "button" value = "Abort" onclick = "document.body.innerHTML='<h1>Aborted</h1><form>Nothing was logged.<br>You can close this tab/window now.</form>'" >
< / f o r m >
< / d i v > `
2021-05-27 16:26:25 +02:00
var rform = document . getElementById ( 'reqform' )
2021-04-12 21:20:09 +02:00
rform . onsubmit = async function ( e ) {
await handleRequestSubmit ( e , json )
document . querySelector ( '.request' ) . remove ( )
setTimeout ( sendMainData , 200 )
}
} )
}
2021-06-08 00:49:42 +02:00
if ( qp . edittime && qp . edittime == 1 ) {
var now = localISOTimeMinutes ( new Date ( ) )
document . getElementById ( 'datetime' ) . value = now ;
document . getElementById ( 'datetime' ) . max = now ;
}
/* Push Notifications */
function sendNotification ( ) {
navigator . serviceWorker . ready . then ( function ( serviceWorker ) {
serviceWorker . showNotification ( "Forgot to sign out?" , {
body : "You didn't sign out of ftracker yet" ,
icon : "/favicon.ico" ,
actions : [ {
action : "depart" ,
title : "Sign Out"
} ]
} )
} )
}
function initPush ( name ) {
// Check availability
var supported = "serviceWorker" in navigator && "PushManager" in window
if ( ! supported ) {
console . warn ( "Push Notifications not supported!" )
return
}
2021-06-11 01:57:32 +02:00
fetch ( '/pushinfo' ) . then ( function ( res ) {
if ( res . ok )
res . json ( ) . then ( function ( push ) {
if ( push . enabled )
registerPush ( name , push . publickey ) ;
} ) ;
} ) ;
}
function registerPush ( name , pushServerPublicKey ) {
2021-06-08 00:49:42 +02:00
// Register service worker
navigator . serviceWorker . register ( "/sw.js" ) . then ( function ( swRegistration ) {
console . log ( "ServiceWorker registered:" , swRegistration )
} )
// Request permission
// TODO: Only do this AFTER the first? SUCCESSFUL sign-in
Notification . requestPermission ( function ( result ) {
return ( result === 'granted' )
} ) . then ( function ( consent ) {
console . log ( 'Notifications' , consent ? 'enabled' : 'denied' ) ;
} )
// Check if already initialized
if ( localStorage . getItem ( 'pushsub' ) )
return
// Register push service
navigator . serviceWorker . ready . then ( function ( serviceWorker ) {
serviceWorker . pushManager . subscribe ( {
userVisibleOnly : true ,
applicationServerKey : pushServerPublicKey
} ) . then ( function ( subscription ) {
console . log ( "User is subscribed:" , subscription ) ;
fetch ( '/pushsubscribe' , {
method : "POST" ,
headers : { "Content-Type" : "application/json" } ,
body : JSON . stringify ( {
name : name ,
sub : subscription
} )
2021-06-11 01:04:44 +02:00
} ) . then ( function ( res ) {
if ( res . ok )
localStorage . setItem ( 'pushsub' , subscription ) ;
2021-06-08 00:49:42 +02:00
} ) ;
} ) ;
} ) ;
}