woocommerce/plugins/woocommerce-admin/client/marketplace/components/my-subscriptions/subscriptions-expired-expir...

99 lines
2.5 KiB
TypeScript
Raw Normal View History

Show notice for expired and expiring subscriptions in settings and in-app extensions page (#47004) * show notice on WC core about subs expired * add notice in wc core my subscription page * dismiss subscription notice * add rest api for dismiss subscription notioce * dismiss notice permanently * code refactor * fix issue in expired subs * not showing expiring subs notice if expired sub notice render not showing expiring subs notice if expired sub notice render * fix lint * added changelog * update comment * fix js lint * update response * added new endpoint for notice * update the endpoint URL * update the endpoint URL * show notice after one month * add css class for refactor * fix lint * Add missing callback after the merge * check plugin is installed on current site * add comments, and fix missing price * fix lint * remove unnecessary duplicate asset load * fix notice so that it only trigger dismiss API on notices close * localize the renew product price * track events in the wc settings page * Use the correct field product_regular_price instead of product_price * Add missing period in the notice message * add nonce to the dismiss notice API * extract dismiss notice API call to different code * extract expired and expiring component to 1 component * add track events for tsx components * fix nonce checking * fix lint * fix lint * enrich the button and hyperlink url to contains the product_id of expiring / expired subscription * fix lint * fix lint --------- Co-authored-by: Akeda Bagus <akeda.bagus@automattic.com> Co-authored-by: prahesa.setia <prahesa.kusuma.setia@automattic.com>
2024-05-31 01:34:36 +00:00
/**
* External dependencies
*/
import { Button } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { recordEvent } from '@woocommerce/tracks';
/**
* Internal dependencies
*/
import Notice from '~/marketplace/components/notice/notice';
import { getAdminSetting } from '~/utils/admin-settings';
export interface SubscriptionsExpiredExpiringNoticeProps {
type: string;
}
export interface TrackEvents {
shown: string;
clicked: string;
dismissed: string;
}
export default function SubscriptionsExpiredExpiringNotice(
props: SubscriptionsExpiredExpiringNoticeProps
): JSX.Element | null {
const { type } = props;
const wccomSettings = getAdminSetting( 'wccomHelper', {} );
const eventKeys: Record< string, TrackEvents > = {
'woo-subscription-expired-notice': {
shown: 'woo_subscription_expired_notice_in_marketplace_shown',
clicked: 'woo_subscription_expired_notice_in_marketplace_clicked',
dismissed:
'woo_subscription_expired_notice_in_marketplace_dismissed',
},
'woo-subscription-expiring-notice': {
shown: 'woo_subscription_expiring_notice_in_marketplace_shown',
clicked: 'woo_subscription_expiring_notice_in_marketplace_clicked',
dismissed:
'woo_subscription_expiring_notice_in_marketplace_dismissed',
},
};
let notice = null;
let notice_id = '';
const dismiss_notice_nonce = wccomSettings?.dismissNoticeNonce || '';
if ( type === 'expired' ) {
notice = wccomSettings?.subscription_expired_notice || {};
notice_id = 'woo-subscription-expired-notice';
} else if ( type === 'expiring' ) {
notice = wccomSettings?.subscription_expiring_notice || {};
notice_id = 'woo-subscription-expiring-notice';
} else {
return null;
}
if ( ! wccomSettings.isConnected || ! notice?.description ) {
return null;
}
const handleClose = () => {
recordEvent( eventKeys[ notice_id ].dismissed );
const data = { notice_id, dismiss_notice_nonce };
apiFetch( {
path: `/wc-admin/notice/dismiss`,
method: 'POST',
data,
} );
};
function handleClick() {
recordEvent( eventKeys[ notice_id ].clicked );
}
function handleLoad() {
recordEvent( eventKeys[ notice_id ].shown );
}
return (
<Notice
id={ notice_id }
description={ notice.description }
isDismissible={ true }
variant="error"
onClose={ handleClose }
onLoad={ handleLoad }
>
<Button
href={ notice.button_link }
variant="secondary"
onClick={ handleClick }
>
{ notice.button_text }
</Button>
</Notice>
);
}