@@ -2,7 +2,7 @@ import React, { useCallback, useContext, useMemo } from 'react';
2
2
import classNames from 'classnames' ;
3
3
import { Button , Container , Stack } from '@openedx/paragon' ;
4
4
import { FormattedDate , FormattedTime , useIntl } from '@edx/frontend-platform/i18n' ;
5
- import { useCommitLibraryChanges } from '../data/apiHooks' ;
5
+ import { useCommitLibraryChanges , useRevertLibraryChanges } from '../data/apiHooks' ;
6
6
import { ContentLibrary } from '../data/api' ;
7
7
import { ToastContext } from '../../generic/toast-context' ;
8
8
import messages from './messages' ;
@@ -14,6 +14,7 @@ type LibraryPublishStatusProps = {
14
14
const LibraryPublishStatus = ( { library } : LibraryPublishStatusProps ) => {
15
15
const intl = useIntl ( ) ;
16
16
const commitLibraryChanges = useCommitLibraryChanges ( ) ;
17
+ const revertLibraryChanges = useRevertLibraryChanges ( ) ;
17
18
const { showToast } = useContext ( ToastContext ) ;
18
19
19
20
const commit = useCallback ( ( ) => {
@@ -25,9 +26,6 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
25
26
} ) ;
26
27
} , [ ] ) ;
27
28
28
- /**
29
- * TODO, the discard changes breaks the library.
30
- * Discomment this when discard changes is fixed.
31
29
const revert = useCallback ( ( ) => {
32
30
revertLibraryChanges . mutateAsync ( library . id )
33
31
. then ( ( ) => {
@@ -36,15 +34,16 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
36
34
showToast ( intl . formatMessage ( messages . revertErrorMsg ) ) ;
37
35
} ) ;
38
36
} , [ ] ) ;
39
- */
40
37
41
38
const {
42
39
isPublished,
40
+ isNew,
43
41
statusMessage,
44
42
extraStatusMessage,
45
43
bodyMessage,
46
44
} = useMemo ( ( ) => {
47
45
let isPublishedResult : boolean ;
46
+ let isNewResult = false ;
48
47
let statusMessageResult : string ;
49
48
let extraStatusMessageResult : string | undefined ;
50
49
let bodyMessageResult : string | undefined ;
@@ -94,6 +93,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
94
93
95
94
if ( ! library . lastPublished ) {
96
95
// Library is never published (new)
96
+ isNewResult = library . numBlocks === 0 ; // allow discarding if components are added
97
97
isPublishedResult = false ;
98
98
statusMessageResult = intl . formatMessage ( messages . draftStatusLabel ) ;
99
99
extraStatusMessageResult = intl . formatMessage ( messages . neverPublishedLabel ) ;
@@ -123,6 +123,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
123
123
}
124
124
return {
125
125
isPublished : isPublishedResult ,
126
+ isNew : isNewResult ,
126
127
statusMessage : statusMessageResult ,
127
128
extraStatusMessage : extraStatusMessageResult ,
128
129
bodyMessage : bodyMessageResult ,
@@ -153,15 +154,11 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
153
154
< Button disabled = { isPublished } onClick = { commit } >
154
155
{ intl . formatMessage ( messages . publishButtonLabel ) }
155
156
</ 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 >
165
162
</ Stack >
166
163
</ Container >
167
164
</ Stack >
0 commit comments