Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions: stop loading subscribe block and panels when module disabled #39802

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Subscriptions: stops loading editor functionality (i.e. the block and panels) when subscriptions module is disabled.
195 changes: 67 additions & 128 deletions projects/plugins/jetpack/extensions/blocks/subscriptions/edit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { numberFormat, ThemeProvider } from '@automattic/jetpack-components';
import { useModuleStatus } from '@automattic/jetpack-shared-extension-utils';
import {
BlockControls,
InspectorControls,
RichText,
withColors,
withFontSizes,
useBlockProps,
__experimentalUseGradient as useGradient, // eslint-disable-line @wordpress/no-unsafe-wp-apis
} from '@wordpress/block-editor';
import { TextControl, Toolbar, withFallbackStyles } from '@wordpress/components';
Expand All @@ -18,7 +16,6 @@ import clsx from 'clsx';
import { isEqual } from 'lodash';
import { getActiveStyleName } from '../../shared/block-styles';
import { getValidatedAttributes } from '../../shared/get-validated-attributes';
import { isNewsletterFeatureEnabled } from '../../shared/memberships/edit';
import GetAddPaidPlanButton, { paidPlanButtonText } from '../../shared/memberships/utils';
import './view.scss';
import { store as membershipProductsStore } from '../../store/membership-products';
Expand All @@ -34,13 +31,10 @@ import {
DEFAULT_SUCCESS_MESSAGE,
} from './constants';
import SubscriptionControls from './controls';
import { SubscriptionsPlaceholder } from './subscription-placeholder';
import SubscriptionSkeletonLoader from './subscription-skeleton-loader';

const { getComputedStyle } = window;
const isGradientAvailable = !! useGradient;
const useGradientIfAvailable = isGradientAvailable ? useGradient : () => ( {} );
const name = metadata.name.replace( 'jetpack/', '' );

const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
const { buttonBackgroundColor, textColor } = ownProps;
Expand Down Expand Up @@ -76,10 +70,6 @@ export function SubscriptionEdit( props ) {
hasTierPlans,
} = props;

const blockProps = useBlockProps();
const { isLoadingModules, isChangingStatus, isModuleActive, changeStatus } =
useModuleStatus( name );

