-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
420 additions
and
16 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
src/library-authoring/collections/CollectionDetails.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,152 @@ | ||
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; | ||
import { Icon, Stack } from '@openedx/paragon'; | ||
import { useContext, useState } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { getItemIcon } from '../../generic/block-type-utils'; | ||
import { ToastContext } from '../../generic/toast-context'; | ||
import { BlockTypeLabel, type CollectionHit, useSearchContext } from '../../search-manager'; | ||
import type { ContentLibrary } from '../data/api'; | ||
import { useUpdateCollection } from '../data/apiHooks'; | ||
import HistoryWidget from '../generic/history-widget'; | ||
import messages from './messages'; | ||
|
||
interface BlockCountProps { | ||
count: number, | ||
blockType?: string, | ||
label: React.ReactNode, | ||
className?: string, | ||
} | ||
|
||
const BlockCount = ({ | ||
count, | ||
blockType, | ||
label, | ||
className, | ||
}: BlockCountProps) => { | ||
const icon = blockType && getItemIcon(blockType); | ||
return ( | ||
<Stack className={classNames('text-center', className)}> | ||
<span className="text-muted">{label}</span> | ||
<Stack direction="horizontal" gap={1} className="justify-content-center"> | ||
{icon && <Icon src={icon} size="lg" />} | ||
<span>{count}</span> | ||
</Stack> | ||
</Stack> | ||
); | ||
}; | ||
|
||
const CollectionStatsWidget = () => { | ||
const { | ||
blockTypes, | ||
} = useSearchContext(); | ||
|
||
const blockTypesArray = Object.entries(blockTypes) | ||
.map(([blockType, count]) => ({ blockType, count })) | ||
.sort((a, b) => b.count - a.count); | ||
|
||
const totalBlocksCount = blockTypesArray.reduce((acc, { count }) => acc + count, 0); | ||
const otherBlocks = blockTypesArray.splice(3); | ||
const otherBlocksCount = otherBlocks.reduce((acc, { count }) => acc + count, 0); | ||
|
||
if (totalBlocksCount === 0) { | ||
return ( | ||
<div className="text-center text-muted"> | ||
<FormattedMessage {...messages.detailsTabStatsNoComponents} /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Stack direction="horizontal" className="p-2 justify-content-between" gap={2}> | ||
<BlockCount | ||
label={<FormattedMessage {...messages.detailsTabStatsTotalComponents} />} | ||
count={totalBlocksCount} | ||
className="border-right" | ||
/> | ||
{blockTypesArray.map(({ blockType, count }) => ( | ||
<BlockCount | ||
key={blockType} | ||
label={<BlockTypeLabel type={blockType} />} | ||
blockType={blockType} | ||
count={count} | ||
/> | ||
))} | ||
{otherBlocks.length > 0 && ( | ||
<BlockCount | ||
label={<FormattedMessage {...messages.detailsTabStatsOtherComponents} />} | ||
count={otherBlocksCount} | ||
/> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
interface CollectionDetailsProps { | ||
library: ContentLibrary, | ||
collection: CollectionHit, | ||
} | ||
|
||
const CollectionDetails = ({ library, collection }: CollectionDetailsProps) => { | ||
const intl = useIntl(); | ||
const { showToast } = useContext(ToastContext); | ||
|
||
const [description, setDescription] = useState(collection.description); | ||
|
||
const updateMutation = useUpdateCollection(library.id, collection.blockId); | ||
|
||
// istanbul ignore if: this should never happen | ||
if (!collection) { | ||
return null; | ||
} | ||
|
||
const onSubmit = (e: React.FocusEvent<HTMLTextAreaElement>) => { | ||
const newDescription = e.target.value; | ||
if (newDescription === collection.description) { | ||
return; | ||
} | ||
updateMutation.mutateAsync({ | ||
description: newDescription, | ||
}).then(() => { | ||
showToast(intl.formatMessage(messages.updateCollectionSuccessMsg)); | ||
}).catch(() => { | ||
showToast(intl.formatMessage(messages.updateCollectionErrorMsg)); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Stack gap={3}> | ||
<div> | ||
<h3 className="h5"> | ||
{intl.formatMessage(messages.detailsTabDescriptionTitle)} | ||
</h3> | ||
{library.canEditLibrary ? ( | ||
<textarea | ||
className="form-control" | ||
value={description} | ||
onChange={(e) => setDescription(e.target.value)} | ||
onBlur={onSubmit} | ||
/> | ||
) : collection.description} | ||
</div> | ||
<div> | ||
<h3 className="h5"> | ||
{intl.formatMessage(messages.detailsTabStatsTitle)} | ||
</h3> | ||
<CollectionStatsWidget /> | ||
</div> | ||
<hr className="w-100" /> | ||
<div> | ||
<h3 className="h5"> | ||
{intl.formatMessage(messages.detailsTabHistoryTitle)} | ||
</h3> | ||
<HistoryWidget | ||
created={collection.created ? new Date(collection.created * 1000) : null} | ||
modified={collection.modified ? new Date(collection.modified * 1000) : null} | ||
/> | ||
</div> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default CollectionDetails; |
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
95 changes: 88 additions & 7 deletions
95
src/library-authoring/collections/CollectionInfoHeader.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 |
---|---|---|
@@ -1,13 +1,94 @@ | ||
import { type CollectionHit } from '../../search-manager/data/api'; | ||
import React, { useState, useContext, useCallback } from 'react'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { | ||
Icon, | ||
IconButton, | ||
Stack, | ||
Form, | ||
} from '@openedx/paragon'; | ||
import { Edit } from '@openedx/paragon/icons'; | ||
|
||
import { ToastContext } from '../../generic/toast-context'; | ||
import type { ContentLibrary } from '../data/api'; | ||
import type { CollectionHit } from '../../search-manager/data/api'; | ||
import { useUpdateCollection } from '../data/apiHooks'; | ||
import messages from './messages'; | ||
|
||
interface CollectionInfoHeaderProps { | ||
collection?: CollectionHit; | ||
library: ContentLibrary; | ||
collection: CollectionHit; | ||
} | ||
|
||
const CollectionInfoHeader = ({ collection } : CollectionInfoHeaderProps) => ( | ||
<div className="d-flex flex-wrap"> | ||
{collection?.displayName} | ||
</div> | ||
); | ||
const CollectionInfoHeader = ({ library, collection } : CollectionInfoHeaderProps) => { | ||
const intl = useIntl(); | ||
const [inputIsActive, setIsActive] = useState(false); | ||
|
||
const updateMutation = useUpdateCollection(library.id, collection.blockId); | ||
const { showToast } = useContext(ToastContext); | ||
|
||
const handleSaveDisplayName = useCallback( | ||
(event) => { | ||
const newTitle = event.target.value; | ||
if (newTitle && newTitle !== collection?.displayName) { | ||
updateMutation.mutateAsync({ | ||
title: newTitle, | ||
}).then(() => { | ||
showToast(intl.formatMessage(messages.updateCollectionSuccessMsg)); | ||
}).catch(() => { | ||
showToast(intl.formatMessage(messages.updateCollectionErrorMsg)); | ||
}).finally(() => { | ||
setIsActive(false); | ||
}); | ||
} | ||
}, | ||
[collection, showToast, intl], | ||
); | ||
|
||
const handleClick = () => { | ||
setIsActive(true); | ||
}; | ||
|
||
const handleOnKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter') { | ||
handleSaveDisplayName(event); | ||
} else if (event.key === 'Escape') { | ||
setIsActive(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Stack direction="horizontal"> | ||
{inputIsActive | ||
? ( | ||
<Form.Control | ||
autoFocus | ||
name="title" | ||
id="title" | ||
type="text" | ||
aria-label="Title input" | ||
defaultValue={collection?.displayName} | ||
onBlur={handleSaveDisplayName} | ||
onKeyDown={handleOnKeyDown} | ||
/> | ||
) | ||
: ( | ||
<> | ||
<span className="font-weight-bold m-1.5"> | ||
{collection?.displayName} | ||
</span> | ||
{library.canEditLibrary && ( | ||
<IconButton | ||
src={Edit} | ||
iconAs={Icon} | ||
alt={intl.formatMessage(messages.editTitleButtonAlt)} | ||
onClick={handleClick} | ||
size="inline" | ||
/> | ||
)} | ||
</> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default CollectionInfoHeader; |
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
Oops, something went wrong.