1
1
/* -------- AFFICHAGE DU CODE DE PROMOTION À L'INSCRIPTION -------- */
2
2
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
3
- initializeCouponTimer ( ) ;
4
- } ) ;
5
-
6
- function initializeCouponTimer ( ) {
7
- let memberData = localStorage . getItem ( '_ms-mem' ) ;
8
- if ( memberData ) {
9
- let memberObj = JSON . parse ( memberData ) ;
10
- if ( memberObj && memberObj . metaData && memberObj . metaData . coupon_name ) {
11
- startCouponTimer ( memberObj . metaData . coupon_name ) ;
12
- }
13
- }
14
-
3
+ updatePromoCode ( ) ;
15
4
let checkInterval = setInterval ( function ( ) {
16
- if ( localStorage . getItem ( 'timer_finished' ) === 'true' ) {
5
+ let memberDataExists = localStorage . getItem ( '_ms-mem' ) !== null ;
6
+ let timerFinished = localStorage . getItem ( 'timer_finished' ) === 'true' ;
7
+
8
+ if ( timerFinished || ! memberDataExists ) {
17
9
clearInterval ( checkInterval ) ;
18
10
} else {
19
11
updatePromoCode ( ) ;
20
12
}
21
- } , 1000 ) ; // Vérifie les données du coupon toutes les secondes
22
- }
13
+ } , 60000 ) ; // Vérifie les données du coupon toutes les minutes
14
+ } ) ;
23
15
24
16
function startCouponTimer ( couponName ) {
25
- let startTime = localStorage . getItem ( 'coupon_start_time' ) ;
17
+ let startTime = parseInt ( localStorage . getItem ( 'coupon_start_time' ) , 10 ) ;
18
+ let currentTime = new Date ( ) . getTime ( ) ;
19
+ let timerFinished = localStorage . getItem ( 'timer_finished' ) ;
20
+
26
21
if ( ! startTime ) {
27
- startTime = new Date ( ) . getTime ( ) . toString ( ) ;
22
+ startTime = currentTime ;
28
23
localStorage . setItem ( 'coupon_start_time' , startTime ) ;
24
+ } else if ( timerFinished ) {
25
+ return ;
26
+ }
27
+
28
+ let elapsedTime = currentTime - startTime ;
29
+ let remainingTime = 24 * 60 * 60 * 1000 - elapsedTime ;
30
+
31
+ if ( remainingTime > 0 && couponName ) {
32
+ document . querySelectorAll ( '[data-co-offer="promo-code-wrapper"]' ) . forEach ( function ( promoPopinElement ) {
33
+ promoPopinElement . style . display = 'block' ;
34
+ } ) ;
29
35
}
30
- startTime = parseInt ( startTime , 10 ) ; // Make sure we work with the number
31
36
32
37
let timerInterval = setInterval ( function ( ) {
33
38
updateTimer ( couponName , startTime , timerInterval ) ;
@@ -36,12 +41,17 @@ function startCouponTimer(couponName) {
36
41
}
37
42
38
43
function updateTimer ( couponName , startTime , timerInterval ) {
39
- let currentTime = new Date ( ) . getTime ( ) ;
40
- let elapsedTime = currentTime - startTime ;
44
+ let elapsedTime = new Date ( ) . getTime ( ) - startTime ;
41
45
let remainingTime = 24 * 60 * 60 * 1000 - elapsedTime ;
42
46
43
47
if ( remainingTime > 0 && couponName ) {
44
- displayPromotionDetails ( remainingTime ) ;
48
+ let hours = Math . floor ( ( remainingTime % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ) ;
49
+ let minutes = Math . floor ( ( remainingTime % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ) ;
50
+ let seconds = Math . floor ( ( remainingTime % ( 1000 * 60 ) ) / 1000 ) ;
51
+
52
+ document . querySelectorAll ( '[data-co-offer="promo-timer"]' ) . forEach ( function ( timerDisplay ) {
53
+ timerDisplay . textContent = `${ hours } h ${ minutes } m ${ seconds } s` ;
54
+ } ) ;
45
55
} else {
46
56
clearInterval ( timerInterval ) ;
47
57
localStorage . setItem ( 'timer_finished' , 'true' ) ;
@@ -51,21 +61,9 @@ function updateTimer(couponName, startTime, timerInterval) {
51
61
}
52
62
}
53
63
54
- function displayPromotionDetails ( remainingTime ) {
55
- let hours = Math . floor ( ( remainingTime % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ) ;
56
- let minutes = Math . floor ( ( remainingTime % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ) ;
57
- let seconds = Math . floor ( ( remainingTime % ( 1000 * 60 ) ) / 1000 ) ;
58
-
59
- document . querySelectorAll ( '[data-co-offer="promo-timer"]' ) . forEach ( function ( timerDisplay ) {
60
- timerDisplay . textContent = `${ hours } h ${ minutes } m ${ seconds } s` ;
61
- } ) ;
62
- document . querySelectorAll ( '[data-co-offer="promo-code-wrapper"]' ) . forEach ( function ( promoPopinElement ) {
63
- promoPopinElement . style . display = 'block' ;
64
- } ) ;
65
- }
66
-
67
64
function updatePromoCode ( ) {
68
65
let memberData = localStorage . getItem ( '_ms-mem' ) ;
66
+
69
67
if ( memberData ) {
70
68
let memberObj = JSON . parse ( memberData ) ;
71
69
if ( memberObj && memberObj . metaData && memberObj . metaData . coupon_name ) {
@@ -79,9 +77,14 @@ function updateCouponDisplay(couponName) {
79
77
promoElements . forEach ( function ( element ) {
80
78
element . textContent = couponName ;
81
79
} ) ;
80
+
81
+ if ( ! localStorage . getItem ( 'timer_finished' ) ) {
82
+ startCouponTimer ( couponName ) ;
83
+ }
82
84
}
83
85
84
86
87
+
85
88
/*--- DÉBUT : DÉVEROUILLAGE FORMATION EN FONCTION DU PLAN DANS LA NAVIGATION ----*/
86
89
async function updateTabLinksAndHideElementsForSpecificPlans ( ) {
87
90
const response = await window . $memberstackDom . getCurrentMember ( ) ;
0 commit comments