Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
26 changes: 26 additions & 0 deletions assets/js/googlesitekit/datastore/site/email-reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { stringifyObject } from '@/js/util';

const START_INVITING_USER = 'START_INVITING_USER';
const FINISH_INVITING_USER = 'FINISH_INVITING_USER';
const RESET_ELIGIBLE_SUBSCRIBERS = 'RESET_ELIGIBLE_SUBSCRIBERS';
const DEFAULT_ELIGIBLE_SUBSCRIBERS_ARGS = {
page: 1,
search: '',
Expand Down Expand Up @@ -301,6 +302,26 @@ const baseActions = {
payload: { userID },
};
},

/**
* Resets the eligible subscribers cache.
*
* @since n.e.x.t
*
* @return {Object} Redux-style action.
*/
*resetEligibleSubscribers() {
const { dispatch } = yield commonActions.getRegistry();

yield {
type: RESET_ELIGIBLE_SUBSCRIBERS,
payload: {},
};

return dispatch( CORE_SITE ).invalidateResolutionForStoreSelector(
'getEligibleSubscribers'
);
},
};

export const baseReducer = createReducer( ( state, action ) => {
Expand All @@ -322,6 +343,11 @@ export const baseReducer = createReducer( ( state, action ) => {
state.emailReporting.invitingUsers[ payload.userID ] = false;
break;
}
case RESET_ELIGIBLE_SUBSCRIBERS: {
state.emailReporting.eligibleSubscribers =
baseInitialState.emailReporting.eligibleSubscribers;
break;
}

default:
break;
Expand Down
94 changes: 94 additions & 0 deletions assets/js/googlesitekit/datastore/site/email-reporting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,100 @@ describe( 'core/site Email Reporting', () => {
} ).toThrow( 'userID should be a positive integer.' );
} );
} );

describe( 'resetEligibleSubscribers', () => {
it( 'clears cached eligible subscribers', async () => {
fetchMock.get( eligibleSubscribersEndpointRegExp, {
body: createEligibleSubscribersResponse( [] ),
status: 200,
} );

registry
.dispatch( CORE_SITE )
.receiveGetEligibleSubscribers(
createEligibleSubscribersResponse( [] ),
{ page: 1, search: '' }
);
registry
.dispatch( CORE_SITE )
.receiveGetEligibleSubscribers(
createEligibleSubscribersResponse( [] ),
{ page: 1, search: 'editor' }
);

expect(
registry.select( CORE_SITE ).getEligibleSubscribers( {
search: '',
} )
).toBeDefined();
expect(
registry.select( CORE_SITE ).getEligibleSubscribers( {
search: 'editor',
} )
).toBeDefined();

await registry.dispatch( CORE_SITE ).resetEligibleSubscribers();

expect(
registry.stores[ CORE_SITE ].store.getState().emailReporting
.eligibleSubscribers
).toEqual( {} );
} );

it( 'invalidates resolver state so prior args can re-fetch', async () => {
provideUserInfo( registry, { id: 1 } );

fetchMock.getOnce( eligibleSubscribersEndpointRegExp, {
body: createEligibleSubscribersResponse( [
{
id: 2,
displayName: 'Eligible User',
email: 'eligible@example.com',
role: 'editor',
subscribed: false,
invited: false,
},
] ),
status: 200,
} );

registry
.select( CORE_SITE )
.getEligibleSubscribers( defaultEligibleSubscribersArgs );
await untilResolved(
registry,
CORE_SITE
).getEligibleSubscribers( defaultEligibleSubscribersArgs );

expect( fetchMock ).toHaveFetchedTimes( 1 );

await registry.dispatch( CORE_SITE ).resetEligibleSubscribers();

fetchMock.getOnce( eligibleSubscribersEndpointRegExp, {
body: createEligibleSubscribersResponse( [
{
id: 3,
displayName: 'Another Eligible User',
email: 'another@example.com',
role: 'author',
subscribed: false,
invited: false,
},
] ),
status: 200,
} );

registry
.select( CORE_SITE )
.getEligibleSubscribers( defaultEligibleSubscribersArgs );
await untilResolved(
registry,
CORE_SITE
).getEligibleSubscribers( defaultEligibleSubscribersArgs );

expect( fetchMock ).toHaveFetchedTimes( 2 );
} );
} );
} );

describe( 'selectors', () => {
Expand Down
10 changes: 10 additions & 0 deletions assets/js/googlesitekit/modules/datastore/sharing-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
createStrictSelect,
createValidationSelector,
} from '@/js/googlesitekit/data/utils';
import { CORE_SITE } from '@/js/googlesitekit/datastore/site/constants';

// Actions
const SET_SHARING_MANAGEMENT = 'SET_SHARING_MANAGEMENT';
Expand Down Expand Up @@ -196,6 +197,10 @@ const baseActions = {
}
}

if ( ! error ) {
registry.dispatch( CORE_SITE ).resetEligibleSubscribers();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
registry.dispatch( CORE_SITE ).resetEligibleSubscribers();
yield registry.dispatch( CORE_SITE ).resetEligibleSubscribers();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use yield for dispatch, we included the rule to prevent it:

 error  Only plain objects should be yielded from action generator functions. dispatch always returns a Promise  sitekit/no-yield-dispatch

}

yield {
type: FINISH_SUBMIT_SHARING_CHANGES,
payload: {},
Expand All @@ -222,6 +227,11 @@ const baseActions = {
const { response, error } =
yield fetchResetSharingSettingsStore.actions.fetchResetSharingSettings();

if ( ! error ) {
const registry = yield commonActions.getRegistry();
registry.dispatch( CORE_SITE ).resetEligibleSubscribers();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
registry.dispatch( CORE_SITE ).resetEligibleSubscribers();
yield registry.dispatch( CORE_SITE ).resetEligibleSubscribers();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}

yield {
type: FINISH_SUBMIT_SHARING_CHANGES,
payload: {},
Expand Down
Loading
Loading