11const LOCALE = 'fr-FR'
22
33window . addEventListener ( 'onWidgetLoad' , async function ( obj ) {
4- const status = await SE_API . getOverlayStatus ( ) ;
5- const projectId = { { projectNumericalId} } ;
4+ const status = await SE_API . getOverlayStatus ( )
5+ const projectId = obj . fieldData . projectNumericalId
66
77 if ( ! projectId ) {
8- console . error ( `[ulule/stats] Expected a valid project ID, got ${ projectId } instead` ) ;
8+ console . error ( `[ulule/stats] Expected a valid project ID, got ${ projectId } instead` )
99 if ( status . isEditorMode ) {
10- $ ( '.amount' ) . html ( `<div>Please input a valid project ID</div>` ) ;
10+ $ ( '.amount' ) . html ( `<div>Please input a valid project ID</div>` )
1111 }
1212 return
13- } ;
14- console . log ( '[ulule/stats] Widget loaded for project' , projectId ) ;
13+ }
14+ console . log ( '[ulule/stats] Widget loaded for project' , projectId )
1515
1616 // DEMO
1717 /*let fakeAmount = 2467;
@@ -28,67 +28,79 @@ window.addEventListener('onWidgetLoad', async function (obj) {
2828 }, 5000);*/
2929 // END DEMO
3030
31- let amount ;
32- const url = `https://data.ulule.com/projects/{{projectNumericalId}} /stats.json?cachebuster=${ Date . now ( ) } ` ;
33- const refreshInterval = { { refreshInterval} } * 1000 || 10000 ;
31+ let amount
32+ const url = `https://data.ulule.com/projects/${ projectId } /stats.json?cachebuster=${ Date . now ( ) } `
33+ const refreshInterval = obj . fieldData . refreshInterval * 1000 || 10000
3434
35- fetchStats ( ) ;
35+ fetchStats ( )
3636 setInterval ( async ( ) => {
37- fetchStats ( ) ;
38- } , refreshInterval ) ;
37+ fetchStats ( )
38+ } , refreshInterval )
3939
4040 async function fetchStats ( ) {
4141 try {
42- const response = await fetch ( url ) ;
42+ const response = await fetch ( url )
4343 if ( ! response . ok ) {
44- throw new Error ( `[ulule/stats] Response status: ${ response . status } ` ) ;
44+ throw new Error ( `[ulule/stats] Response status: ${ response . status } ` )
4545 }
4646 const {
4747 amount_raised : amountRaised ,
4848 committed,
4949 goal,
5050 orders_count : ordersCount ,
51- supporters_count : supportersCount
52- } = await response . json ( ) ;
51+ supporters_count : supportersCount ,
52+ } = await response . json ( )
5353 if ( ! amount ) {
54- amount = committed ;
54+ amount = committed
5555 }
5656 const isFinancial = amountRaised === String ( committed )
57- const suffix = isFinancial ? '{{ currency}}' : '{{ presaleSuffix}}' ;
57+ const suffix = isFinancial ? obj . fieldData . currency : obj . fieldData . presaleSuffix
5858
59- $ ( { amount : amount } ) . animate ( { amount : committed } , {
60- duration : 3000 ,
61- easing :'swing' ,
62- step : function ( ) {
63- const goalElement = goal > 0 ? `<span class="amount__goal"> / ${ goal . toLocaleString ( LOCALE ) } ${ suffix } <span></div>` : '' ;
64- $ ( '.amount' ) . html ( `<div>${ Math . round ( this . amount ) . toLocaleString ( LOCALE ) } ${ isFinancial ? suffix : '' } ${ goalElement } ` ) ;
65- }
66- } ) ;
67- amount = committed ;
59+ $ ( { amount : amount } ) . animate (
60+ { amount : committed } ,
61+ {
62+ duration : 3000 ,
63+ easing : 'swing' ,
64+ step : function ( ) {
65+ const goalElement =
66+ goal > 0 ? `<span class="amount__goal"> / ${ goal . toLocaleString ( LOCALE ) } ${ suffix } <span></div>` : ''
67+ $ ( '.amount' ) . html (
68+ `<div>${ Math . round ( this . amount ) . toLocaleString ( LOCALE ) } ${ isFinancial ? suffix : '' } ${ goalElement } ` ,
69+ )
70+ } ,
71+ } ,
72+ )
73+ amount = committed
6874
6975 if ( goal > 0 ) {
70- const progress = Math . floor ( committed / goal * 100 ) ;
71- $ ( ".progress-bar" ) . show ( ) ;
72- if ( { { targetPercentage} } > 100 ) {
73- stretchGoalPercentage = ( progress / { { targetPercentage} } ) * 100 ;
74- console . log ( 'stretchGoal' , stretchGoalPercentage ) ;
75- $ ( ".progress-bar__content" ) . animate ( {
76- easing : 'swing' ,
77- width : `${ stretchGoalPercentage > 100 ? 100 : stretchGoalPercentage } %` ,
78- } , 1000 ) ;
76+ const progress = Math . floor ( ( committed / goal ) * 100 )
77+ $ ( '.progress-bar' ) . show ( )
78+ if ( obj . fieldData . targetPercentage > 100 ) {
79+ stretchGoalPercentage = ( progress / obj . fieldData . targetPercentage ) * 100
80+ console . log ( 'stretchGoal' , stretchGoalPercentage )
81+ $ ( '.progress-bar__content' ) . animate (
82+ {
83+ easing : 'swing' ,
84+ width : `${ stretchGoalPercentage > 100 ? 100 : stretchGoalPercentage } %` ,
85+ } ,
86+ 1000 ,
87+ )
7988 if ( stretchGoalPercentage < 100 ) {
80- $ ( " .stretch-goal" ) . text ( `Prochain palier : {{ targetPercentage}} %` ) ;
89+ $ ( ' .stretch-goal' ) . text ( `Prochain palier : ${ obj . fieldData . targetPercentage } %` )
8190 }
82- $ ( " .progress-bar__content" ) . text ( `${ progress } %` ) ;
91+ $ ( ' .progress-bar__content' ) . text ( `${ progress } %` )
8392 } else {
84- $ ( ".progress-bar__content" ) . animate ( {
85- width : `${ progress > 100 ? 100 : progress } %` ,
86- } , 1000 ) ;
87- $ ( ".progress-bar__content" ) . text ( `${ progress } %` ) ;
93+ $ ( '.progress-bar__content' ) . animate (
94+ {
95+ width : `${ progress > 100 ? 100 : progress } %` ,
96+ } ,
97+ 1000 ,
98+ )
99+ $ ( '.progress-bar__content' ) . text ( `${ progress } %` )
88100 }
89101 }
90102 } catch ( error ) {
91- console . error ( '[ulule/stats] Failed to fetch stats' , error . message ) ;
103+ console . error ( '[ulule/stats] Failed to fetch stats' , error . message )
92104 }
93105 }
94- } ) ;
106+ } )
0 commit comments