Skip to content

Commit

Permalink
Handle empty module titles on saving the course from the tour (#7652)
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin authored Aug 2, 2024
1 parent 4272ec4 commit 80d3026
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
38 changes: 37 additions & 1 deletion assets/admin/tour/course-tour/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -512,6 +547,7 @@ function getTourSteps() {
);

if ( ! savedLesson ) {
ensureModulesHaveTitles();
const { savePost } = dispatch( editorStore );
savePost();
await waitForElement( savedlessonSelector, 15 );
Expand Down
4 changes: 4 additions & 0 deletions changelog/fix-handle-empty-module-on-save-frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Set default names for modules without titles when save course in the course tour

0 comments on commit 80d3026

Please sign in to comment.