Skip to content

Commit

Permalink
Merge pull request #9174 from google/enhancement/#8800-reset-followup
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv authored Aug 7, 2024
2 parents 62dabda + 3535828 commit 5d691d3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import { __ } from '@wordpress/i18n';
*/
import { useDispatch, useSelect } from 'googlesitekit-data';
import { Button, SpinnerButton } from 'googlesitekit-components';
import { MODULES_READER_REVENUE_MANAGER } from '../../datastore/constants';
import { CORE_FORMS } from '../../../../googlesitekit/datastore/forms/constants';
import {
MODULES_READER_REVENUE_MANAGER,
READER_REVENUE_MANAGER_SETUP_FORM,
RESET_PUBLICATIONS,
} from '../../datastore/constants';
import ExternalIcon from '../../../../../svg/icons/external.svg';

export default function PublicationCreate( { onCompleteSetup } ) {
Expand All @@ -43,10 +48,17 @@ export default function PublicationCreate( { onCompleteSetup } ) {
select( MODULES_READER_REVENUE_MANAGER ).getServiceURL()
);

const { setValues } = useDispatch( CORE_FORMS );
const { selectPublication } = useDispatch( MODULES_READER_REVENUE_MANAGER );

const hasPublication = publications && publications.length > 0;

const handleLinkClick = useCallback( () => {
setValues( READER_REVENUE_MANAGER_SETUP_FORM, {
[ RESET_PUBLICATIONS ]: true,
} );
}, [ setValues ] );

const handleCompleteSetupClick = useCallback( async () => {
if ( ! hasPublication ) {
return;
Expand Down Expand Up @@ -84,6 +96,7 @@ export default function PublicationCreate( { onCompleteSetup } ) {
trailingIcon={
<ExternalIcon width={ 14 } height={ 14 } />
}
onClick={ handleLinkClick }
>
{ __( 'Create publication', 'google-site-kit' ) }
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ import { SpinnerButton } from 'googlesitekit-components';
import { useDispatch, useSelect } from 'googlesitekit-data';
import Link from '../../../../components/Link';
import StoreErrorNotices from '../../../../components/StoreErrorNotices';
import { CORE_FORMS } from '../../../../googlesitekit/datastore/forms/constants';
import {
MODULE_SLUG,
MODULES_READER_REVENUE_MANAGER,
READER_REVENUE_MANAGER_SETUP_FORM,
RESET_PUBLICATIONS,
} from '../../datastore/constants';
import { PublicationOnboardingStateNotice, PublicationSelect } from '../common';

Expand All @@ -57,10 +60,17 @@ export default function SetupForm( { onCompleteSetup } ) {
select( MODULES_READER_REVENUE_MANAGER ).getServiceURL()
);

const { setValues } = useDispatch( CORE_FORMS );
const { findMatchedPublication, selectPublication } = useDispatch(
MODULES_READER_REVENUE_MANAGER
);

const handleLinkClick = useCallback( () => {
setValues( READER_REVENUE_MANAGER_SETUP_FORM, {
[ RESET_PUBLICATIONS ]: true,
} );
}, [ setValues ] );

const submitForm = useCallback(
( event ) => {
event.preventDefault();
Expand All @@ -84,6 +94,10 @@ export default function SetupForm( { onCompleteSetup } ) {
}
}, [ findMatchedPublication, publicationID, selectPublication ] );

if ( ! publications ) {
return null;
}

return (
<form onSubmit={ submitForm }>
<StoreErrorNotices
Expand All @@ -105,7 +119,7 @@ export default function SetupForm( { onCompleteSetup } ) {
<PublicationSelect />
</div>
<PublicationOnboardingStateNotice />
<Link external href={ serviceURL }>
<Link external href={ serviceURL } onClick={ handleLinkClick }>
{ __( 'Create new publication', 'google-site-kit' ) }
</Link>
<div className="googlesitekit-setup-module__action">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe( 'SetupForm', () => {

expect( getByText( 'Publication' ) ).toBeInTheDocument();
expect(
getByRole( 'link', { name: /create new publication/i } )
getByRole( 'button', { name: /create new publication/i } )
).toBeInTheDocument();
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { CORE_FORMS } from '../../../../googlesitekit/datastore/forms/constants'
import {
MODULES_READER_REVENUE_MANAGER,
READER_REVENUE_MANAGER_SETUP_FORM,
RESET_PUBLICATIONS,
SHOW_PUBLICATION_CREATE,
} from '../../datastore/constants';
import { useDispatch, useSelect } from 'googlesitekit-data';
Expand All @@ -59,23 +60,29 @@ export default function SetupMain( { finishSetup = () => {} } ) {
SHOW_PUBLICATION_CREATE
)
);
const publicationID = useSelect( ( select ) =>
select( MODULES_READER_REVENUE_MANAGER ).getPublicationID()
const shouldResetPublications = useSelect( ( select ) =>
select( CORE_FORMS ).getValue(
READER_REVENUE_MANAGER_SETUP_FORM,
RESET_PUBLICATIONS
)
);

const { setValues } = useDispatch( CORE_FORMS );
const { resetPublications, submitChanges } = useDispatch(
MODULES_READER_REVENUE_MANAGER
);

const reset = useCallback( () => {
// Do not reset if the publication ID is already set.
if ( publicationID !== '' ) {
const reset = useCallback( async () => {
if ( ! shouldResetPublications ) {
return;
}

await setValues( READER_REVENUE_MANAGER_SETUP_FORM, {
[ RESET_PUBLICATIONS ]: false,
} );

resetPublications();
}, [ publicationID, resetPublications ] );
}, [ resetPublications, setValues, shouldResetPublications ] );

// Reset publication data when user re-focuses window.
useRefocus( reset, 15000 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ exports[`SetupForm should render the form correctly 1`] = `
</div>
</div>
</div>
<a
aria-label="Create new publication (opens in a new tab)"
<button
class="googlesitekit-cta-link"
href=""
rel="noopener noreferrer"
target="_blank"
>
<span
class="googlesitekit-cta-link__contents"
Expand All @@ -70,7 +66,7 @@ exports[`SetupForm should render the form correctly 1`] = `
>
<svg />
</span>
</a>
</button>
<div
class="googlesitekit-setup-module__action"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export const READER_REVENUE_MANAGER_SETUP_FORM =
'readerRevenueManagerSetupForm';

export const SHOW_PUBLICATION_CREATE = 'showPublicationCreate';

export const RESET_PUBLICATIONS = 'resetPublications';
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,21 @@ const baseActions = {
* Resets the publications data in the store.
*
* @since n.e.x.t
*
* @return {Object} The dispatched action results.
*/
*resetPublications() {
const registry = yield commonActions.getRegistry();

yield errorStoreActions.clearErrors( 'getPublications' );

yield commonActions.await(
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.invalidateResolutionForStoreSelector( 'getPublications' )
);

yield {
type: 'RESET_PUBLICATIONS',
};

yield errorStoreActions.clearErrors( 'getPublications' );

return registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.invalidateResolutionForStoreSelector( 'getPublications' );
},

/**
Expand Down

0 comments on commit 5d691d3

Please sign in to comment.