forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LibraryPublishStatus component added
- Connection with API to publish an revert changes - LibraryPublishStatus component created with draft and publish status - Component create with the feature to publish and revert changes
- Loading branch information
Showing
11 changed files
with
301 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "library-authoring/library-info/LibraryPublishStatus" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/library-authoring/library-info/LibraryPublishStatus.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.library-publish-status { | ||
|
||
&.draft-status { | ||
background-color: #FDF3E9; | ||
border-top: 4px solid #F4B57B; | ||
} | ||
|
||
&.published-status { | ||
background-color: $info-100; | ||
border-top: 4px solid $info-400; | ||
} | ||
} | ||
|
136 changes: 136 additions & 0 deletions
136
src/library-authoring/library-info/LibraryPublishStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import React, { useCallback, useContext, useMemo } from "react"; | ||
import { Button, Container, Stack } from "@openedx/paragon"; | ||
import { ContentLibrary } from "../data/api"; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import messages from "./messages"; | ||
import classNames from 'classnames'; | ||
import { useCommitLibraryChanges, useRevertLibraryChanges } from "../data/apiHook"; | ||
import { ToastContext } from "../../generic/toast-context"; | ||
import { convertToStringFromDateAndFormat } from "../../utils"; | ||
import { COMMA_SEPARATED_DATE_FORMAT, TIME_FORMAT } from "../../constants"; | ||
|
||
type LibraryPublishStatusProps = { | ||
library: ContentLibrary, | ||
} | ||
|
||
const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => { | ||
const intl = useIntl(); | ||
const commitLibraryChanges = useCommitLibraryChanges(); | ||
const revertLibraryChanges = useRevertLibraryChanges(); | ||
const { showToast } = useContext(ToastContext); | ||
|
||
const commit = useCallback(() => { | ||
commitLibraryChanges.mutateAsync(library.id) | ||
.then(() => { | ||
showToast(intl.formatMessage(messages.publishSuccessMsg)); | ||
}).catch(() => { | ||
showToast(intl.formatMessage(messages.publishErrorMsg)); | ||
}); | ||
}, []); | ||
|
||
const revert = useCallback(() => { | ||
revertLibraryChanges.mutateAsync(library.id) | ||
.then(() => { | ||
showToast(intl.formatMessage(messages.revertSuccessMsg)); | ||
}).catch(() => { | ||
showToast(intl.formatMessage(messages.revertErrorMsg)); | ||
}); | ||
}, []); | ||
|
||
const { | ||
isPublished, | ||
statusMessage, | ||
extraStatusMessage, | ||
bodyMessage, | ||
} = useMemo(() => { | ||
let isPublished : boolean; | ||
let statusMessage : string; | ||
let extraStatusMessage : string | undefined = undefined; | ||
let bodyMessage : string | undefined = undefined; | ||
const buildDraftBodyMessage = (() => { | ||
if (library.lastDraftCreatedBy) { | ||
return intl.formatMessage(messages.lastDraftMsg, { | ||
date: <b>{convertToStringFromDateAndFormat(library.lastDraftCreated, COMMA_SEPARATED_DATE_FORMAT)}</b>, | ||
time: <b>{convertToStringFromDateAndFormat(library.lastDraftCreated, TIME_FORMAT)}</b>, | ||
user: <b>{library.lastDraftCreatedBy}</b>, | ||
}); | ||
} else { | ||
return intl.formatMessage(messages.lastDraftMsgWithoutUser, { | ||
date: <b>{convertToStringFromDateAndFormat(library.lastDraftCreated, COMMA_SEPARATED_DATE_FORMAT)}</b>, | ||
time: <b>{convertToStringFromDateAndFormat(library.lastDraftCreated, TIME_FORMAT)}</b>, | ||
}); | ||
} | ||
}); | ||
|
||
if (!library.lastPublished) { | ||
// Library is never published (new) | ||
isPublished = false; | ||
statusMessage = intl.formatMessage(messages.draftStatusLabel); | ||
extraStatusMessage = intl.formatMessage(messages.neverPublishedLabel); | ||
bodyMessage = buildDraftBodyMessage(); | ||
} else if (library.hasUnpublishedChanges || library.hasUnpublishedDeletes) { | ||
// Library is on Draft state | ||
isPublished = false; | ||
statusMessage = intl.formatMessage(messages.draftStatusLabel); | ||
extraStatusMessage = intl.formatMessage(messages.unpublishedStatusLabel); | ||
bodyMessage = buildDraftBodyMessage(); | ||
} else { | ||
// Library is published | ||
isPublished = true; | ||
statusMessage = intl.formatMessage(messages.publishedStatusLabel); | ||
if (library.publishedBy) { | ||
bodyMessage = intl.formatMessage(messages.lastPublishedMsg, { | ||
date: <b>{convertToStringFromDateAndFormat(library.lastPublished, COMMA_SEPARATED_DATE_FORMAT)}</b>, | ||
time: <b>{convertToStringFromDateAndFormat(library.lastPublished, TIME_FORMAT)}</b>, | ||
user: <b>{library.publishedBy}</b>, | ||
}) | ||
} else { | ||
bodyMessage = intl.formatMessage(messages.lastPublishedMsgWithoutUser, { | ||
date: <b>{convertToStringFromDateAndFormat(library.lastPublished, COMMA_SEPARATED_DATE_FORMAT)}</b>, | ||
time: <b>{convertToStringFromDateAndFormat(library.lastPublished, TIME_FORMAT)}</b>, | ||
}) | ||
} | ||
} | ||
return { | ||
isPublished, | ||
statusMessage, | ||
extraStatusMessage, | ||
bodyMessage, | ||
} | ||
}, [library]) | ||
|
||
return ( | ||
<Stack> | ||
<Container className={classNames("library-publish-status", { | ||
"draft-status": !isPublished, | ||
"published-status": isPublished, | ||
})}> | ||
<span className="font-weight-bold"> | ||
{statusMessage} | ||
</span> | ||
{ extraStatusMessage && ( | ||
<span className="ml-1"> | ||
{extraStatusMessage} | ||
</span> | ||
)} | ||
</Container> | ||
<Container> | ||
<Stack> | ||
<span> | ||
{bodyMessage} | ||
</span> | ||
<Button disabled={isPublished} onClick={commit}> | ||
{intl.formatMessage(messages.publishButtonLabel)} | ||
</Button> | ||
<div className='d-flex justify-content-end'> | ||
<Button disabled={isPublished} variant='link' onClick={revert}> | ||
{intl.formatMessage(messages.discardChangesButtonLabel)} | ||
</Button> | ||
</div> | ||
</Stack> | ||
</Container> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default LibraryPublishStatus; |
Oops, something went wrong.