woocommerce/plugins/woocommerce-admin/client/marketplace/components/my-subscriptions/my-subscriptions.tsx

237 lines
5.9 KiB
TypeScript
Raw Normal View History

/**
* 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';
/**
* Internal dependencies
*/
2023-09-26 12:12:14 +00:00
import { getAdminSetting } from '../../../utils/admin-settings';
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';
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
);
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 &&
2023-09-27 11:03:56 +00:00
( subscription.active ||
subscription.product_key === '' ||
subscription.expired )
2023-09-26 12:12:14 +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-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;
2023-09-27 10:26:13 +00:00
} else if ( subscription.local.version && subscription.version ) {
return subscription.local.version + ' > ' + subscription.version;
} else if ( subscription.version ) {
return subscription.version;
} else if ( subscription.local.version ) {
return subscription.local.version;
2023-09-27 10:21:50 +00:00
}
2023-09-27 10:26:13 +00:00
return '';
2023-09-27 10:21:50 +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>
<p>
2023-09-21 14:11:19 +00:00
<span
dangerouslySetInnerHTML={ {
__html: updateConnectionHTML,
} }
/>
<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:11:19 +00:00
<Button
icon={ help }
iconSize={ 20 }
isSmall={ true }
label={ __( 'Help', 'woocommerce' ) }
/>
</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' },
{ display: '...' },
];
} ) }
/>
</section>
<section>
2023-09-21 14:11:19 +00:00
<h2>{ __( 'Available', 'woocommerce' ) }</h2>
<p>
2023-09-21 14:11:19 +00:00
{ __(
'Your unused and free WooCommerce.com subscriptions.',
'woocommerce'
) }
</p>
<Table
2023-09-21 14:11:19 +00:00
headers={ tableHeadersAvailable }
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 ) },
{ display: '...' },
{ display: '...' },
];
} ) }
/>
</section>
</div>
);
}