Skip to content

Commit

Permalink
Only show notice after sensei pattern inserted
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran92 committed Dec 12, 2023
1 parent af681c6 commit ddb29ea
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assets/admin/editor-wizard/steps/patterns-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const PatternsStep = ( { title, replaces, onCompletion } ) => {
? replacePlaceholders( blocks, replaces )
: blocks;

resetEditorBlocks( newBlocks );
resetEditorBlocks( newBlocks, { patternName: name } );
onCompletion();

//Auto select a template if the pattern specifies an available one.
Expand Down
78 changes: 74 additions & 4 deletions assets/js/admin/first-course-creation-notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { select, subscribe, dispatch } from '@wordpress/data';
import { select, subscribe, dispatch, use } from '@wordpress/data';
import domReady from '@wordpress/dom-ready';
import { __ } from '@wordpress/i18n';
import { find as findObject } from 'lodash';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -50,6 +51,75 @@ export const hasPublishedLessonInOutline = ( blocks ) => {
} );
};

// Detect when a user selects a Sensei pattern.
let selectedSenseiPattern = false;

const rewrittenActions = {};
const originalActions = {};

const checkIfAnySenseiPatternGotInserted = ( ...args ) => {
const meta = findObject( args, ( item ) => item?.patternName );
let patternName = meta?.patternName;
if ( patternName && patternName.includes( 'sensei' ) ) {
selectedSenseiPattern = true;
}
};

const REDUX_ACTION_OVERRIDERS = {
'core/block-editor': {
insertBlocks: checkIfAnySenseiPatternGotInserted,
},
'core/editor': {
resetEditorBlocks: checkIfAnySenseiPatternGotInserted,
},
};

use( ( registry ) => ( {
dispatch: ( namespace ) => {
const namespaceName =
typeof namespace === 'object' ? namespace.name : namespace;
const actions = { ...registry.dispatch( namespaceName ) };
const overriderActions = REDUX_ACTION_OVERRIDERS[ namespaceName ];

if ( overriderActions ) {
Object.keys( overriderActions ).forEach( ( actionName ) => {
const originalAction = actions[ actionName ];
const newAction = overriderActions[ actionName ];

if ( ! rewrittenActions[ namespaceName ] ) {
rewrittenActions[ namespaceName ] = {};
}
if ( ! originalActions[ namespaceName ] ) {
originalActions[ namespaceName ] = {};
}

if (
! originalActions[ namespaceName ][ actionName ] ||
originalActions[ namespaceName ][ actionName ] !==
originalAction
) {
originalActions[ namespaceName ][
actionName
] = originalAction;
rewrittenActions[ namespaceName ][ actionName ] = (
...args
) => {
try {
newAction( ...args );
} catch ( err ) {}
return originalAction( ...args );
};
}

actions[ actionName ] =
rewrittenActions[ namespaceName ][ actionName ];
} );
}

return actions;
},
} ) );

export const handleFirstCourseCreationHelperNotice = () => {
const { createInfoNotice, removeNotice } = dispatch( 'core/notices' );
const userId = select( 'core' ).getCurrentUser()?.id;
Expand All @@ -69,9 +139,8 @@ export const handleFirstCourseCreationHelperNotice = () => {
);

subscribe( () => {
// If the notice has been created, and the course outline block has been created, and the notice hasn't been removed, and there are published lessons in the outline, remove the notice.
// Function calls are added later to short circuit the logic for performance.
if (
selectedSenseiPattern &&
noticeCreated &&
! noticeRemoved &&
hasOutlineBlock() &&
Expand All @@ -82,8 +151,9 @@ export const handleFirstCourseCreationHelperNotice = () => {
removeNotice( noticeId );
}

// If the notice hasn't been created, and notice hasn't been dismissed, and either the course outline block hasn't been created OR there are no published lessons in the outline, create the notice.
// If the user selects a Sensei pattern, and the notice hasn't been created, and notice hasn't been dismissed, and either the course outline block hasn't been created OR there are no published lessons in the outline, create the notice.
if (
selectedSenseiPattern &&
! noticeCreated &&
! isFirstCourseNoticeDismissed &&
! (
Expand Down

0 comments on commit ddb29ea

Please sign in to comment.