2023-09-21 14:00:04 +00:00
/ * *
* External dependencies
* /
import { __ , sprintf } from '@wordpress/i18n' ;
import { Button , Tooltip } from '@wordpress/components' ;
import { getNewPath } from '@woocommerce/navigation' ;
import { help } from '@wordpress/icons' ;
import { Table } from '@woocommerce/components' ;
2023-09-26 12:12:14 +00:00
import { useContext , useEffect , useState } from '@wordpress/element' ;
2023-09-21 14:00:04 +00:00
/ * *
* Internal dependencies
* /
2023-09-26 12:12:14 +00:00
import { getAdminSetting } from '../../../utils/admin-settings' ;
2023-09-21 14:00:04 +00:00
import { Subscription } from './types' ;
import './my-subscriptions.scss' ;
2023-09-26 12:12:14 +00:00
import { MarketplaceContext } from '../../contexts/marketplace-context' ;
import { fetchSubscriptions } from '../../../marketplace/utils/functions' ;
2023-09-21 14:00:04 +00:00
export default function MySubscriptions ( ) : JSX . Element {
2023-09-26 12:12:14 +00:00
const [ subscriptions , setSubscriptions ] = useState <
Array < Subscription >
> ( [ ] ) ;
const marketplaceContextValue = useContext ( MarketplaceContext ) ;
const { setIsLoading } = marketplaceContextValue ;
// Get the content for this screen
useEffect ( ( ) = > {
setIsLoading ( true ) ;
fetchSubscriptions ( )
. then ( ( subscriptionResponse ) = > {
setSubscriptions ( subscriptionResponse ) ;
} )
. finally ( ( ) = > {
setIsLoading ( false ) ;
} ) ;
} , [ ] ) ;
2023-09-21 14:11:19 +00:00
const updateConnectionUrl = getNewPath (
{
page : 'wc-addons' ,
section : 'helper' ,
filter : 'all' ,
'wc-helper-refresh' : 1 ,
'wc-helper-nonce' : getAdminSetting ( 'wc_helper_nonces' ) . refresh ,
} ,
''
) ;
const updateConnectionHTML = sprintf (
2023-09-26 12:12:14 +00:00
// translators: %s is a link to the update connection page.
2023-09-21 14:11:19 +00:00
__ (
'If you don\'t see your subscription, try <a href="%s">updating</a> your connection.' ,
'woocommerce'
) ,
updateConnectionUrl
) ;
2023-09-21 14:00:04 +00:00
const tableHeadersInstalled = [
{
key : 'name' ,
label : __ ( 'Name' , 'woocommerce' ) ,
} ,
{
key : 'status' ,
label : __ ( 'Status' , 'woocommerce' ) ,
} ,
{
key : 'expiry' ,
label : __ ( 'Expiry/Renewal date' , 'woocommerce' ) ,
} ,
{
key : 'autoRenew' ,
label : __ ( 'Auto-renew' , 'woocommerce' ) ,
} ,
{
key : 'version' ,
label : __ ( 'Version' , 'woocommerce' ) ,
isNumeric : true ,
} ,
{
key : 'activated' ,
label : __ ( 'Activated' , 'woocommerce' ) ,
} ,
{
key : 'actions' ,
label : __ ( 'Actions' , 'woocommerce' ) ,
} ,
] ;
2023-09-26 12:12:14 +00:00
const subscriptionsInstalled : Array < Subscription > = subscriptions . filter (
2023-09-27 10:21:50 +00:00
( subscription : Subscription ) = >
subscription . local . active &&
( subscription . active || subscription . product_key === '' )
2023-09-26 12:12:14 +00:00
) ;
2023-09-21 14:00:04 +00:00
const tableHeadersAvailable = [
{
key : 'name' ,
label : __ ( 'Name' , 'woocommerce' ) ,
} ,
{
key : 'status' ,
label : __ ( 'Status' , 'woocommerce' ) ,
} ,
{
key : 'expiry' ,
label : __ ( 'Expiry/Renewal date' , 'woocommerce' ) ,
} ,
{
key : 'autoRenew' ,
label : __ ( 'Auto-renew' , 'woocommerce' ) ,
} ,
{
key : 'version' ,
label : __ ( 'Version' , 'woocommerce' ) ,
isNumeric : true ,
} ,
{
key : 'install' ,
label : __ ( 'Install' , 'woocommerce' ) ,
} ,
{
key : 'actions' ,
label : __ ( 'Actions' , 'woocommerce' ) ,
} ,
] ;
2023-09-26 12:12:14 +00:00
const subscriptionsAvailable : Array < Subscription > = subscriptions . filter (
2023-09-27 10:21:50 +00:00
( subscription : Subscription ) = >
! subscriptionsInstalled . includes ( subscription )
2023-09-26 12:12:14 +00:00
) ;
2023-09-21 14:00:04 +00:00
2023-09-27 10:21:50 +00:00
const getStatus = ( subscription : Subscription ) : string = > {
// TODO add statuses for subscriptions
if ( subscription . product_key === '' ) {
return __ ( 'Not found' , 'woocommerce' ) ;
} else if ( subscription . expired ) {
return __ ( 'Expired' , 'woocommerce' ) ;
}
return __ ( 'Active' , 'woocommerce' ) ;
} ;
const getVersion = ( subscription : Subscription ) : string = > {
if ( subscription . local . version === subscription . version ) {
return subscription . local . version ;
}
return subscription . local . version + ' > ' + subscription . version ;
} ;
2023-09-21 14:00:04 +00:00
return (
< div className = "woocommerce-marketplace__my-subscriptions" >
< section >
2023-09-21 14:11:19 +00:00
< h2 > { __ ( 'Installed on this store' , 'woocommerce' ) } < / h2 >
2023-09-21 14:00:04 +00:00
< p >
2023-09-21 14:11:19 +00:00
< span
dangerouslySetInnerHTML = { {
__html : updateConnectionHTML ,
} }
/ >
2023-09-21 14:00:04 +00:00
< Tooltip
text = {
< >
2023-09-22 10:05:42 +00:00
< h3 >
{ __ (
"Still don't see your subscription?" ,
'woocommerce'
) }
< / h3 >
< p
dangerouslySetInnerHTML = { {
__html : __ (
'To see all your subscriptions go to <a href="https://woocommerce.com/my-account/" target="_blank" class="woocommerce-marketplace__my-subscriptions__tooltip-external-link">your account</a> on WooCommerce.com.' ,
'woocommerce'
) ,
} }
/ >
2023-09-21 14:00:04 +00:00
< / >
}
>
2023-09-21 14:11:19 +00:00
< Button
icon = { help }
iconSize = { 20 }
isSmall = { true }
label = { __ ( 'Help' , 'woocommerce' ) }
/ >
2023-09-21 14:00:04 +00:00
< / Tooltip >
< / p >
< Table
headers = { tableHeadersInstalled }
rows = { subscriptionsInstalled . map ( ( item ) = > {
return [
2023-09-26 12:12:14 +00:00
{ display : item.product_name } ,
2023-09-27 10:21:50 +00:00
{ display : getStatus ( item ) } ,
2023-09-26 12:12:14 +00:00
{ display : item.expires } ,
{ display : item.autorenew ? 'true' : 'false' } ,
2023-09-27 10:21:50 +00:00
{ display : getVersion ( item ) } ,
2023-09-26 12:12:14 +00:00
{ display : item.active ? 'true' : 'false' } ,
2023-09-21 14:00:04 +00:00
{ display : '...' } ,
] ;
} ) }
/ >
< / section >
< section >
2023-09-21 14:11:19 +00:00
< h2 > { __ ( 'Available' , 'woocommerce' ) } < / h2 >
2023-09-21 14:00:04 +00:00
< p >
2023-09-21 14:11:19 +00:00
{ __ (
'Your unused and free WooCommerce.com subscriptions.' ,
'woocommerce'
) }
2023-09-21 14:00:04 +00:00
< / p >
< Table
2023-09-21 14:11:19 +00:00
headers = { tableHeadersAvailable }
2023-09-21 14:00:04 +00:00
rows = { subscriptionsAvailable . map ( ( item ) = > {
return [
2023-09-26 12:12:14 +00:00
{ display : item.product_name } ,
2023-09-27 10:21:50 +00:00
{ display : getStatus ( item ) } ,
2023-09-26 12:12:14 +00:00
{ display : item.expires } ,
{ display : item.autorenew ? 'true' : 'false' } ,
2023-09-27 10:21:50 +00:00
{ display : getVersion ( item ) } ,
2023-09-21 14:00:04 +00:00
{ display : '...' } ,
{ display : '...' } ,
] ;
} ) }
/ >
< / section >
< / div >
) ;
}