Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty module titles on saving the course from the tour #7652

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
Loading