-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added implementation for new sidebar (#1260)
* feat: added implementation for new sidebar * test: fixed test cases * refactor: fixed naming convention and combine useeffects * refactor: improved sidebar UI and renamed sidebar flag * refactor: remove additional states * refactor: fixed UI and logic related issue * refactor: simplified condition * refactor: toggle sidebar action * refactor: fixed toggle issues * refactor: back arrow component * refactor: changed useeffect position --------- Co-authored-by: Awais Ansari <[email protected]>
- Loading branch information
1 parent
4dc4725
commit 023f5ac
Showing
28 changed files
with
834 additions
and
15 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
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,17 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
import SidebarContext from './SidebarContext'; | ||
import { SIDEBARS } from './sidebars'; | ||
|
||
const Sidebar = () => { | ||
const { currentSidebar } = useContext(SidebarContext); | ||
|
||
if (currentSidebar === null) { return null; } | ||
const SidebarToRender = SIDEBARS[currentSidebar].Sidebar; | ||
|
||
return ( | ||
<SidebarToRender /> | ||
); | ||
}; | ||
|
||
export default Sidebar; |
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,5 @@ | ||
import React from 'react'; | ||
|
||
const SidebarContext = React.createContext({}); | ||
|
||
export default SidebarContext; |
103 changes: 103 additions & 0 deletions
103
src/courseware/course/new-sidebar/SidebarContextProvider.jsx
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,103 @@ | ||
import React, { | ||
useCallback, useEffect, useMemo, useState, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import isEmpty from 'lodash/isEmpty'; | ||
|
||
import { breakpoints, useWindowSize } from '@edx/paragon'; | ||
|
||
import { getLocalStorage, setLocalStorage } from '../../../data/localStorage'; | ||
import { useModel } from '../../../generic/model-store'; | ||
import WIDGETS from './constants'; | ||
import SidebarContext from './SidebarContext'; | ||
import { SIDEBARS } from './sidebars'; | ||
|
||
const SidebarProvider = ({ | ||
courseId, | ||
unitId, | ||
children, | ||
}) => { | ||
const shouldDisplayFullScreen = useWindowSize().width < breakpoints.large.minWidth; | ||
const shouldDisplaySidebarOpen = useWindowSize().width > breakpoints.medium.minWidth; | ||
const query = new URLSearchParams(window.location.search); | ||
const initialSidebar = (shouldDisplaySidebarOpen || query.get('sidebar') === 'true') | ||
? SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID : null; | ||
const [currentSidebar, setCurrentSidebar] = useState(initialSidebar); | ||
const [notificationStatus, setNotificationStatus] = useState(getLocalStorage(`notificationStatus.${courseId}`)); | ||
const [hideDiscussionbar, setHideDiscussionbar] = useState(false); | ||
const [hideNotificationbar, setHideNotificationbar] = useState(false); | ||
const [upgradeNotificationCurrentState, setUpgradeNotificationCurrentState] = useState( | ||
getLocalStorage(`upgradeNotificationCurrentState.${courseId}`), | ||
); | ||
const topic = useModel('discussionTopics', unitId); | ||
const { verifiedMode } = useModel('courseHomeMeta', courseId); | ||
const isDiscussionbarAvailable = topic?.id && topic?.enabledInContext; | ||
const isNotificationbarAvailable = !isEmpty(verifiedMode); | ||
|
||
const onNotificationSeen = useCallback(() => { | ||
setNotificationStatus('inactive'); | ||
setLocalStorage(`notificationStatus.${courseId}`, 'inactive'); | ||
}, [courseId]); | ||
|
||
useEffect(() => { | ||
setHideDiscussionbar(!isDiscussionbarAvailable); | ||
setHideNotificationbar(!isNotificationbarAvailable); | ||
setCurrentSidebar(SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID); | ||
}, [unitId, topic]); | ||
|
||
useEffect(() => { | ||
if (hideDiscussionbar && hideNotificationbar) { | ||
setCurrentSidebar(null); | ||
} | ||
}, [hideDiscussionbar, hideNotificationbar]); | ||
|
||
const toggleSidebar = useCallback((sidebarId = null, widgetId = null) => { | ||
if (widgetId) { | ||
setHideDiscussionbar(prevWidgetId => (widgetId === WIDGETS.DISCUSSIONS ? true : prevWidgetId)); | ||
setHideNotificationbar(prevWidgetId => (widgetId === WIDGETS.NOTIFICATIONS ? true : prevWidgetId)); | ||
} else { | ||
setCurrentSidebar(prevSidebar => (sidebarId === prevSidebar ? null : sidebarId)); | ||
setHideDiscussionbar(!isDiscussionbarAvailable); | ||
setHideNotificationbar(!isNotificationbarAvailable); | ||
} | ||
}, [isDiscussionbarAvailable, isNotificationbarAvailable]); | ||
|
||
const contextValue = useMemo(() => ({ | ||
toggleSidebar, | ||
onNotificationSeen, | ||
setNotificationStatus, | ||
currentSidebar, | ||
notificationStatus, | ||
upgradeNotificationCurrentState, | ||
setUpgradeNotificationCurrentState, | ||
shouldDisplaySidebarOpen, | ||
shouldDisplayFullScreen, | ||
courseId, | ||
unitId, | ||
hideDiscussionbar, | ||
hideNotificationbar, | ||
isNotificationbarAvailable, | ||
isDiscussionbarAvailable, | ||
}), [courseId, currentSidebar, notificationStatus, onNotificationSeen, shouldDisplayFullScreen, | ||
shouldDisplaySidebarOpen, toggleSidebar, unitId, upgradeNotificationCurrentState, hideDiscussionbar, | ||
hideNotificationbar, isNotificationbarAvailable, isDiscussionbarAvailable]); | ||
|
||
return ( | ||
<SidebarContext.Provider value={contextValue}> | ||
{children} | ||
</SidebarContext.Provider> | ||
); | ||
}; | ||
|
||
SidebarProvider.propTypes = { | ||
courseId: PropTypes.string.isRequired, | ||
unitId: PropTypes.string.isRequired, | ||
children: PropTypes.node, | ||
}; | ||
|
||
SidebarProvider.defaultProps = { | ||
children: null, | ||
}; | ||
|
||
export default SidebarProvider; |
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,21 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
import SidebarContext from './SidebarContext'; | ||
import { SIDEBAR_ORDER, SIDEBARS } from './sidebars'; | ||
|
||
const SidebarTriggers = () => { | ||
const { toggleSidebar } = useContext(SidebarContext); | ||
|
||
return ( | ||
<div className="d-flex ml-auto"> | ||
{SIDEBAR_ORDER.map((sidebarId) => { | ||
const { Trigger } = SIDEBARS[sidebarId]; | ||
return ( | ||
<Trigger onClick={() => toggleSidebar(sidebarId)} key={sidebarId} /> | ||
); | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
export default SidebarTriggers; |
113 changes: 113 additions & 0 deletions
113
src/courseware/course/new-sidebar/common/SidebarBase.jsx
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,113 @@ | ||
import React, { useCallback, useContext } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import classNames from 'classnames'; | ||
|
||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { Icon, IconButton } from '@edx/paragon'; | ||
import { ArrowBackIos, Close } from '@edx/paragon/icons'; | ||
|
||
import { useEventListener } from '../../../../generic/hooks'; | ||
import WIDGETS from '../constants'; | ||
import messages from '../messages'; | ||
import SidebarContext from '../SidebarContext'; | ||
|
||
const SidebarBase = ({ | ||
title, | ||
ariaLabel, | ||
sidebarId, | ||
className, | ||
children, | ||
showTitleBar, | ||
width, | ||
allowFullHeight, | ||
showBorder, | ||
}) => { | ||
const intl = useIntl(); | ||
const { | ||
toggleSidebar, | ||
shouldDisplayFullScreen, | ||
currentSidebar, | ||
} = useContext(SidebarContext); | ||
|
||
const receiveMessage = useCallback(({ data }) => { | ||
const { type } = data; | ||
if (type === 'learning.events.sidebar.close') { | ||
toggleSidebar(currentSidebar, WIDGETS.DISCUSSIONS); | ||
} | ||
}, [toggleSidebar]); | ||
|
||
useEventListener('message', receiveMessage); | ||
|
||
return ( | ||
<section | ||
className={classNames('ml-0 ml-lg-4 h-auto align-top', { | ||
'min-vh-100': !shouldDisplayFullScreen && allowFullHeight, | ||
'bg-white m-0 border-0 fixed-top vh-100 rounded-0': shouldDisplayFullScreen, | ||
'd-none': currentSidebar !== sidebarId, | ||
'border border-light-400 rounded-sm': showBorder, | ||
}, className)} | ||
data-testid={`sidebar-${sidebarId}`} | ||
style={{ width: shouldDisplayFullScreen ? '100%' : width }} | ||
aria-label={ariaLabel} | ||
> | ||
{shouldDisplayFullScreen | ||
&& ( | ||
<div | ||
className="pt-2 pb-2.5 border-bottom border-light-400 d-flex align-items-center ml-2" | ||
onClick={() => toggleSidebar(null)} | ||
onKeyDown={() => toggleSidebar(null)} | ||
role="button" | ||
tabIndex="0" | ||
alt={intl.formatMessage(messages.responsiveCloseSidebarTray)} | ||
> | ||
<Icon src={ArrowBackIos} /> | ||
<span className="font-weight-bold m-2 d-inline-block"> | ||
{intl.formatMessage(messages.responsiveCloseSidebarTray)} | ||
</span> | ||
</div> | ||
)} | ||
{showTitleBar && ( | ||
<> | ||
<div className="d-flex align-items-center"> | ||
<span className="p-2.5 d-inline-block">{title}</span> | ||
<div className="d-inline-flex mr-2 mt-1.5 ml-auto"> | ||
<IconButton | ||
src={Close} | ||
size="sm" | ||
iconAs={Icon} | ||
onClick={() => toggleSidebar(sidebarId)} | ||
alt={intl.formatMessage(messages.closeTrigger)} | ||
className="icon-hover" | ||
/> | ||
</div> | ||
</div> | ||
<div className="py-1 bg-gray-100 border-top border-bottom border-light-400" /> | ||
</> | ||
)} | ||
{children} | ||
</section> | ||
); | ||
}; | ||
|
||
SidebarBase.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
ariaLabel: PropTypes.string.isRequired, | ||
sidebarId: PropTypes.string.isRequired, | ||
className: PropTypes.string, | ||
children: PropTypes.element.isRequired, | ||
showTitleBar: PropTypes.bool, | ||
width: PropTypes.string, | ||
allowFullHeight: PropTypes.bool, | ||
showBorder: PropTypes.bool, | ||
}; | ||
|
||
SidebarBase.defaultProps = { | ||
width: '50rem', | ||
allowFullHeight: false, | ||
showTitleBar: true, | ||
className: '', | ||
showBorder: true, | ||
}; | ||
|
||
export default SidebarBase; |
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,6 @@ | ||
const WIDGETS = { | ||
DISCUSSIONS: 'DISCUSSIONS', | ||
NOTIFICATIONS: 'NOTIFICATIONS', | ||
}; | ||
|
||
export default WIDGETS; |
20 changes: 20 additions & 0 deletions
20
src/courseware/course/new-sidebar/icons/RightSidebarFilled.jsx
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,20 @@ | ||
import * as React from 'react'; | ||
|
||
const RightSidebarFilled = (props) => ( | ||
<svg | ||
width={24} | ||
height={24} | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M2 22V2h20v20H2ZM14 4H4v16h10V4Z" | ||
fill="currentColor" | ||
/> | ||
</svg> | ||
); | ||
export default RightSidebarFilled; |
20 changes: 20 additions & 0 deletions
20
src/courseware/course/new-sidebar/icons/RightSidebarOutlined.jsx
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,20 @@ | ||
import * as React from 'react'; | ||
|
||
const RightSidebarOutlined = (props) => ( | ||
<svg | ||
width={24} | ||
height={24} | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M2 2v20h20V2H2Zm18 2h-4v16h4V4ZM4 4h10v16H4V4Z" | ||
fill="currentColor" | ||
/> | ||
</svg> | ||
); | ||
export default RightSidebarOutlined; |
Oops, something went wrong.