const validatedAttributes = getValidatedAttributes( metadata.attributes, attributes );
if ( ! isEqual( validatedAttributes, attributes ) ) {
setAttributes( validatedAttributes );
Expand All @@ -103,12 +93,6 @@ export function SubscriptionEdit( props ) {
const activeStyleName = getActiveStyleName( metadata.styles, className );

const { subscriberCount, subscriberCountString } = useSelect( select => {
if ( ! isModuleActive ) {
return {
subscriberCounts: 0,
subscriberCountString: '',
};
}
const { emailSubscribers, socialFollowers } =
select( membershipProductsStore ).getSubscriberCounts();
let count = emailSubscribers;
Expand Down Expand Up @@ -217,132 +201,87 @@ export function SubscriptionEdit( props ) {
const previousButtonBackgroundColor = usePrevious( buttonBackgroundColor );

useEffect( () => {
if ( ! isModuleActive ) {
return;
}
if (
previousButtonBackgroundColor?.color !== borderColor?.color ||
borderColor?.color === buttonBackgroundColor?.color
) {
return;
}
setBorderColor( buttonBackgroundColor.color );
}, [
buttonBackgroundColor,
previousButtonBackgroundColor,
borderColor,
setBorderColor,
isModuleActive,
] );

let content;

const paidPlanLabel = paidPlanButtonText( hasTierPlans );
}, [ buttonBackgroundColor, previousButtonBackgroundColor, borderColor, setBorderColor ] );

if ( isLoadingModules ) {
content = <SubscriptionSkeletonLoader />;
} else if ( ! isModuleActive ) {
content = (
<SubscriptionsPlaceholder
changeStatus={ changeStatus }
isModuleActive={ isModuleActive }
isLoading={ isChangingStatus }
/>
);
} else {
content = (
<>
<InspectorControls>
<SubscriptionControls
buttonBackgroundColor={ buttonBackgroundColor }
borderColor={ borderColor }
buttonGradient={ buttonGradient }
borderRadius={ borderRadius }
borderWeight={ borderWeight }
buttonOnNewLine={ buttonOnNewLine }
emailFieldBackgroundColor={ emailFieldBackgroundColor }
fallbackButtonBackgroundColor={ fallbackButtonBackgroundColor }
fallbackTextColor={ fallbackTextColor }
fontSize={ fontSize }
includeSocialFollowers={ includeSocialFollowers }
isGradientAvailable={ isGradientAvailable }
padding={ padding }
setAttributes={ setAttributes }
setBorderColor={ setBorderColor }
setButtonBackgroundColor={ setButtonBackgroundColor }
setTextColor={ setTextColor }
showSubscribersTotal={ showSubscribersTotal }
spacing={ spacing }
subscriberCount={ subscriberCount }
textColor={ textColor }
buttonWidth={ buttonWidth }
subscribePlaceholder={ subscribePlaceholder }
submitButtonText={ submitButtonText }
successMessage={ successMessage }
/>
</InspectorControls>
{ isNewsletterFeatureEnabled() && (
<BlockControls>
<Toolbar label={ paidPlanLabel }>
<GetAddPaidPlanButton context={ 'toolbar' } hasTierPlans={ hasTierPlans } />
</Toolbar>
</BlockControls>
) }

<div style={ cssVars }>
<div className="wp-block-jetpack-subscriptions__container is-not-subscriber">
<div className="wp-block-jetpack-subscriptions__form" role="form">
<div className="wp-block-jetpack-subscriptions__form-elements">
{ activeStyleName !== 'button' && (
<TextControl
__nextHasNoMarginBottom={ true }
placeholder={ subscribePlaceholder }
disabled={ true }
className={ clsx(
emailFieldClasses,
'wp-block-jetpack-subscriptions__textfield'
) }
style={ emailFieldStyles }
/>
) }
<RichText
return (
<>
<InspectorControls>
<SubscriptionControls
buttonBackgroundColor={ buttonBackgroundColor }
borderColor={ borderColor }
buttonGradient={ buttonGradient }
borderRadius={ borderRadius }
borderWeight={ borderWeight }
buttonOnNewLine={ buttonOnNewLine }
emailFieldBackgroundColor={ emailFieldBackgroundColor }
fallbackButtonBackgroundColor={ fallbackButtonBackgroundColor }
fallbackTextColor={ fallbackTextColor }
fontSize={ fontSize }
includeSocialFollowers={ includeSocialFollowers }
isGradientAvailable={ isGradientAvailable }
padding={ padding }
setAttributes={ setAttributes }
setBorderColor={ setBorderColor }
setButtonBackgroundColor={ setButtonBackgroundColor }
setTextColor={ setTextColor }
showSubscribersTotal={ showSubscribersTotal }
spacing={ spacing }
subscriberCount={ subscriberCount }
textColor={ textColor }
buttonWidth={ buttonWidth }
subscribePlaceholder={ subscribePlaceholder }
submitButtonText={ submitButtonText }
successMessage={ successMessage }
/>
</InspectorControls>
<BlockControls>
<Toolbar label={ paidPlanButtonText( hasTierPlans ) }>
<GetAddPaidPlanButton context={ 'toolbar' } hasTierPlans={ hasTierPlans } />
</Toolbar>
</BlockControls>
<div style={ cssVars }>
<div className="wp-block-jetpack-subscriptions__container is-not-subscriber">
<div className="wp-block-jetpack-subscriptions__form" role="form">
<div className="wp-block-jetpack-subscriptions__form-elements">
{ activeStyleName !== 'button' && (
<TextControl
__nextHasNoMarginBottom={ true }
placeholder={ subscribePlaceholder }
disabled={ true }
className={ clsx(
buttonClasses,
'wp-block-jetpack-subscriptions__button',
'wp-block-button__link'
emailFieldClasses,
'wp-block-jetpack-subscriptions__textfield'
) }
onChange={ value => setAttributes( { submitButtonText: value } ) }
style={ buttonStyles }
value={ submitButtonText }
withoutInteractiveFormatting
allowedFormats={ [ 'core/bold', 'core/italic', 'core/strikethrough' ] }
style={ emailFieldStyles }
/>
</div>
) }
<RichText
className={ clsx(
buttonClasses,
'wp-block-jetpack-subscriptions__button',
'wp-block-button__link'
) }
onChange={ value => setAttributes( { submitButtonText: value } ) }
style={ buttonStyles }
value={ submitButtonText }
withoutInteractiveFormatting
allowedFormats={ [ 'core/bold', 'core/italic', 'core/strikethrough' ] }
/>
</div>
</div>
{ showSubscribersTotal && (
<div className="wp-block-jetpack-subscriptions__subscount">
{ subscriberCountString }
</div>
) }
</div>
</>
);
}

return (
<div
{ ...blockProps }
className={ clsx(
blockProps.className,
'wp-block-jetpack-subscriptions__container',
'wp-block-jetpack-subscriptions__supports-newline',
buttonOnNewLine ? 'wp-block-jetpack-subscriptions__use-newline' : undefined,
showSubscribersTotal ? 'wp-block-jetpack-subscriptions__show-subs' : undefined
) }
>
{ content }
</div>
{ showSubscribersTotal && (
<div className="wp-block-jetpack-subscriptions__subscount">{ subscriberCountString }</div>
) }
</div>
</>
);
}

Expand Down
Loading
Loading