Skip to content

Commit

Permalink
Improve the syncPublicationOnboardingState action.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfmohit authored and kuasha420 committed Aug 7, 2024
1 parent 0c22831 commit 31fc275
Showing 1 changed file with 29 additions and 37 deletions.
66 changes: 29 additions & 37 deletions assets/js/modules/reader-revenue-manager/datastore/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const baseInitialState = {

const baseActions = {
/**
* Syncronizes the onboarding state of the publication with the API.
* Synchronizes the onboarding state of the publication with the API.
* Updates the settings on the server.
*
* @since 1.132.0
Expand All @@ -68,35 +68,38 @@ const baseActions = {
*/
*syncPublicationOnboardingState() {
const registry = yield commonActions.getRegistry();
const connected = yield commonActions.await(
registry
.resolveSelect( CORE_MODULES )
.isModuleConnected( MODULE_SLUG )
);

// If the module is not connected, do not attempt to sync the onboarding state.
if ( ! connected ) {
const hasPublicationIDChanged = registry
.select( MODULES_READER_REVENUE_MANAGER )
.hasSettingChanged( 'publicationID' );

// Do not attempt to sync the onboarding state if the publication ID
// in state is not the "saved" publication ID.
if ( hasPublicationIDChanged ) {
return;
}

yield commonActions.await(
const settings = yield commonActions.await(
registry
.resolveSelect( MODULES_READER_REVENUE_MANAGER )
.getSettings()
);

const publicationID = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationID();
const {
publicationID,
publicationOnboardingState: currentOnboardingState,
} = settings;

// If there is no publication ID, do not attempt to sync the onboarding state.
if ( publicationID === undefined ) {
return;
}

const publications = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublications();
const publications = yield commonActions.await(
registry
.resolveSelect( MODULES_READER_REVENUE_MANAGER )
.getPublications()
);

// If there are no publications, do not attempt to sync the onboarding state.
if ( ! publications ) {
Expand All @@ -113,33 +116,22 @@ const baseActions = {
return;
}

const { onboardingState } = publication;
const currentOnboardingState = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingState();

const settings = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getSettings();
const { onboardingState: newOnboardingState } = publication;

if ( onboardingState !== currentOnboardingState ) {
settings.publicationOnboardingState = onboardingState;
}

// eslint-disable-next-line sitekit/no-direct-date
settings.publicationOnboardingStateLastSyncedAtMs = Date.now();

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );
registry.dispatch( MODULES_READER_REVENUE_MANAGER ).setSettings( {
publicationOnboardingState: newOnboardingState,
// The "last synced" value should reflect the real time this action
// was performed, so we don't use the reference date here.
// eslint-disable-next-line sitekit/no-direct-date
publicationOnboardingStateLastSyncedAtMs: Date.now(),
} );

// Save the settings to the API.
registry.dispatch( MODULES_READER_REVENUE_MANAGER ).saveSettings();

// If the onboarding state changes to complete, set the key in CORE_UI to trigger the notification.
if (
onboardingState !== currentOnboardingState &&
onboardingState ===
newOnboardingState !== currentOnboardingState &&
newOnboardingState ===
PUBLICATION_ONBOARDING_STATES.ONBOARDING_COMPLETE
) {
registry
Expand All @@ -152,7 +144,7 @@ const baseActions = {
},

/**
* Syncronizes the onboarding state of the publication based on the last synced time.
* Synchronizes the onboarding state of the publication based on the last synced time.
*
* @since n.e.x.t
*
Expand Down

0 comments on commit 31fc275

Please sign in to comment.