From 8b2e935767f6da62843c5bac1c0e3ed75f31d06f Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Wed, 31 Jul 2024 20:16:25 -0600 Subject: [PATCH 1/2] Set default titles for modules when save course from tour --- assets/admin/tour/course-tour/steps.js | 38 +++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/assets/admin/tour/course-tour/steps.js b/assets/admin/tour/course-tour/steps.js index bc8b567a29..6aa2447b71 100644 --- a/assets/admin/tour/course-tour/steps.js +++ b/assets/admin/tour/course-tour/steps.js @@ -13,6 +13,7 @@ import { store as editorStore } from '@wordpress/editor'; * Internal dependencies */ import { TourStep } from '../types'; +import { setBlockMeta } from '../../../shared/blocks/block-metadata'; import { getFirstBlockByName } from '../../../blocks/course-outline/data'; import { highlightElementsWithBorders, @@ -86,6 +87,40 @@ async function ensureLessonBlocksIsInEditor() { insertLessonBlock( 'Lesson 1' ); } +/** + * Check all modules have titles, if not, assign a default title. + */ +function ensureModulesHaveTitles() { + const blocks = getCourseOutlineBlock().innerBlocks; + const modules = blocks.filter( + ( block ) => block.name === 'sensei-lms/course-outline-module' + ); + const moduleTitles = modules + .map( ( block ) => { + if ( block.name !== 'sensei-lms/course-outline-module' ) { + return null; + } + const title = block.attributes?.title?.trim(); + return title || null; + } ) + .filter( ( value ) => !! value ); + + if ( modules.length > 0 ) { + let i = 0; + modules.forEach( ( module ) => { + let title = module.attributes?.title?.trim(); + if ( title !== '' ) { + return; + } + do { + title = __( 'Module', 'sensei-lms' ) + ' ' + ++i; + } while ( moduleTitles.includes( title ) ); + module.attributes.title = title; + setBlockMeta( module.clientId, module.attributes ); + } ); + } +} + /** * Returns the tour steps for the Course Outline block. * @@ -209,7 +244,7 @@ function getTourSteps() { heading: __( 'Adding a module', 'sensei-lms' ), descriptions: { desktop: __( - 'A module is a container for a group of related lessons in a course. Click + to open the inserter. Then click the Module option.', + 'A module is a container for a group of related lessons in a course. Click + to open the inserter. Then click the Module option and give it a name.', 'sensei-lms' ), mobile: null, @@ -512,6 +547,7 @@ function getTourSteps() { ); if ( ! savedLesson ) { + ensureModulesHaveTitles(); const { savePost } = dispatch( editorStore ); savePost(); await waitForElement( savedlessonSelector, 15 ); From 4549b26373a31b83fb9e7fed35a22a3599250c6a Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Wed, 31 Jul 2024 20:25:17 -0600 Subject: [PATCH 2/2] Add changelog entry --- changelog/fix-handle-empty-module-on-save-frontend | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/fix-handle-empty-module-on-save-frontend diff --git a/changelog/fix-handle-empty-module-on-save-frontend b/changelog/fix-handle-empty-module-on-save-frontend new file mode 100644 index 0000000000..8a279f96bd --- /dev/null +++ b/changelog/fix-handle-empty-module-on-save-frontend @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Set default names for modules without titles when save course in the course tour