Skip to content

Commit

Permalink
Add RRM settings validations in validateCanSubmitChanges.
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-t committed Jan 13, 2025
1 parent c027dc8 commit fa54013
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions assets/js/modules/reader-revenue-manager/datastore/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { createStrictSelect } from '../../../googlesitekit/data/utils';
import {
isValidPublicationID,
isValidOnboardingState,
isValidSnippetMode,
} from '../utils/validation';

// Invariant error messages.
Expand All @@ -42,6 +43,20 @@ export const INVARIANT_INVALID_PUBLICATION_ID =
export const INVARIANT_INVALID_PUBLICATION_ONBOARDING_STATE =
'a valid publication onboarding state is required';

export const INVARIANT_INVALID_SNIPPET_MODE =
'a valid snippet mode is required';

export const INVARIANT_INVALID_POST_TYPES =
'a valid post types array is required';

export const INVARIANT_INVALID_PRODUCT_ID = 'a valid product ID is required';

export const INVARIANT_INVALID_PRODUCT_IDS =
'a valid product IDs array is required';

export const INVARIANT_INVALID_PAYMENT_OPTION =
'a valid payment option is required';

export function validateCanSubmitChanges( select ) {
const strictSelect = createStrictSelect( select );
// Strict select will cause all selector functions to throw an error
Expand All @@ -52,13 +67,24 @@ export function validateCanSubmitChanges( select ) {
isDoingSubmitChanges,
getPublicationID,
getPublicationOnboardingState,
getSnippetMode,
getPostTypes,
getProductID,
getProductIDs,
getPaymentOption,
} = strictSelect( MODULES_READER_REVENUE_MANAGER );

invariant( ! isDoingSubmitChanges(), INVARIANT_DOING_SUBMIT_CHANGES );
invariant( haveSettingsChanged(), INVARIANT_SETTINGS_NOT_CHANGED );

const publicationID = getPublicationID();
const onboardingState = getPublicationOnboardingState();
const snippetMode = getSnippetMode();
const postTypes = getPostTypes();
const productID = getProductID();
const productIDs = getProductIDs();
const paymentOption = getPaymentOption();

invariant(
isValidPublicationID( publicationID ),
INVARIANT_INVALID_PUBLICATION_ID
Expand All @@ -68,4 +94,30 @@ export function validateCanSubmitChanges( select ) {
isValidOnboardingState( onboardingState ),
INVARIANT_INVALID_PUBLICATION_ONBOARDING_STATE
);

invariant(
isValidSnippetMode( snippetMode ),
INVARIANT_INVALID_SNIPPET_MODE
);

invariant(
Array.isArray( postTypes ) &&
postTypes.every( ( item ) => typeof item === 'string' ),
INVARIANT_INVALID_POST_TYPES
);

invariant( typeof productID === 'string', INVARIANT_INVALID_PRODUCT_ID );

invariant(
Array.isArray( productIDs ) &&
productIDs.every( ( item ) => typeof item === 'string' ),
INVARIANT_INVALID_PRODUCT_IDS
);

invariant(
typeof paymentOption === 'string',
INVARIANT_INVALID_PAYMENT_OPTION
);

return true;
}

0 comments on commit fa54013

Please sign in to comment.