@@ -2,7 +2,7 @@ import React, { useCallback, useContext, useMemo } from 'react';
22import classNames from 'classnames' ;
33import { Button , Container , Stack } from '@openedx/paragon' ;
44import { FormattedDate , FormattedTime , useIntl } from '@edx/frontend-platform/i18n' ;
5- import { useCommitLibraryChanges } from '../data/apiHooks' ;
5+ import { useCommitLibraryChanges , useRevertLibraryChanges } from '../data/apiHooks' ;
66import { ContentLibrary } from '../data/api' ;
77import { ToastContext } from '../../generic/toast-context' ;
88import messages from './messages' ;
@@ -14,6 +14,7 @@ type LibraryPublishStatusProps = {
1414const LibraryPublishStatus = ( { library } : LibraryPublishStatusProps ) => {
1515 const intl = useIntl ( ) ;
1616 const commitLibraryChanges = useCommitLibraryChanges ( ) ;
17+ const revertLibraryChanges = useRevertLibraryChanges ( ) ;
1718 const { showToast } = useContext ( ToastContext ) ;
1819
1920 const commit = useCallback ( ( ) => {
@@ -25,9 +26,6 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
2526 } ) ;
2627 } , [ ] ) ;
2728
28- /**
29- * TODO, the discard changes breaks the library.
30- * Discomment this when discard changes is fixed.
3129 const revert = useCallback ( ( ) => {
3230 revertLibraryChanges . mutateAsync ( library . id )
3331 . then ( ( ) => {
@@ -36,15 +34,16 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
3634 showToast ( intl . formatMessage ( messages . revertErrorMsg ) ) ;
3735 } ) ;
3836 } , [ ] ) ;
39- */
4037
4138 const {
4239 isPublished,
40+ isNew,
4341 statusMessage,
4442 extraStatusMessage,
4543 bodyMessage,
4644 } = useMemo ( ( ) => {
4745 let isPublishedResult : boolean ;
46+ let isNewResult = false ;
4847 let statusMessageResult : string ;
4948 let extraStatusMessageResult : string | undefined ;
5049 let bodyMessageResult : string | undefined ;
@@ -94,6 +93,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
9493
9594 if ( ! library . lastPublished ) {
9695 // Library is never published (new)
96+ isNewResult = library . numBlocks === 0 ; // allow discarding if components are added
9797 isPublishedResult = false ;
9898 statusMessageResult = intl . formatMessage ( messages . draftStatusLabel ) ;
9999 extraStatusMessageResult = intl . formatMessage ( messages . neverPublishedLabel ) ;
@@ -123,6 +123,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
123123 }
124124 return {
125125 isPublished : isPublishedResult ,
126+ isNew : isNewResult ,
126127 statusMessage : statusMessageResult ,
127128 extraStatusMessage : extraStatusMessageResult ,
128129 bodyMessage : bodyMessageResult ,
@@ -153,15 +154,11 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
153154 < Button disabled = { isPublished } onClick = { commit } >
154155 { intl . formatMessage ( messages . publishButtonLabel ) }
155156 </ Button >
156- { /*
157- * TODO, the discard changes breaks the library.
158- * Discomment this when discard changes is fixed.
159- <div className="d-flex justify-content-end">
160- <Button disabled={isPublished} variant="link" onClick={revert}>
161- {intl.formatMessage(messages.discardChangesButtonLabel)}
162- </Button>
163- </div>
164- */ }
157+ < div className = "d-flex justify-content-end" >
158+ < Button disabled = { isPublished || isNew } variant = "link" onClick = { revert } >
159+ { intl . formatMessage ( messages . discardChangesButtonLabel ) }
160+ </ Button >
161+ </ div >
165162 </ Stack >
166163 </ Container >
167164 </ Stack >
0 commit comments