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

Update addGroupNotice. #9136

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -24,7 +24,7 @@ import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';
import { useState, useCallback, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -33,24 +33,35 @@ import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from 'googlesitekit-data';
import {
AUDIENCE_ADD_GROUP_NOTICE_SLUG,
AUDIENCE_SELECTION_CHANGED,
AUDIENCE_SELECTED,
AUDIENCE_SELECTION_FORM,
AUDIENCE_SELECTION_PANEL_OPENED_KEY,
} from './constants';
import { CORE_FORMS } from '../../../../../../googlesitekit/datastore/forms/constants';
import { CORE_USER } from '../../../../../../googlesitekit/datastore/user/constants';
import InfoIcon from '../../../../../../../svg/icons/info-circle.svg';
import InfoNotice from '../InfoNotice';
import { CORE_UI } from '../../../../../../googlesitekit/datastore/ui/constants';
import { MODULES_ANALYTICS_4 } from '../../../../datastore/constants';

export default function AddGroupNotice() {
const [ twoOrMoreAudiencesSelected, setTwoOrMoreAudiencesSelected ] =
useState( false );

export default function AddGroupNotice( { savedItemSlugs = [] } ) {
const isDismissed = useSelect( ( select ) =>
select( CORE_USER ).isItemDismissed( AUDIENCE_ADD_GROUP_NOTICE_SLUG )
);
const selectionChanged = useSelect(
( select ) =>
select( CORE_FORMS ).getValue(
AUDIENCE_SELECTION_FORM,
AUDIENCE_SELECTION_CHANGED
) || false
const isSelectionPanelOpen = useSelect( ( select ) =>
select( CORE_UI ).getValue( AUDIENCE_SELECTION_PANEL_OPENED_KEY )
);
const isLoading = useSelect( ( select ) =>
select( MODULES_ANALYTICS_4 ).isFetchingSyncAvailableAudiences()
);
const selectedAudiences = useSelect( ( select ) =>
select( CORE_FORMS ).getValue(
AUDIENCE_SELECTION_FORM,
AUDIENCE_SELECTED
)
);

const { dismissItem } = useDispatch( CORE_USER );
Expand All @@ -59,7 +70,38 @@ export default function AddGroupNotice( { savedItemSlugs = [] } ) {
await dismissItem( AUDIENCE_ADD_GROUP_NOTICE_SLUG );
}, [ dismissItem ] );

if ( isDismissed || savedItemSlugs.length !== 1 || selectionChanged ) {
useEffect( () => {
if ( ! Array.isArray( selectedAudiences ) ) {
return;
}

if ( selectedAudiences.length > 1 ) {
setTwoOrMoreAudiencesSelected( true );
}

// If selected items changed to only 1 or less selected audiences
// reset the value. Otherwise `twoOrMoreAudiencesSelected` always remains `true`.
// We are checking if selection panel is closed to do that, so notice is not re-surfaced
// while selection panel is still open.
if ( ! isSelectionPanelOpen && selectedAudiences?.length === 1 ) {
setTwoOrMoreAudiencesSelected( false );
}
}, [
selectedAudiences,
isSelectionPanelOpen,
setTwoOrMoreAudiencesSelected,
] );

if (
// Do not render the notice if the slection panel is dismissed.
isDismissed ||
// Has two or more audiences selected.
twoOrMoreAudiencesSelected ||
// Items are showing as preview blocks - re-syncing is still in progress.
isLoading ||
// There is no audience selected.
! selectedAudiences?.length
) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function AudienceSelectionPanel() {
>
<Header closePanel={ closePanel } />
<AudienceItems savedItemSlugs={ savedItemSlugs } />
<AddGroupNotice savedItemSlugs={ savedItemSlugs } />
<AddGroupNotice />
<AudienceCreationNotice />
<LearnMoreLink />
<ErrorNotice />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,17 @@ describe( 'AudienceSelectionPanel', () => {

describe( 'AddGroupNotice', () => {
it( 'should display notice when there is a saved selection of one group', async () => {
const selectedAudiences = [ 'properties/12345/audiences/3' ];
registry
.dispatch( MODULES_ANALYTICS_4 )
.setConfiguredAudiences( [ 'properties/12345/audiences/3' ] );
.setConfiguredAudiences( selectedAudiences );

registry
.dispatch( CORE_FORMS )
.setValues( AUDIENCE_SELECTION_FORM, {
[ AUDIENCE_SELECTED ]: selectedAudiences,
[ AUDIENCE_SELECTION_CHANGED ]: true,
} );

const { getByText, waitForRegistry } = render(
<AudienceSelectionPanel />,
Expand Down
Loading