Skip to content

Commit

Permalink
Show notice when there is no published lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran92 committed Dec 12, 2023
1 parent 12bd25d commit 4bc987c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
14 changes: 5 additions & 9 deletions assets/js/admin/first-course-creation-notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@ export const handleCourseOutlineBlockIncomplete = async () => {
// If the function isn't globally available, the link button doesn't find the reference.
window.handleCourseOutlineBlockIncomplete = handleCourseOutlineBlockIncomplete;

export const hasPublishedLessonInOutline = ( blocks ) => {
export const hasLessonInOutline = ( blocks ) => {
return blocks.some( ( block ) => {
if (
block.name === 'sensei-lms/course-outline-lesson' &&
! block.attributes.draft
) {
if ( block.name === 'sensei-lms/course-outline-lesson' ) {
return true;
}

if ( block.innerBlocks?.length ) {
return hasPublishedLessonInOutline( block.innerBlocks );
return hasLessonInOutline( block.innerBlocks );
}

return false;
Expand Down Expand Up @@ -146,7 +143,7 @@ export const handleFirstCourseCreationHelperNotice = () => {
noticeCreated &&
! noticeRemoved &&
hasOutlineBlock() &&
hasPublishedLessonInOutline( [ hasOutlineBlock() ] )
hasLessonInOutline( [ hasOutlineBlock() ] )
) {
noticeRemoved = true;
noticeCreated = false;
Expand All @@ -159,8 +156,7 @@ export const handleFirstCourseCreationHelperNotice = () => {
! noticeCreated &&
! isFirstCourseNoticeDismissed &&
! (
hasOutlineBlock() &&
hasPublishedLessonInOutline( [ hasOutlineBlock() ] )
hasOutlineBlock() && hasLessonInOutline( [ hasOutlineBlock() ] )
)
) {
noticeCreated = true;
Expand Down
13 changes: 6 additions & 7 deletions assets/js/admin/first-course-creation-notice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
hasOutlineBlock,
handleCourseOutlineBlockIncomplete,
handleFirstCourseCreationHelperNotice,
hasPublishedLessonInOutline,
hasLessonInOutline,
} from './first-course-creation-notice';
import { getFirstBlockByName } from '../../blocks/course-outline/data';

Expand Down Expand Up @@ -45,28 +45,27 @@ jest.mock( './first-course-creation-notice' );
jest.mock( '../../blocks/course-outline/data' );

describe( 'hasPublishedLessonInOutline', () => {
it( 'should return true when there is a published lesson in the outline', () => {
it( 'should return true when there is a lesson in the outline', () => {
const blocks = [
{
name: 'sensei-lms/course-outline-lesson',
attributes: { draft: false },
},
];

hasPublishedLessonInOutline.mockImplementation(
hasLessonInOutline.mockImplementation(
jest.requireActual( './first-course-creation-notice' )
.hasPublishedLessonInOutline
);

const result = hasPublishedLessonInOutline( blocks );
const result = hasLessonInOutline( blocks );

expect( result ).toBe( true );
} );

it( 'should return false when there is no published lesson in the outline', () => {
it( 'should return false when there is no lesson in the outline', () => {
const blocks = [ { name: 'some-other-block' } ];

const result = hasPublishedLessonInOutline( blocks );
const result = hasLessonInOutline( blocks );

expect( result ).toBe( false );
} );
Expand Down

0 comments on commit 4bc987c

Please sign in to comment.