Skip to content

Commit 707f951

Browse files
committed
some more tweaks for the prpl-badge-progress-bar component
1 parent 34809f2 commit 707f951

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

assets/js/web-components/prpl-badge-progress-bar.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ customElements.define(
3737
maxPoints: parseInt(
3838
this.getAttribute( 'data-max-points' ) || 10
3939
),
40+
brandingId: parseInt(
41+
this.getAttribute( 'data-branding-id' ) || 0
42+
),
4043
};
4144
}
4245

@@ -75,6 +78,10 @@ customElements.define(
7578
* Get the progress percent.
7679
*/
7780
get progressPercent() {
81+
// Prevent division by zero.
82+
if ( 0 === this.maxPoints ) {
83+
return 0;
84+
}
7885
return ( this.points / this.maxPoints ) * 100;
7986
}
8087

@@ -103,8 +110,14 @@ customElements.define(
103110
// Convert kebab-case to camelCase
104111
.replace( /-([a-z])/g, ( _, chr ) => chr.toUpperCase() );
105112

106-
// Update state.
107-
this.state[ camelCaseName ] = newVal;
113+
// Update state with proper type conversion.
114+
this.state[ camelCaseName ] = [
115+
'points',
116+
'maxPoints',
117+
'brandingId',
118+
].includes( camelCaseName )
119+
? parseInt( newVal || 0 )
120+
: newVal;
108121

109122
// Update progress.
110123
this.updateProgress();

0 commit comments

Comments
 (0)