diff --git a/assets/js/web-components/prpl-popover-post-content.js b/assets/js/web-components/prpl-popover-post-content.js new file mode 100644 index 000000000..83bdb8d86 --- /dev/null +++ b/assets/js/web-components/prpl-popover-post-content.js @@ -0,0 +1,50 @@ +/* global customElements, HTMLElement */ + +/** + * Register the custom web component. + */ +customElements.define( + 'prpl-popover-post-content', + class extends HTMLElement { + constructor( postId = '', buttonContent = '' ) { + // Get parent class properties + super(); + + this.postId = postId; + this.buttonContent = buttonContent; + + if ( ! this.postId && this.hasAttribute( 'post-id' ) ) { + this.postId = this.getAttribute( 'post-id' ); + } + + if ( ! this.postId ) { + return; + } + + if ( ! this.buttonContent ) { + this.buttonContent = this.getAttribute( 'button-content' ); + } + + // Get the JSON response from https://progressplanner.com/wp-json/wp/v2/posts/{postId} + fetch( + `https://progressplanner.com/wp-json/wp/v2/posts/${ this.postId }` + ) + .then( ( response ) => response.json() ) + .then( ( data ) => { + if ( ! data.content.rendered || ! data.title.rendered ) { + return; + } + + this.innerHTML = ` + +