@@ -3,25 +3,28 @@ import { useSelector } from 'react-redux';
33import {
44 getLocale , injectIntl , intlShape , isRtl ,
55} from '@edx/frontend-platform/i18n' ;
6+ import PropTypes from 'prop-types' ;
67import { useModel } from '../../../generic/model-store' ;
78
89import CompleteDonutSegment from './CompleteDonutSegment' ;
910import IncompleteDonutSegment from './IncompleteDonutSegment' ;
1011import LockedDonutSegment from './LockedDonutSegment' ;
1112import messages from './messages' ;
1213
13- const CompletionDonutChart = ( { intl } ) => {
14+ const CompletionDonutChart = ( { intl, optional } ) => {
1415 const {
1516 courseId,
1617 } = useSelector ( state => state . courseHome ) ;
1718
19+ const key = optional ? 'optionalCompletionSummary' : 'completionSummary' ;
20+ const label = optional ? intl . formatMessage ( messages . optionalDonutLabel ) : intl . formatMessage ( messages . donutLabel ) ;
21+
22+ const progress = useModel ( 'progress' , courseId ) ;
1823 const {
19- completionSummary : {
20- completeCount,
21- incompleteCount,
22- lockedCount,
23- } ,
24- } = useModel ( 'progress' , courseId ) ;
24+ completeCount,
25+ incompleteCount,
26+ lockedCount,
27+ } = progress [ key ] ;
2528
2629 const numTotalUnits = completeCount + incompleteCount + lockedCount ;
2730 const completePercentage = completeCount ? Number ( ( ( completeCount / numTotalUnits ) * 100 ) . toFixed ( 0 ) ) : 0 ;
@@ -30,6 +33,10 @@ const CompletionDonutChart = ({ intl }) => {
3033
3134 const isLocaleRtl = isRtl ( getLocale ( ) ) ;
3235
36+ if ( optional && numTotalUnits === 0 ) {
37+ return < > </ > ;
38+ }
39+
3340 return (
3441 < >
3542 < svg role = "img" width = "50%" height = "100%" viewBox = "0 0 42 42" className = "donut" style = { { maxWidth : '178px' } } aria-hidden = "true" >
@@ -42,7 +49,7 @@ const CompletionDonutChart = ({ intl }) => {
4249 { completePercentage } { isLocaleRtl && '\u200f' } %
4350 </ text >
4451 < text x = "50%" y = "50%" className = "donut-chart-label" >
45- { intl . formatMessage ( messages . donutLabel ) }
52+ { label }
4653 </ text >
4754 </ g >
4855 < IncompleteDonutSegment incompletePercentage = { incompletePercentage } />
@@ -62,8 +69,13 @@ const CompletionDonutChart = ({ intl }) => {
6269 ) ;
6370} ;
6471
72+ CompletionDonutChart . defaultProps = {
73+ optional : false ,
74+ } ;
75+
6576CompletionDonutChart . propTypes = {
6677 intl : intlShape . isRequired ,
78+ optional : PropTypes . bool ,
6779} ;
6880
6981export default injectIntl ( CompletionDonutChart ) ;
0 commit comments