gocodebox/lifterlms

View on GitHub
src/blocks/course-syllabus/index.jsx

Summary

Maintainability
A
1 hr
Test Coverage
// WordPress dependencies.
import { registerBlockType } from '@wordpress/blocks';
import {
    PanelBody,
    Disabled,
    Spinner,
} from '@wordpress/components';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import ServerSideRender from '@wordpress/server-side-render';
import { useMemo } from '@wordpress/element';

// Internal dependencies.
import blockJson from './block.json';
import Icon from './icon.jsx';
import { PostSelect, usePostOptions, useLlmsPostType } from '../../../packages/components/src/post-select';

const Edit = ( props ) => {
    const { attributes, setAttributes } = props;
    const blockProps = useBlockProps();
    const isLlmsPostType = useLlmsPostType();
    const courseOptions = usePostOptions();

    const memoizedServerSideRender = useMemo( () => {
        return <ServerSideRender
            block={ blockJson.name }
            attributes={ {
                course_id: attributes.course_id ?? courseOptions?.[ 0 ]?.value,
            } }
            LoadingResponsePlaceholder={ () =>
                <Spinner />
            }
            ErrorResponsePlaceholder={ () =>
                <p className={ 'llms-block-error' }>{ __( 'Error loading content. Please check block settings are valid. This block will not be displayed.', 'lifterlms' ) }</p>
            }
            EmptyResponsePlaceholder={ () =>
                <p className={ 'llms-block-empty' }>{ __( 'No syllabus found for this course. This block will not be displayed.', 'lifterlms' ) }</p>
            }
        />;
    }, [ attributes ] );

    if ( ! attributes.course_id && ! isLlmsPostType ) {
        setAttributes( {
            course_id: courseOptions?.[ 0 ]?.value,
        } );
    }

    return <>
        <InspectorControls>
            <PanelBody
                title={ __( 'Course Syllabus Settings', 'lifterlms' ) }
            >
                <PostSelect
                    attributes={ attributes }
                    setAttributes={ setAttributes }
                />
            </PanelBody>
        </InspectorControls>
        <div { ...blockProps }>
            <Disabled>
                { memoizedServerSideRender }
            </Disabled>
        </div>
    </>;
};

registerBlockType( blockJson, {
    icon: Icon,
    edit: Edit,
} );