2022-12-19 22:26:51 +00:00
const base _url = "https://oss110metz.rezo-rm.fr/"
2022-12-21 21:58:48 +00:00
var page = [ '#main-page' , '#admin-page' , '#login-page' , '#demande-page' ] ;
2022-12-21 21:57:49 +00:00
window . onpopstate = history . onpushstate = function ( e ) {
2022-12-21 22:22:56 +00:00
if ( e . state ) {
show _page ( e . state , true ) ;
} else {
history . back ( ) ;
}
2022-12-21 21:57:49 +00:00
} ;
history . pushState ( "#main-page" , "" , "" )
2023-01-09 10:16:03 +00:00
var save _udpate = null ;
2022-12-21 22:25:34 +00:00
function show _page ( id , historyPush ) {
2022-12-21 22:43:56 +00:00
//si le z-index de la nouvelle page est inférieur on fait un transi 'forward' sinon 'backward'
2022-12-21 21:58:48 +00:00
for ( i in page ) {
2022-12-21 22:43:56 +00:00
$ ( page [ i ] ) . hide ( ) . removeClass ( "away" ) ;
2022-12-21 21:58:48 +00:00
}
2023-01-08 21:18:37 +00:00
2022-12-21 22:43:56 +00:00
$hs = $ ( history . state ) . show ( ) ;
if ( ! historyPush )
$hs . addClass ( 'away' ) ;
$id = $ ( id ) . show ( ) ;
2023-01-08 21:18:37 +00:00
2023-01-09 10:16:03 +00:00
/ * i f ( i d = = " # d e m a n d e - p a g e " ) {
save _udpate = setInterval ( ( ) => {
let save _scroll = $ ( window ) . scrollTop ( ) ;
updateDemandes ( ) ;
$ ( window ) . scrollTop ( save _scroll ) ;
} , 5000 ) ;
} else if ( ! save _udpate ) {
clearInterval ( save _udpate ) ;
} * /
2022-12-21 22:43:56 +00:00
if ( ! historyPush ) {
2022-12-21 22:22:56 +00:00
history . pushState ( id , "" , "" )
}
2022-12-19 22:26:51 +00:00
}
2023-01-07 16:46:33 +00:00
//show_page("#demande-page");
2022-12-21 21:20:48 +00:00
$ ( "#login-button" ) . click ( function ( e ) {
var data = JSON . stringify ( {
2023-01-08 23:10:18 +00:00
user : $ ( "#login" ) . val ( ) . trim ( ) ,
password : $ ( "#password" ) . val ( ) . trim ( )
2022-12-21 21:20:48 +00:00
} ) ;
$ ( "#password" ) . val ( '' ) ;
$ . ajax ( {
type : "POST" ,
url : base _url + "login" ,
data : data ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
localStorage . setItem ( "token" , data . token ) ;
2023-01-03 19:13:45 +00:00
localStorage . setItem ( "user" , data . user . uid ) ;
localStorage . setItem ( "isAdmin" , data . user . isAdmin ) ;
2023-01-03 19:15:40 +00:00
if ( data . user . isAdmin ) {
show _page ( '#admin-page' ) ;
2023-01-03 19:20:27 +00:00
get _admin ( "prank" ) ;
2023-01-03 19:15:40 +00:00
} else {
show _page ( '#demande-page' ) ;
2023-01-08 22:08:41 +00:00
updateDemandes ( ) ;
2023-01-03 19:15:40 +00:00
}
2022-12-21 21:20:48 +00:00
} else {
2023-01-07 16:46:33 +00:00
$ ( '#error-message' ) . empty ( ) ;
t = new TypeIt ( '#error-message' , {
speed : 110 ,
lifeLike : true
} )
. type ( data . why )
. go ( ) ;
2022-12-21 21:20:48 +00:00
}
2023-01-07 16:46:33 +00:00
} ,
error : function ( e , status , i ) {
$ ( '#error-message' ) . empty ( ) ;
t = new TypeIt ( '#error-message' , {
speed : 110 ,
lifeLike : true
} )
. type ( status )
. go ( ) ;
2022-12-21 21:20:48 +00:00
}
} ) ;
} ) ;
2023-01-08 21:18:37 +00:00
$ ( "#register-button" ) . click ( function ( e ) {
var data = JSON . stringify ( {
2023-01-08 23:10:18 +00:00
user : $ ( "#register-user" ) . val ( ) . trim ( ) ,
password : $ ( "#register-password" ) . val ( ) . trim ( )
2023-01-08 21:18:37 +00:00
} ) ;
2023-01-08 17:05:48 +00:00
if ( $ ( "#register-password-confirm" ) . val ( ) != $ ( "#register-password" ) . val ( ) ) {
t = new TypeIt ( '#error-message-register' , {
2023-01-08 18:14:17 +00:00
speed : 100 ,
2023-01-08 17:05:48 +00:00
lifeLike : true
} )
. type ( "Oups, les 2 mots de passe ne correspondent pas !" )
. go ( ) ;
return ;
}
2023-01-08 21:18:37 +00:00
$ ( "#register-password" ) . val ( '' ) ;
2023-01-08 17:05:48 +00:00
$ ( "#register-password-confirm" ) . val ( "" ) ;
2023-01-08 21:18:37 +00:00
$ . ajax ( {
type : "POST" ,
url : base _url + "register" ,
data : data ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
localStorage . setItem ( "token" , data . token ) ;
localStorage . setItem ( "user" , data . user . uid ) ;
localStorage . setItem ( "isAdmin" , data . user . isAdmin ) ;
if ( data . user . isAdmin ) {
show _page ( '#admin-page' ) ;
get _admin ( "prank" ) ;
} else {
show _page ( '#demande-page' ) ;
2023-01-08 22:08:41 +00:00
updateDemandes ( ) ;
2023-01-08 21:18:37 +00:00
}
} else {
$ ( '#error-message-register' ) . empty ( ) ;
t = new TypeIt ( '#error-message-register' , {
speed : 110 ,
lifeLike : true
} )
. type ( data . why )
. go ( ) ;
}
} ,
error : function ( e , status , i ) {
$ ( '#error-message-register' ) . empty ( ) ;
t = new TypeIt ( '#error-message-register' , {
speed : 110 ,
lifeLike : true
} )
. type ( status )
. go ( ) ;
}
} ) ;
} ) ;
2022-12-21 21:20:48 +00:00
$ ( "#prank-button" ) . click ( function ( ) {
2022-12-21 22:42:53 +00:00
if ( localStorage . getItem ( 'token' ) ) {
2022-12-21 21:20:48 +00:00
show _page ( '#demande-page' ) ;
2023-01-08 22:08:41 +00:00
updateDemandes ( ) ;
2022-12-21 21:20:48 +00:00
}
else {
show _page ( '#login-page' ) ;
}
} ) ;
2022-12-21 22:42:53 +00:00
$ ( "#logo" ) . click ( function ( ) {
if ( localStorage . getItem ( 'token' ) ) {
if ( localStorage . getItem ( 'isAdmin' ) ) {
show _page ( '#admin-page' ) ;
2023-01-01 20:40:58 +00:00
// Load prank by default
2023-01-02 22:33:43 +00:00
get _admin ( "prank" ) ;
2022-12-21 22:42:53 +00:00
}
else {
show _page ( '#demande-page' ) ;
2023-01-08 22:08:41 +00:00
updateDemandes ( ) ;
2022-12-21 22:42:53 +00:00
}
}
else {
show _page ( '#login-page' ) ;
}
} ) ;
2023-01-02 22:45:00 +00:00
$ ( "#admin-prank" ) . click ( ( ) => {
get _admin ( "prank" ) ;
} )
$ ( "#admin-treasure" ) . click ( ( ) => {
get _admin ( "treasure" ) ;
} )
$ ( "#admin-activity" ) . click ( ( ) => {
get _admin ( "activity" ) ;
} )
2023-01-01 20:40:58 +00:00
function get _admin ( type ) {
2023-01-03 19:48:52 +00:00
$ ( "#admin-content" ) . empty ( ) ;
2023-01-01 20:40:58 +00:00
$ . ajax ( {
type : "POST" ,
2023-01-03 19:20:27 +00:00
url : base _url + "get" ,
2023-01-01 20:40:58 +00:00
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : type ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
switch ( type ) {
2023-01-02 22:35:30 +00:00
case "prank" :
2023-01-01 20:40:58 +00:00
let doneCnt = 0 ;
let prankData = [ ] ;
for ( prankUid in data . prankData ) {
data . prankData [ prankUid ] . prankUid = prankUid ;
prankData . push ( data . prankData [ prankUid ] ) ;
}
2023-01-03 22:11:42 +00:00
prankData . sort ( ( a , b ) => new Date ( a . date ) - new Date ( b . date ) ) ;
2023-01-01 20:40:58 +00:00
for ( entry in prankData ) {
switch ( prankData [ entry ] . type ) {
2023-01-02 22:35:30 +00:00
case "crêpe" :
2023-01-03 22:32:56 +00:00
if ( prankData [ entry ] . state == "Pending"
|| prankData [ entry ] . state == "Accepted" ) {
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . append ( `
2023-01-01 20:40:58 +00:00
< span id = "${prankData[entry].prankUid}" class = "prank" >
< div class = "prank-title" > Demande de crêpe par $ { prankData [ entry ] . creator } < / d i v >
< div class = "prank-amount" > Quantité : $ { prankData [ entry ] . amount } < / d i v >
< div class = "prank-supplement" > Garniture : $ { prankData [ entry ] . supplement } < / d i v >
< div class = "prank-where" > Livraison : $ { prankData [ entry ] . where } < / d i v >
< div class = "prank-note" > Livraison : $ { prankData [ entry ] . note } < / d i v >
< span class = "prank-footer" >
< div class = "prank-uid" > UID : $ { prankData [ entry ] . prankUid } < / d i v >
2023-01-03 22:38:44 +00:00
< div class = "prank-date" > DATE : $ { prankData [ entry ] . date } < / d i v >
2023-01-01 20:40:58 +00:00
< / s p a n >
< span class = "prank-btn" >
2023-01-03 22:32:56 +00:00
< div class = "prank-btn-accept" style = "display: ${(prankData[entry].state == " Accepted " && prankData[entry].manageBy == localStorage.getItem(" user ")) ? " none " : " flex "};" > $ { ( prankData [ entry ] . state == "Pending" ) ? "Accepter" : "Récupérer (accepté par: " + prankData [ entry ] . manageBy + ")" } < / d i v >
< div class = "prank-btn-done" style = "display: ${(prankData[entry].state == " Accepted " && prankData[entry].manageBy == localStorage.getItem(" user ")) ? " flex " : " none "};" > Done < / d i v >
2023-01-03 22:40:55 +00:00
< div class = "prank-btn-refused" > Refuser < / d i v >
2023-01-01 20:40:58 +00:00
< / s p a n >
< / s p a n >
` )
2023-01-08 23:15:57 +00:00
} else if ( prankData [ entry ] . state == "Done" ) {
2023-01-01 20:40:58 +00:00
doneCnt ++ ;
}
break ;
2023-01-02 22:35:30 +00:00
case "kidnap" :
2023-01-02 20:38:58 +00:00
// Not Handle
2023-01-01 20:40:58 +00:00
break ;
}
}
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . append ( `
2023-01-02 20:38:58 +00:00
< span class = "prank" >
2023-01-08 17:05:48 +00:00
< div id = prankDone > Nombre totale de missions achevées : $ { doneCnt } < / d i v >
2023-01-02 20:38:58 +00:00
< / s p a n >
` );
2023-01-01 20:40:58 +00:00
break ;
2023-01-02 22:35:30 +00:00
case "treasure" :
2023-01-02 22:33:43 +00:00
let treasureData = [ ] ;
for ( treasureUid in data . treasureData ) {
data . treasureData [ treasureUid ] . treasureUid = treasureUid ;
treasureData . push ( data . treasureData [ treasureUid ] ) ;
}
2023-01-03 22:11:42 +00:00
treasureData . sort ( ( a , b ) => new Date ( a . date ) - new Date ( b . date ) ) ;
2023-01-02 22:33:43 +00:00
for ( entry in treasureData ) {
2023-01-03 22:32:56 +00:00
if ( treasureData [ entry ] . state == "Pending" ) {
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . append ( `
2023-01-02 22:33:43 +00:00
< span id = "${treasureData[entry].treasureUid}" class = "treasure" >
< div class = "treasure-title" > Demande de vérification par $ { treasureData [ entry ] . creator } < / d i v >
< div class = "treasure-activity" > Activité : $ { treasureData [ entry ] . activity . title } < / d i v >
< div class = "treasure-desc" > Description : $ { treasureData [ entry ] . desc } < / d i v >
2023-01-03 23:25:40 +00:00
< div class = "treasure-image" > Image : < a href = "${base_url}images/${treasureData[entry].image}.html" target = "_blank" > Lien < / a > < / d i v >
2023-01-02 22:33:43 +00:00
< span class = "treasure-footer" >
< div class = "treasure-uid" > UID : $ { treasureData [ entry ] . treasureUid } < / d i v >
2023-01-03 23:36:48 +00:00
< div class = "treasure-date" > DATE : $ { treasureData [ entry ] . date } < / d i v >
2023-01-02 22:33:43 +00:00
< / s p a n >
< span class = "treasure-btn" >
< div class = "treasure-btn-accept" > Accepter < / d i v >
< div class = "treasure-btn-refused" > Refuser < / d i v >
< / s p a n >
< / s p a n >
` )
}
}
2023-01-01 20:40:58 +00:00
break ;
2023-01-02 22:35:30 +00:00
case "activity" :
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . append ( `
< span id = "addActivity" class = "activity" >
< label for = "activityType" > Choisir une option : < / l a b e l >
< select name = "type" id = "activityType" >
< option value = "event" > Event < / o p t i o n >
< option value = "treasure" > Trésor < / o p t i o n >
< / s e l e c t >
2023-01-03 21:07:05 +00:00
< div class = "activity-title" > Titre : < textarea class = "admin-textarea" > < / t e x t a r e a > < / d i v >
< div class = "activity-desc" > Description : < textarea class = "admin-textarea" > < / t e x t a r e a > < / d i v >
< div class = "activity-where" > Lieu : < textarea class = "admin-textarea" > < / t e x t a r e a > < / d i v >
< div class = "activity-start" > Début : < input type = "datetime-local" class = "admin-input-date" > < / i n p u t > < / d i v >
< div id = "activity-end-add" class = "activity-end" > Fin : < input type = "datetime-local" class = "admin-input-date" > < / i n p u t > < / d i v >
2023-01-03 19:35:48 +00:00
< span class = "activity-btn" >
< div id = "activity-btn-add" > Ajouter une activité < / d i v >
< / s p a n >
2023-01-02 22:33:43 +00:00
< / s p a n >
` )
let activityData = [ ] ;
for ( activityUid in data . activityData ) {
data . activityData [ activityUid ] . activityUid = activityUid ;
activityData . push ( data . activityData [ activityUid ] ) ;
}
2023-01-03 22:11:42 +00:00
activityData . sort ( ( a , b ) => new Date ( a . start ) - new Date ( b . start ) ) ;
2023-01-02 22:33:43 +00:00
for ( entry in activityData ) {
2023-01-09 09:38:50 +00:00
if ( activityData [ entry ] . type == "event" ) {
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . append ( `
2023-01-02 22:33:43 +00:00
< span id = "${activityData[entry].activityUid}" class = "activity" >
2023-01-03 21:07:05 +00:00
< div class = "activity-title" > Event : < textarea class = "admin-textarea" > $ { activityData [ entry ] . title } < / t e x t a r e a > < / d i v >
< div class = "activity-desc" > Description : < textarea class = "admin-textarea" > $ { activityData [ entry ] . desc } < / t e x t a r e a > < / d i v >
< div class = "activity-where" > Lieu : < textarea class = "admin-textarea" > $ { activityData [ entry ] . where } < / t e x t a r e a > < / d i v >
< div class = "activity-start" > Début : < input type = "datetime-local" class = "admin-input-date" value = "${toIsoString(new Date(activityData[entry].start))}" > < / i n p u t > < / d i v >
< div class = "activity-end" > Fin : < input type = "datetime-local" class = "admin-input-date" value = "${toIsoString(new Date(activityData[entry].end))}" > < / i n p u t > < / d i v >
2023-01-02 22:33:43 +00:00
< span class = "activity-footer" >
< div class = "activity-uid" > UID : $ { activityData [ entry ] . activityUid } < / d i v >
< / s p a n >
< span class = "activity-btn" >
< div class = "activity-btn-update" > Update < / d i v >
< div class = "activity-btn-delete" > Supprimer < / d i v >
< / s p a n >
< / s p a n >
` )
2023-01-09 09:38:50 +00:00
} else if ( activityData [ entry ] . type == "treasure" ) {
2023-01-03 19:35:48 +00:00
$ ( "#admin-content" ) . prepend ( `
2023-01-02 22:33:43 +00:00
< span id = "${activityData[entry].activityUid}" class = "activity" >
2023-01-03 21:07:05 +00:00
< div class = "activity-title" > Trésor : < textarea class = "admin-textarea" > $ { activityData [ entry ] . title } < / t e x t a r e a > < / d i v >
< div class = "activity-desc" > Description : < textarea class = "admin-textarea" > $ { activityData [ entry ] . desc } < / t e x t a r e a > < / d i v >
< div class = "activity-where" > Lieu : < textarea class = "admin-textarea" > $ { activityData [ entry ] . where } < / t e x t a r e a > < / d i v >
< div class = "activity-start" > Début : < input type = "datetime-local" class = "admin-input-date" value = "${toIsoString(new Date(activityData[entry].start))}" > < / i n p u t > < / d i v >
2023-01-02 22:33:43 +00:00
< div class = "activity-state" > Status : $ { activityData [ entry ] . treasureState } < / d i v >
< span class = "activity-footer" >
< div class = "activity-uid" > UID : $ { activityData [ entry ] . activityUid } < / d i v >
< / s p a n >
< span class = "activity-btn" >
< div class = "activity-btn-update" > Update < / d i v >
< div class = "activity-btn-delete" > Supprimer < / d i v >
< / s p a n >
< / s p a n >
` )
}
}
2023-01-01 20:40:58 +00:00
break ;
}
2023-01-02 22:33:43 +00:00
refreshListener ( ) ;
2023-01-01 20:40:58 +00:00
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-01 20:40:58 +00:00
}
}
} ) ;
}
2023-01-02 22:33:43 +00:00
function refreshListener ( ) {
$ ( ".prank-btn-accept" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "acceptPrank" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
prankUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
2023-01-02 20:38:58 +00:00
2023-01-02 22:33:43 +00:00
success : function ( data ) {
if ( data . success ) {
let btnElems = $ ( "#" + uid ) . children ( ) [ 6 ] ;
2023-01-03 22:44:30 +00:00
btnElems . children [ 0 ] . style . display = "none" ;
2023-01-03 22:48:11 +00:00
btnElems . children [ 1 ] . style . display = "flex" ;
2023-01-02 22:33:43 +00:00
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
2023-01-02 20:38:58 +00:00
}
2023-01-02 22:33:43 +00:00
} ) ;
} )
2023-01-02 20:38:58 +00:00
2023-01-02 22:33:43 +00:00
$ ( ".prank-btn-refused" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "refusePrank" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
prankUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
2023-01-02 20:38:58 +00:00
2023-01-02 22:33:43 +00:00
success : function ( data ) {
if ( data . success ) {
$ ( "#" + uid ) . remove ( ) ;
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
2023-01-02 20:38:58 +00:00
}
2023-01-02 22:33:43 +00:00
} ) ;
} )
2023-01-02 20:38:58 +00:00
2023-01-02 22:33:43 +00:00
$ ( ".prank-btn-done" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "donePrank" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
prankUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
2023-01-02 20:38:58 +00:00
2023-01-02 22:33:43 +00:00
success : function ( data ) {
if ( data . success ) {
$ ( "#" + uid ) . remove ( ) ;
2023-01-03 19:48:52 +00:00
$ ( "#prankDone" ) [ 0 ] . innerHTML = "Nombre totale de mission achevées: " + ( parseInt ( $ ( "#prankDone" ) [ 0 ] . innerHTML . replace ( "Nombre totale de mission achevées: " , "" ) ) + 1 ) ;
2023-01-02 22:33:43 +00:00
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
}
} ) ;
} )
$ ( ".treasure-btn-accept" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "acceptTreasure" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
treasureUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
$ ( "#" + uid ) . remove ( ) ;
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
2023-01-02 20:38:58 +00:00
}
2023-01-02 22:33:43 +00:00
} ) ;
} )
$ ( ".treasure-btn-refused" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "refuseTreasure" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
treasureUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
$ ( "#" + uid ) . remove ( ) ;
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
}
} ) ;
} )
$ ( ".activity-btn-update" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id ;
2023-01-03 20:04:09 +00:00
let children = e . target . parentNode . parentNode . children ;
2023-01-02 22:33:43 +00:00
$ . ajax ( {
type : "POST" ,
2023-01-03 20:32:08 +00:00
url : base _url + "addActivity" ,
2023-01-02 22:33:43 +00:00
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
activityUid : uid ,
type : ( children [ 0 ] . innerHTML . substring ( 0 , 5 ) == "Event" ) ? "event" : "treasure" ,
2023-01-03 20:07:45 +00:00
title : children [ 0 ] . children [ 0 ] . value ,
desc : children [ 1 ] . children [ 0 ] . value ,
where : children [ 2 ] . children [ 0 ] . value ,
start : new Date ( children [ 3 ] . children [ 0 ] . value ) . toString ( ) ,
end : ( children [ 3 ] . children . length == 0 ) ? null : new Date ( children [ 4 ] . children [ 0 ] . value ) . toString ( ) ,
2023-01-02 22:33:43 +00:00
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
alert ( "Update réussie" )
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
}
} ) ;
} )
$ ( ".activity-btn-delete" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id
$ . ajax ( {
type : "POST" ,
url : base _url + "delActivity" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
activityUid : uid ,
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
$ ( "#" + uid ) . remove ( ) ;
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
}
} ) ;
} )
2023-01-03 19:58:46 +00:00
$ ( "#activityType" ) . on ( "change" , function ( ) {
2023-01-02 22:33:43 +00:00
if ( this . value == "treasure" ) {
2023-01-03 19:58:46 +00:00
$ ( "#activity-end-add" ) . css ( "display" , "none" ) ;
2023-01-02 22:33:43 +00:00
} else if ( this . value == "event" ) {
2023-01-03 19:58:46 +00:00
$ ( "#activity-end-add" ) . css ( "display" , "flex" ) ;
2023-01-02 20:38:58 +00:00
}
2023-01-02 22:33:43 +00:00
} )
2023-01-03 20:04:09 +00:00
$ ( "#activity-btn-add" ) . click ( ( e ) => {
let children = e . target . parentNode . parentNode . children ;
2023-01-02 22:33:43 +00:00
$ . ajax ( {
type : "POST" ,
2023-01-03 20:32:08 +00:00
url : base _url + "addActivity" ,
2023-01-02 22:33:43 +00:00
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : $ ( "#activityType" ) . val ( ) ,
2023-01-03 20:07:45 +00:00
title : children [ 2 ] . children [ 0 ] . value ,
desc : children [ 3 ] . children [ 0 ] . value ,
where : children [ 4 ] . children [ 0 ] . value ,
start : new Date ( children [ 5 ] . children [ 0 ] . value ) . toString ( ) ,
end : ( $ ( "#activityType" ) . val ( ) == "treasure" ) ? null : new Date ( children [ 6 ] . children [ 0 ] . value ) . toString ( ) ,
2023-01-02 22:33:43 +00:00
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
get _admin ( "activity" ) ;
} else {
2023-01-03 19:24:31 +00:00
if ( data . why == "Not authentificated" ) {
show _page ( '#login-page' ) ;
} else {
alert ( data . why ) ;
}
2023-01-02 22:33:43 +00:00
}
}
} ) ;
} )
2023-01-08 22:14:04 +00:00
$ ( ".prank-btn-delete" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id ;
$ . ajax ( {
type : 'POST' ,
url : base _url + 'delPrank' ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
prankUid : uid
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
updateDemandes ( ) ;
}
else {
alert ( data . why ) ;
}
}
} ) ;
} ) ;
2023-01-08 18:14:17 +00:00
$ ( ".treasure-btn-submit" ) . click ( ( e ) => {
let uid = e . target . parentNode . parentNode . id ;
2023-01-08 19:44:25 +00:00
$ ( '#treasure-popup-activity-uid' ) . val ( uid ) ;
$ ( '#treasure-popup-uid' ) . val ( $ ( '#' + uid + ' .treasure-uid' ) . text ( ) ) ;
2023-01-08 19:07:10 +00:00
$ ( '#treasure-popup-title' ) . text ( $ ( '#' + uid + ' .treasure-title' ) . text ( ) ) ;
$ ( '#treasure-popup-desc' ) . text ( $ ( '#' + uid + ' .treasure-user-desc' ) . text ( ) ) ;
2023-01-08 18:14:17 +00:00
$ ( '.overlay' ) . css ( { "opacity" : 1 , "visibility" : "visible" } ) ;
} ) ;
2023-01-02 22:33:43 +00:00
}
2023-01-02 20:38:58 +00:00
2023-01-08 18:14:17 +00:00
$ ( '.close' ) . click ( ( e ) => {
$ ( '.overlay' ) . css ( { "opacity" : 0 , "visibility" : "hidden" } ) ;
} ) ;
2023-01-08 19:07:10 +00:00
$ ( '#treasure-popup-button' ) . click ( ( e ) => {
var fr = new FileReader ( ) ;
2023-01-09 08:38:03 +00:00
let f = $ ( '#treasure-popup-image' ) . prop ( 'files' ) [ 0 ] ;
if ( f . size > 10 * * 7 ) {
alert ( 'Le fichier est beaucoup trop gros !' ) ;
return ;
}
2023-01-08 19:07:10 +00:00
2023-01-08 19:44:25 +00:00
var indata = {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
activityUid : $ ( '#treasure-popup-activity-uid' ) . val ( ) ,
desc : $ ( '#treasure-popup-desc' ) . text ( ) ,
}
var tuid = $ ( '#treasure-popup-uid' ) . val ( ) ;
if ( tuid != '' ) {
indata . treasureUid = tuid ;
2023-01-09 09:38:50 +00:00
}
2023-01-08 19:44:25 +00:00
2023-01-08 19:07:10 +00:00
fr . addEventListener ( 'load' , ( ) => {
2023-01-08 19:44:25 +00:00
indata . image = fr . result ;
2023-01-08 19:07:10 +00:00
$ . ajax ( {
type : "POST" ,
url : base _url + "sendTreasure" ,
2023-01-08 19:44:25 +00:00
data : JSON . stringify ( indata ) ,
2023-01-08 19:07:10 +00:00
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
2023-01-09 09:38:50 +00:00
2023-01-08 19:07:10 +00:00
success : function ( data ) {
if ( data . success ) {
updateDemandes ( ) ;
$ ( '.overlay' ) . css ( { "opacity" : 0 , "visibility" : "hidden" } ) ;
} else {
console . error ( data . why ) ;
}
}
} ) ;
} ) ;
2023-01-09 09:38:50 +00:00
2023-01-09 08:38:03 +00:00
fr . readAsDataURL ( f ) ;
2023-01-08 19:07:10 +00:00
} ) ;
2023-01-08 11:41:10 +00:00
$ ( '#demande-button' ) . click ( function ( ) {
2023-01-10 07:58:05 +00:00
amount = $ ( '#demande-amount' ) . val ( ) ;
if ( amount > 9 ) {
t = new TypeIt ( '#error-message-demande' , {
speed : 100 ,
lifeLike : true
} )
. type ( "Trop de crêpes, maximum c'est 9" )
. go ( ) ;
return ;
}
2023-01-08 11:41:10 +00:00
$ . ajax ( {
type : "POST" ,
url : base _url + "addPrank" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : "crêpe" ,
2023-01-10 07:58:05 +00:00
amount : amount ,
2023-01-08 11:41:10 +00:00
where : $ ( '#demande-where' ) . val ( ) ,
2023-01-08 15:34:56 +00:00
supplement : $ ( '#demande-supp' ) . val ( ) ,
2023-01-08 16:47:33 +00:00
note : $ ( '#demande-notes' ) . val ( ) ,
2023-01-08 11:41:10 +00:00
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
$ ( '#demande-form input' ) . val ( "" ) ;
2023-01-10 08:02:51 +00:00
alert ( "C'est envoyé, gros gourmand ^^" )
2023-01-08 15:34:56 +00:00
updateDemandes ( ) ;
2023-01-08 11:41:10 +00:00
} else {
2023-01-08 15:34:56 +00:00
$ ( '#error-message-demande' ) . empty ( ) ;
t = new TypeIt ( '#error-message-demande' , {
speed : 100 ,
lifeLike : true
} )
. type ( data . why )
. go ( ) ;
if ( data . why . startsWith ( 'Token expired' ) || data . why . startsWith ( 'Not authen' ) )
show _page ( "#login-page" ) ;
2023-01-08 11:41:10 +00:00
}
}
} ) ;
} ) ;
2022-12-21 21:58:48 +00:00
2023-01-08 15:34:56 +00:00
function updateDemandes ( ) {
2023-01-08 17:05:48 +00:00
if ( localStorage . getItem ( 'user' ) && localStorage . getItem ( 'token' ) ) {
$ . ajax ( {
type : "POST" ,
url : base _url + "get" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : "prank"
2023-01-08 15:34:56 +00:00
2023-01-08 17:05:48 +00:00
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
2023-01-08 15:34:56 +00:00
2023-01-08 17:05:48 +00:00
success : function ( data ) {
if ( data . success ) {
$ ( '#demande-list' ) . html ( '' ) ;
let pd = [ ] ;
for ( pd _uid in data . prankData ) {
if ( data . prankData [ pd _uid ] . creator == localStorage . getItem ( 'user' ) ) {
data . prankData [ pd _uid ] . prankUid = pd _uid ;
pd . push ( data . prankData [ pd _uid ] ) ;
}
2023-01-08 21:53:50 +00:00
}
2023-01-08 17:05:48 +00:00
pd . sort ( ( a , b ) => new Date ( b . date ) - new Date ( a . date ) ) ;
for ( const pd _uid in pd ) {
$ ( '#demande-list' ) . append ( `
< span id = "${pd[pd_uid].prankUid}" class = "prank" >
< h2 class = "prank-title" > Demande de crêpe < / h 2 >
< div class = "prank-amount" > Quantité : $ { pd [ pd _uid ] . amount } < / d i v >
< div class = "prank-supplement" > Garniture : $ { pd [ pd _uid ] . supplement } < / d i v >
< div class = "prank-where" > Livraison : $ { pd [ pd _uid ] . where } < / d i v >
< div class = "prank-note" > Notes : $ { pd [ pd _uid ] . note } < / d i v >
< div class = "prank-status" > Statue : $ { pd [ pd _uid ] . state } < / d i v >
< span class = "prank-footer" >
< div class = "prank-uid" > UID : $ { pd [ pd _uid ] . prankUid } < / d i v >
< div class = "prank-date" > DATE : $ { pd [ pd _uid ] . date } < / d i v >
< / s p a n >
$ { ( pd [ pd _uid ] . state != "Accepted" && pd [ pd _uid ] . state != "Refused" ) ?
` <span class="prank-btn">
< div class = "prank-btn-delete" > Annuler < / d i v >
< / s p a n > `
: ''
}
2023-01-08 22:18:31 +00:00
< / s p a n >
2023-01-08 17:05:48 +00:00
` )
}
refreshListener ( ) ;
2023-01-08 15:34:56 +00:00
}
2023-01-08 17:05:48 +00:00
else {
2023-01-08 19:07:10 +00:00
if ( data . why == "Not authentificated" || data . why == "Token expired" ) {
2023-01-08 17:05:48 +00:00
show _page ( '#login-page' ) ;
} else {
2023-01-08 19:07:10 +00:00
console . error ( data . why ) ;
2023-01-08 17:05:48 +00:00
}
2023-01-08 21:59:32 +00:00
}
2023-01-08 15:34:56 +00:00
}
2023-01-08 17:05:48 +00:00
} ) ;
2023-01-08 18:14:17 +00:00
$ . ajax ( {
type : "POST" ,
url : base _url + "get" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : "activity"
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
$ ( '#treasure-list' ) . html ( '' ) ;
let ad = data . activityData ;
for ( const ad _uid in ad ) {
if ( ad [ ad _uid ] . type == "treasure" ) {
$ ( '#treasure-list' ) . append ( `
< span id = "${ad_uid}" class = "treasure ${ad[ad_uid].treasureState == " Accepted " ? " treasure - accepted " : " "}" >
< h2 class = "treasure-title" > $ { ad [ ad _uid ] . title } < / h 2 >
2023-01-09 08:02:50 +00:00
$ { ad [ ad _uid ] . where ? ` <div class="treasure-reward">Récompense : ${ ad [ ad _uid ] . where } </div> ` : "" }
2023-01-08 19:58:23 +00:00
< div class = "treasure-desc" > Validité : $ { ad [ ad _uid ] . desc } < / d i v >
2023-01-08 18:14:17 +00:00
< div class = "treasure-status" > Statue : $ { ad [ ad _uid ] . treasureState } < / d i v >
$ { ( ad [ ad _uid ] . treasureState != "Accepted" && ad [ ad _uid ] . treasureState != "Refused" ) ?
` <span class="treasure-btn">
< div class = "treasure-btn-submit" > Soumettre sa résolution < / d i v >
< / s p a n > `
: ''
}
< / s p a n >
` )
}
}
refreshListener ( ) ;
}
else {
2023-01-08 19:07:10 +00:00
console . error ( data . why ) ;
2023-01-08 18:14:17 +00:00
}
}
} ) ;
$ . ajax ( {
type : "POST" ,
url : base _url + "get" ,
data : JSON . stringify ( {
uid : localStorage . getItem ( 'user' ) ,
token : localStorage . getItem ( 'token' ) ,
type : "treasure"
} ) ,
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : function ( data ) {
if ( data . success ) {
let td = data . treasureData ;
for ( const td _uid in td ) {
2023-01-09 08:49:19 +00:00
if ( td [ td _uid ] . treasureState != "Accepted" ) {
2023-01-08 19:44:25 +00:00
$elem = $ ( '#' + td [ td _uid ] . activityUid ) ;
2023-01-09 08:49:19 +00:00
if ( td [ td _uid ] . treasureState == "Refused" )
$elem . addClass ( 'treasure-refused' ) ;
else
$elem . addClass ( 'treasure-submitted' ) ;
2023-01-08 19:44:25 +00:00
$elem . find ( ".treasure-btn-submit" ) . text ( 'Editer la soumission' ) ;
2023-01-09 09:38:50 +00:00
2023-01-08 19:44:25 +00:00
$ ( ` <div class="treasure-image">Image: <a href=" ${ base _url } images/ ${ td [ td _uid ] . image } .html" target="_blank"> Lien</a></div> ` ) . insertAfter ( $elem . find ( '.treasure-desc' ) )
$ ( ` <div class="treasure-user-desc"> ${ td [ td _uid ] . desc } </div> ` ) . insertAfter ( $elem . find ( '.treasure-desc' ) )
$ ( ` <p>Votre soumission :</p> ` ) . insertAfter ( $elem . find ( '.treasure-desc' ) )
$ ( ` <span class="treasure-footer">
< div class = "treasure-uid" > $ { td _uid } < / d i v >
< / s p a n > ` ) . i n s e r t A f t e r ( $ e l e m . f i n d ( ' . t r e a s u r e - b t n ' ) ) ;
}
2023-01-08 18:14:17 +00:00
}
refreshListener ( ) ;
}
else {
2023-01-08 19:07:10 +00:00
console . error ( data . why ) ;
2023-01-08 18:14:17 +00:00
}
}
2023-01-09 09:38:50 +00:00
2023-01-08 18:14:17 +00:00
} ) ;
2023-01-08 17:05:48 +00:00
}
2023-01-08 15:34:56 +00:00
}
2023-01-08 11:41:10 +00:00
function updateNextActivity ( ) {
2023-01-07 16:46:33 +00:00
$ . ajax ( {
2023-01-08 11:41:10 +00:00
type : "POST" ,
url : base _url + "get" ,
data : JSON . stringify ( {
token : "" ,
uid : "" ,
type : "activity" ,
} ) ,
2023-01-07 16:46:33 +00:00
contentType : "application/json; charset=utf-8" ,
dataType : "json" ,
success : ( data ) => {
if ( data . success ) {
2023-01-08 11:41:10 +00:00
let curr _date = new Date ( ) ;
let ad = data . activityData ;
var candidate = [ ] ;
var next _acti = { title : "Pas encore d'activité" , desc : "Encore aucune activité n'a été renseigné, faite le vite !" } ;
for ( const uid in ad ) {
if ( ad [ uid ] . type == "event" && curr _date < new Date ( ad [ uid ] . start ) ) {
candidate . push ( ad [ uid ] ) ;
}
}
next _acti = candidate . reduce ( function ( prev , curr ) {
2023-01-08 19:58:23 +00:00
return new Date ( prev . start ) < new Date ( curr . start ) ? prev : curr ;
2023-01-08 11:41:10 +00:00
} ) ;
var opt = { speed : 100 } ;
2023-01-07 16:46:33 +00:00
new TypeIt ( "#activite-title" , opt )
2023-01-08 11:41:10 +00:00
. type ( next _acti . title )
2023-01-07 16:46:33 +00:00
. go ( ) ;
new TypeIt ( "#activite-desc" , opt )
2023-01-08 11:41:10 +00:00
. type ( next _acti . desc )
. go ( ) ;
new TypeIt ( "#activite-place" , opt )
. type ( "Lieu : " + next _acti . where )
2023-01-07 16:46:33 +00:00
. go ( ) ;
2023-01-08 11:41:10 +00:00
let diff = Math . ceil ( ( new Date ( next _acti . start ) - new Date ( ) ) / 1000 )
let days = Math . trunc ( diff / ( 3600 * 24 ) )
let h = Math . trunc ( diff / 3600 ) - days * 24
let m = Math . trunc ( diff / 60 ) - ( h + days * 24 ) * 60
let s = diff - h * 3600 - m * 60 - days * 24 * 3600
2023-01-08 18:14:17 +00:00
$ ( '#timer' ) . text ( ( days == 0 ? "" : days + " jours " ) + ( new Date ( 'December 17, 1995 ' + [ h , m , s ] . join ( ':' ) ) ) . toLocaleTimeString ( 'default' , {
2023-01-07 16:46:33 +00:00
hour : '2-digit' ,
minute : '2-digit' ,
second : '2-digit'
} ) ) ;
2023-01-08 11:41:10 +00:00
}
2023-01-07 16:46:33 +00:00
}
} )
2023-01-08 11:41:10 +00:00
}
$ ( window ) . on ( "load" , function ( ) {
/* Register the Service Worker */
if ( 'serviceWorker' in navigator ) {
navigator . serviceWorker . register ( './js/sw.js' ) ;
}
for ( i in page ) {
$ ( page [ i ] ) . css ( 'z-index' , page . length - i ) ;
}
updateNextActivity ( ) ;
2023-01-07 16:46:33 +00:00
2022-12-15 13:30:34 +00:00
/* load timer */
window . setInterval ( function ( ) {
2023-01-08 11:41:10 +00:00
var b = $ ( "#timer" ) . text ( ) . split ( ' jours ' ) ;
2023-01-08 18:14:17 +00:00
if ( b . length == 2 ) {
var time = b [ 1 ] ;
var days = b [ 0 ] ;
} else {
var time = b [ 0 ] ;
var days = 0 ;
}
2022-12-15 13:30:34 +00:00
var t = time . split ( ":" ) . map ( t => parseInt ( t ) ) ;
2023-01-08 21:18:37 +00:00
2022-12-15 13:30:34 +00:00
t [ 2 ] -= 1 ;
if ( t [ 2 ] < 0 ) {
t [ 2 ] = 59 ;
t [ 1 ] -= 1 ;
}
if ( t [ 1 ] < 0 ) {
t [ 1 ] = 59 ;
t [ 0 ] -= 1 ;
}
if ( t [ 0 ] < 0 ) {
2023-01-08 11:41:10 +00:00
t [ 0 ] = 23 ;
t [ 1 ] = 59 ;
t [ 2 ] = 59 ;
days -= 1 ;
}
if ( days < 0 ) {
2022-12-15 13:30:34 +00:00
t [ 0 ] = 0 ;
t [ 1 ] = 0 ;
t [ 2 ] = 0 ;
2023-01-08 11:41:10 +00:00
days = 0 ;
updateNextActivity ( ) ;
2022-12-15 13:30:34 +00:00
}
2023-01-08 17:05:48 +00:00
$ ( '#timer' ) . text ( ( days == 0 ? "" : days + " jours " ) + ( new Date ( 'December 17, 1995 ' + t . join ( ':' ) ) ) . toLocaleTimeString ( 'default' , {
2022-12-15 16:14:41 +00:00
hour : '2-digit' ,
minute : '2-digit' ,
second : '2-digit'
} ) ) ;
2023-01-07 16:46:33 +00:00
2022-12-15 13:30:34 +00:00
} , 1000 ) ;
2022-12-19 22:26:51 +00:00
} ) ;
2023-01-02 22:33:43 +00:00
function toIsoString ( date ) {
var tzo = - date . getTimezoneOffset ( ) ,
dif = tzo >= 0 ? '+' : '-' ,
pad = function ( num ) {
return ( num < 10 ? '0' : '' ) + num ;
} ;
return date . getFullYear ( ) +
'-' + pad ( date . getMonth ( ) + 1 ) +
'-' + pad ( date . getDate ( ) ) +
'T' + pad ( date . getHours ( ) ) +
':' + pad ( date . getMinutes ( ) ) +
':' + pad ( date . getSeconds ( ) ) ;
}