44import { ArrowBack } from '@openedx/paragon/icons' ;
55import { useCallback , useMemo , useState } from 'react' ;
66import { ContainerType } from '@src/generic/key-utils' ;
7+ import ErrorAlert from '@src/generic/alert-error' ;
78import { LoadingSpinner } from '@src/generic/Loading' ;
8- import { useContainerChildren } from '@src/library-authoring/data/apiHooks' ;
9+ import { useContainer , useContainerChildren } from '@src/library-authoring/data/apiHooks' ;
910import { useIntl } from '@edx/frontend-platform/i18n' ;
1011import ChildrenPreview from './ChildrenPreview' ;
1112import ContainerRow from './ContainerRow' ;
@@ -15,7 +16,6 @@ import { diffPreviewContainerChildren, isRowClickable } from './utils';
1516import messages from './messages' ;
1617
1718interface ContainerInfoProps {
18- title : string ;
1919 upstreamBlockId : string ;
2020 downstreamBlockId : string ;
2121}
@@ -30,19 +30,24 @@ interface Props extends ContainerInfoProps {
3030 * Actual implementation of the displaying diff between children of containers.
3131 */
3232const CompareContainersWidgetInner = ( {
33- title,
3433 upstreamBlockId,
3534 downstreamBlockId,
3635 parent,
3736 onRowClick,
3837 onBackBtnClick,
3938} : Props ) => {
4039 const intl = useIntl ( ) ;
41- const { data, isPending } = useCourseContainerChildren ( downstreamBlockId ) ;
40+ const { data, isError , error } = useCourseContainerChildren ( downstreamBlockId ) ;
4241 const {
4342 data : libData ,
44- isPending : libPending ,
43+ isError : isLibError ,
44+ error : libError ,
4545 } = useContainerChildren ( upstreamBlockId , true ) ;
46+ const {
47+ data : containerData ,
48+ isError : isContainerTitleError ,
49+ error : containerTitleError ,
50+ } = useContainer ( upstreamBlockId ) ;
4651
4752 const result = useMemo ( ( ) => {
4853 if ( ! data || ! libData ) {
@@ -52,7 +57,7 @@ const CompareContainersWidgetInner = ({
5257 } , [ data , libData ] ) ;
5358
5459 const renderBeforeChildren = useCallback ( ( ) => {
55- if ( isPending ) {
60+ if ( ! result [ 0 ] ) {
5661 return < div className = "m-auto" > < LoadingSpinner /> </ div > ;
5762 }
5863
@@ -67,10 +72,10 @@ const CompareContainersWidgetInner = ({
6772 onClick = { ( ) => onRowClick ( child ) }
6873 />
6974 ) ) ;
70- } , [ isPending , result ] ) ;
75+ } , [ result ] ) ;
7176
7277 const renderAfterChildren = useCallback ( ( ) => {
73- if ( libPending || isPending ) {
78+ if ( ! result [ 1 ] ) {
7479 return < div className = "m-auto" > < LoadingSpinner /> </ div > ;
7580 }
7681
@@ -84,9 +89,13 @@ const CompareContainersWidgetInner = ({
8489 onClick = { ( ) => onRowClick ( child ) }
8590 />
8691 ) ) ;
87- } , [ libPending , isPending , result ] ) ;
92+ } , [ result ] ) ;
93+
94+ const getTitleComponent = useCallback ( ( title ?: string | null ) => {
95+ if ( ! title ) {
96+ return < div className = "m-auto" > < LoadingSpinner /> </ div > ;
97+ }
8898
89- const getTitleComponent = ( ) => {
9099 if ( parent . length === 0 ) {
91100 return title ;
92101 }
@@ -95,6 +104,7 @@ const CompareContainersWidgetInner = ({
95104 ariaLabel = { intl . formatMessage ( messages . breadcrumbAriaLabel ) }
96105 links = { [
97106 {
107+ // This raises failed prop-type error as label expects a string but it works without any issues
98108 label : < Stack direction = "horizontal" gap = { 1 } > < Icon size = "xs" src = { ArrowBack } /> Back</ Stack > ,
99109 onClick : onBackBtnClick ,
100110 variant : 'link' ,
@@ -110,20 +120,24 @@ const CompareContainersWidgetInner = ({
110120 linkAs = { Button }
111121 />
112122 ) ;
113- } ;
123+ } , [ parent ] ) ;
124+
125+ if ( isError || isLibError || isContainerTitleError ) {
126+ return < ErrorAlert error = { error || libError || containerTitleError } /> ;
127+ }
114128
115129 return (
116130 < div className = "row" >
117131 < div className = "col col-6 p-1" >
118132 < Card className = "p-4" >
119- < ChildrenPreview title = { getTitleComponent ( ) } side = "Before" >
133+ < ChildrenPreview title = { getTitleComponent ( data ?. displayName ) } side = "Before" >
120134 { renderBeforeChildren ( ) }
121135 </ ChildrenPreview >
122136 </ Card >
123137 </ div >
124138 < div className = "col col-6 p-1" >
125139 < Card className = "p-4" >
126- < ChildrenPreview title = { getTitleComponent ( ) } side = "After" >
140+ < ChildrenPreview title = { getTitleComponent ( containerData ?. publishedDisplayName ) } side = "After" >
127141 { renderAfterChildren ( ) }
128142 </ ChildrenPreview >
129143 </ Card >
@@ -137,11 +151,10 @@ const CompareContainersWidgetInner = ({
137151 * and allows the user to select the container to view. This is a wrapper component that maintains current
138152 * source state. Actual implementation of the diff view is done by CompareContainersWidgetInner.
139153 */
140- export const CompareContainersWidget = ( { title , upstreamBlockId, downstreamBlockId } : ContainerInfoProps ) => {
154+ export const CompareContainersWidget = ( { upstreamBlockId, downstreamBlockId } : ContainerInfoProps ) => {
141155 const [ currentContainerState , setCurrentContainerState ] = useState < ContainerInfoProps & {
142156 parent : ContainerInfoProps [ ] ;
143157 } > ( {
144- title,
145158 upstreamBlockId,
146159 downstreamBlockId,
147160 parent : [ ] ,
@@ -153,11 +166,9 @@ export const CompareContainersWidget = ({ title, upstreamBlockId, downstreamBloc
153166 }
154167
155168 setCurrentContainerState ( ( prev ) => ( {
156- title : row . name ,
157169 upstreamBlockId : row . id ! ,
158170 downstreamBlockId : row . downstreamId ! ,
159171 parent : [ ...prev . parent , {
160- title : prev . title ,
161172 upstreamBlockId : prev . upstreamBlockId ,
162173 downstreamBlockId : prev . downstreamBlockId ,
163174 } ] ,
@@ -172,7 +183,6 @@ export const CompareContainersWidget = ({ title, upstreamBlockId, downstreamBloc
172183 }
173184 const prevParent = prev . parent [ prev . parent . length - 1 ] ;
174185 return {
175- title : prevParent ! . title ,
176186 upstreamBlockId : prevParent ! . upstreamBlockId ,
177187 downstreamBlockId : prevParent ! . downstreamBlockId ,
178188 parent : prev . parent . slice ( 0 , - 1 ) ,
@@ -182,7 +192,6 @@ export const CompareContainersWidget = ({ title, upstreamBlockId, downstreamBloc
182192
183193 return (
184194 < CompareContainersWidgetInner
185- title = { currentContainerState . title }
186195 upstreamBlockId = { currentContainerState . upstreamBlockId }
187196 downstreamBlockId = { currentContainerState . downstreamBlockId }
188197 parent = { currentContainerState . parent }
0 commit comments