diff --git a/includes/assets/js/NFDPluginsMarketplace.js b/includes/assets/js/NFDPluginsMarketplace.js index 8a4df72..f4981dc 100644 --- a/includes/assets/js/NFDPluginsMarketplace.js +++ b/includes/assets/js/NFDPluginsMarketplace.js @@ -1,99 +1,119 @@ /** * Class to show Marketplace products (plugins) in a tab under plugin-install.php */ -class NFDPluginsMarketplace { - constructor() { - // Add event listener for when the DOM is loaded - window.addEventListener('DOMContentLoaded', () => { - this.setupContainer(); - - // Fetch data from the Marketplace API - fetch(nfdPremiumPluginsMarketplace.restApiRoot + '/newfold-marketplace/v1/marketplace', { - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json', - 'X-WP-Nonce': nfdPremiumPluginsMarketplace.restApiNonce - } - }) - .then((response) => response.json()) - .then((data) => { - if (! data.hasOwnProperty('products')) { - this.isError(); - } else { - this.isLoaded(); - this.showProducts(this.filterProducts(data.products.data)); - } - }) - }) - } - // Function that sets up the container for the plugin marketplace - setupContainer() { - const pluginsListTable = document.createElement('div'); - pluginsListTable.classList.add('wp-list-table', 'widefat', 'plugin-install', 'nfd-premium-plugins-marketplace'); - pluginsListTable.innerHTML = `

Plugins list

+class NFDPluginsMarketplace { + constructor() { + // Add event listener for when the DOM is loaded + window.addEventListener('DOMContentLoaded', () => { + this.setupContainer(); + + // Fetch data from the Marketplace API + fetch( + nfdPremiumPluginsMarketplace.restApiRoot + + '/newfold-marketplace/v1/marketplace', + { + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json', + 'X-WP-Nonce': nfdPremiumPluginsMarketplace.restApiNonce, + }, + } + ) + .then((response) => response.json()) + .then((data) => { + if (!data.hasOwnProperty('products')) { + this.isError(); + } else { + this.isLoaded(); + this.showProducts(this.filterProducts(data.products.data)); + } + }); + }); + } + + // Function that sets up the container for the plugin marketplace + setupContainer() { + const pluginsListTable = document.createElement('div'); + pluginsListTable.classList.add( + 'wp-list-table', + 'widefat', + 'plugin-install', + 'nfd-premium-plugins-marketplace' + ); + pluginsListTable.innerHTML = `

Plugins list

${nfdPremiumPluginsMarketplace.marketplaceDescription}

${this.isLoading()}
`; - const filterArea = document.querySelector('.plugin-install-tab-premium-marketplace .wp-filter'); - filterArea.parentNode.insertBefore(pluginsListTable, filterArea.nextSibling); - } - - // Filter products to only show plugins - filterProducts(products) { - const plugins = products.filter(product => product.type == "plugin"); - - return plugins; + const filterArea = document.querySelector( + '.plugin-install-tab-premium-marketplace .wp-filter' + ); + filterArea.parentNode.insertBefore( + pluginsListTable, + filterArea.nextSibling + ); + } + + // Filter products to only show plugins + filterProducts(products) { + const plugins = products.filter((product) => product.type == 'plugin'); + + return plugins; + } + + // Returns whether the environment supports CTB + supportsCTB() { + if (typeof nfdctb === 'undefined') { + return false; } - // Returns whether the environment supports CTB - supportsCTB() { - if (typeof nfdctb === 'undefined') { - return false; - } + return nfdctb.supportsCTB; + } - return nfdctb.supportsCTB; - } + // Show the filtered products on the page + showProducts(products) { + // Helper function to render the thumbnail for each product + const renderThumbnail = (product) => { + if (product.smallThumbnailUrl) { + return `${product.name} Thumbnail`; + } - // Show the filtered products on the page - showProducts(products) { - - // Helper function to render the thumbnail for each product - const renderThumbnail = (product) => { - if (product.smallThumbnailUrl) { - return `${product.name} Thumbnail`; - } - - const fallback = 'https://hiive-space-cdn.nyc3.cdn.digitaloceanspaces.com/marketplace/premium-plugins/fallback.svg'; - return `${product.name} Thumbnail`; - } + const fallback = + 'https://hiive-space-cdn.nyc3.cdn.digitaloceanspaces.com/marketplace/premium-plugins/fallback.svg'; + return `${product.name} Thumbnail`; + }; - // Helper function to render the action buttons for each product - const renderActions = (product) => { - const supportsCTB = this.supportsCTB(); - let actionButtons = []; + // Helper function to render the action buttons for each product + const renderActions = (product) => { + let actionButtons = []; - if (product.primaryUrl && product.price) { - const primaryAction = `Buy $${product.price}`; + ${ + product?.clickToBuyId + ? 'data-action="load-nfd-ctb" data-ctb-id="' + + product?.clickToBuyId + + '"' + : '' + }>Buy $${product.price}`; - actionButtons.push(primaryAction); - } + actionButtons.push(primaryAction); + } - if (product.secondaryUrl) { - const secondaryAction = `More Details`; + if (product.secondaryUrl) { + const secondaryAction = `More Details`; - actionButtons.push(secondaryAction); - } - - return actionButtons.join(''); - } - - // Helper function to render a template for each product - const buildProduct = (product) => { - return `
+ actionButtons.push(secondaryAction); + } + + return actionButtons.join(''); + }; + + // Helper function to render a template for each product + const buildProduct = (product) => { + return `
${renderThumbnail(product)} @@ -109,20 +129,19 @@ class NFDPluginsMarketplace {
`; - } - - const render = products.map(product => buildProduct(product)).join(''); + }; - const pluginsListTable = document.querySelector('.wp-list-table #the-list'); - return pluginsListTable.innerHTML = render; - } + const render = products.map((product) => buildProduct(product)).join(''); - // Shows skeleton when the marketplace is loading API data - isLoading() { + const pluginsListTable = document.querySelector('.wp-list-table #the-list'); + return (pluginsListTable.innerHTML = render); + } - // Helper function to generate skeleton template - const skeleton = (styles, customClass) => { - return `
{ + return `
`; - } + }; - // Helper function to build a product skeleton template - const buildSkeleton = () => { - return `
+ // Helper function to build a product skeleton template + const buildSkeleton = () => { + return `
- ${skeleton({width:"128px", height:"128px"})} + ${skeleton({ width: '128px', height: '128px' })}
- ${skeleton({width:"70%", height:"25px", marginBottom:"20px"})} + ${skeleton({ + width: '70%', + height: '25px', + marginBottom: '20px', + })}
- ${skeleton({width:"90px", height:"32px", marginRight:"0px", marginLeft:"auto"})} + ${skeleton({ + width: '90px', + height: '32px', + marginRight: '0px', + marginLeft: 'auto', + })}
- ${skeleton({width:"100%", height:"12px"})} - ${skeleton({width:"90%", height:"12px", marginTop:"8px"})} - ${skeleton({width:"100%", height:"12px", marginTop:"8px"})} - ${skeleton({width:"60%", height:"12px", marginTop:"8px"})} + ${skeleton({ width: '100%', height: '12px' })} + ${skeleton({ + width: '90%', + height: '12px', + marginTop: '8px', + })} + ${skeleton({ + width: '100%', + height: '12px', + marginTop: '8px', + })} + ${skeleton({ + width: '60%', + height: '12px', + marginTop: '8px', + })}
`; - } + }; - const items = 12; - let pluginCardsSkeleton = []; + const items = 12; + let pluginCardsSkeleton = []; - for (let i = 0; i < items; i++) { - pluginCardsSkeleton += buildSkeleton(); - } - - return pluginCardsSkeleton; + for (let i = 0; i < items; i++) { + pluginCardsSkeleton += buildSkeleton(); } - // Clears the marketplace container from skeleton HTML - isLoaded() { - const pluginsListTable = document.querySelector('.wp-list-table #the-list'); - pluginsListTable.innerHTML = ""; - } + return pluginCardsSkeleton; + } - // Shows error message when API fail - isError() { - const pluginsListTable = document.querySelector('.wp-list-table #the-list'); - pluginsListTable.innerHTML = `
+ // Clears the marketplace container from skeleton HTML + isLoaded() { + const pluginsListTable = document.querySelector('.wp-list-table #the-list'); + pluginsListTable.innerHTML = ''; + } + + // Shows error message when API fail + isError() { + const pluginsListTable = document.querySelector('.wp-list-table #the-list'); + pluginsListTable.innerHTML = `
Error laoding content

Oops, something went wrong.

@@ -184,12 +224,12 @@ class NFDPluginsMarketplace {
`; - document.addEventListener('mouseup', (e) => { - if (e.target.id === 'nfd-premium-marketplace-reload') { - location.reload(); - } - }) - } + document.addEventListener('mouseup', (e) => { + if (e.target.id === 'nfd-premium-marketplace-reload') { + location.reload(); + } + }); + } } const nfdPluginsMarketplace = new NFDPluginsMarketplace();