11import { useCallback , useState } from 'react' ;
2- import PropTypes from 'prop-types' ;
32import { useDispatch , useSelector } from 'react-redux' ;
43import { getConfig } from '@edx/frontend-platform' ;
54import { useIntl , FormattedMessage } from '@edx/frontend-platform/i18n' ;
@@ -21,6 +20,41 @@ import { useEventListener } from '../../generic/hooks';
2120import VideoSelectorPage from '../../editors/VideoSelectorPage' ;
2221import EditorPage from '../../editors/EditorPage' ;
2322import { fetchCourseSectionVerticalData } from '../data/thunk' ;
23+ import { SelectedComponent } from '../../library-authoring' ;
24+
25+ type ComponentTemplateData = {
26+ displayName : string ,
27+ category ?: string ,
28+ type : string ,
29+ beta ?: boolean ,
30+ templates : Array < {
31+ boilerplateName ?: string ,
32+ category ?: string ,
33+ displayName : string ,
34+ supportLevel ?: string | boolean ,
35+ } > ,
36+ supportLegend : {
37+ allowUnsupportedXblocks ?: boolean ,
38+ documentationLabel ?: string ,
39+ showLegend ?: boolean ,
40+ } ,
41+ } ;
42+
43+ export interface AddComponentProps {
44+ isSplitTestType ?: boolean ,
45+ isUnitVerticalType ?: boolean ,
46+ parentLocator : string ,
47+ handleCreateNewCourseXBlock : (
48+ args : object ,
49+ callback ?: ( args : { courseKey : string , locator : string } ) => void
50+ ) => void ,
51+ isProblemBankType ?: boolean ,
52+ addComponentTemplateData ?: {
53+ blockId : string ,
54+ parentLocator ?: string ,
55+ model : ComponentTemplateData ,
56+ } ,
57+ }
2458
2559const AddComponent = ( {
2660 parentLocator,
@@ -29,24 +63,24 @@ const AddComponent = ({
2963 isProblemBankType,
3064 addComponentTemplateData,
3165 handleCreateNewCourseXBlock,
32- } ) => {
66+ } : AddComponentProps ) => {
3367 const intl = useIntl ( ) ;
3468 const dispatch = useDispatch ( ) ;
3569
3670 const [ isOpenAdvanced , openAdvanced , closeAdvanced ] = useToggle ( false ) ;
3771 const [ isOpenHtml , openHtml , closeHtml ] = useToggle ( false ) ;
3872 const [ isOpenOpenAssessment , openOpenAssessment , closeOpenAssessment ] = useToggle ( false ) ;
3973 const { componentTemplates = { } } = useSelector ( getCourseSectionVertical ) ;
40- const blockId = addComponentTemplateData . parentLocator || parentLocator ;
74+ const blockId = addComponentTemplateData ? .parentLocator || parentLocator ;
4175 const [ isAddLibraryContentModalOpen , showAddLibraryContentModal , closeAddLibraryContentModal ] = useToggle ( ) ;
4276 const [ isVideoSelectorModalOpen , showVideoSelectorModal , closeVideoSelectorModal ] = useToggle ( ) ;
4377 const [ isXBlockEditorModalOpen , showXBlockEditorModal , closeXBlockEditorModal ] = useToggle ( ) ;
4478
45- const [ blockType , setBlockType ] = useState ( null ) ;
46- const [ courseId , setCourseId ] = useState ( null ) ;
47- const [ newBlockId , setNewBlockId ] = useState ( null ) ;
79+ const [ blockType , setBlockType ] = useState < string | null > ( null ) ;
80+ const [ courseId , setCourseId ] = useState < string | null > ( null ) ;
81+ const [ newBlockId , setNewBlockId ] = useState < string | null > ( null ) ;
4882 const [ isSelectLibraryContentModalOpen , showSelectLibraryContentModal , closeSelectLibraryContentModal ] = useToggle ( ) ;
49- const [ selectedComponents , setSelectedComponents ] = useState ( [ ] ) ;
83+ const [ selectedComponents , setSelectedComponents ] = useState < SelectedComponent [ ] > ( [ ] ) ;
5084 const [ usageId , setUsageId ] = useState ( null ) ;
5185 const { sendMessageToIframe } = useIframe ( ) ;
5286 const { useVideoGalleryFlow } = useWaffleFlags ( courseId ?? undefined ) ;
@@ -85,7 +119,7 @@ const AddComponent = ({
85119 dispatch ( fetchCourseSectionVerticalData ( blockId , sequenceId ) ) ;
86120 } , [ closeXBlockEditorModal , closeVideoSelectorModal , sendMessageToIframe , blockId , sequenceId ] ) ;
87121
88- const handleLibraryV2Selection = useCallback ( ( selection ) => {
122+ const handleLibraryV2Selection = useCallback ( ( selection : SelectedComponent ) => {
89123 handleCreateNewCourseXBlock ( {
90124 type : COMPONENT_TYPES . libraryV2 ,
91125 category : selection . blockType ,
@@ -95,7 +129,7 @@ const AddComponent = ({
95129 closeAddLibraryContentModal ( ) ;
96130 } , [ usageId ] ) ;
97131
98- const handleCreateNewXBlock = ( type , moduleName ) => {
132+ const handleCreateNewXBlock = ( type : string , moduleName ?: string ) => {
99133 switch ( type ) {
100134 case COMPONENT_TYPES . discussion :
101135 case COMPONENT_TYPES . dragAndDrop :
@@ -164,9 +198,9 @@ const AddComponent = ({
164198 < >
165199 < h5 className = "h3 mb-4 text-center" > { intl . formatMessage ( messages . title ) } </ h5 >
166200 < ul className = "new-component-type list-unstyled m-0 d-flex flex-wrap justify-content-center" >
167- { componentTemplates . map ( ( component ) => {
201+ { componentTemplates . map ( ( component : ComponentTemplateData ) => {
168202 const { type, displayName, beta } = component ;
169- let modalParams ;
203+ let modalParams : { open : ( ) => void , close : ( ) => void , isOpen : boolean } ;
170204
171205 if ( ! component . templates . length ) {
172206 return null ;
@@ -269,7 +303,7 @@ const AddComponent = ({
269303 />
270304 </ div >
271305 </ StandardModal >
272- { isXBlockEditorModalOpen && (
306+ { isXBlockEditorModalOpen && courseId && blockType && newBlockId && (
273307 < div className = "editor-page" >
274308 < EditorPage
275309 courseId = { courseId }
@@ -289,32 +323,4 @@ const AddComponent = ({
289323 return null ;
290324} ;
291325
292- AddComponent . propTypes = {
293- isSplitTestType : PropTypes . bool . isRequired ,
294- isUnitVerticalType : PropTypes . bool . isRequired ,
295- parentLocator : PropTypes . string . isRequired ,
296- handleCreateNewCourseXBlock : PropTypes . func . isRequired ,
297- addComponentTemplateData : {
298- blockId : PropTypes . string . isRequired ,
299- model : PropTypes . shape ( {
300- displayName : PropTypes . string . isRequired ,
301- category : PropTypes . string ,
302- type : PropTypes . string . isRequired ,
303- templates : PropTypes . arrayOf (
304- PropTypes . shape ( {
305- boilerplateName : PropTypes . string ,
306- category : PropTypes . string ,
307- displayName : PropTypes . string . isRequired ,
308- supportLevel : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . bool ] ) ,
309- } ) ,
310- ) ,
311- supportLegend : PropTypes . shape ( {
312- allowUnsupportedXblocks : PropTypes . bool ,
313- documentationLabel : PropTypes . string ,
314- showLegend : PropTypes . bool ,
315- } ) ,
316- } ) ,
317- } ,
318- } ;
319-
320326export default AddComponent ;
0 commit comments