Skip to content

Commit

Permalink
fix: make side bar notification behave correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
leangseu-edx committed Nov 21, 2023
1 parent bfbd4d4 commit 659fe6e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 31 deletions.
12 changes: 0 additions & 12 deletions src/courseware/course/Course.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import SidebarProvider from './sidebar/SidebarContextProvider';
import SidebarTriggers from './sidebar/SidebarTriggers';

import { useModel } from '../../generic/model-store';
import { getSessionStorage, setSessionStorage } from '../../data/sessionStorage';

const Course = ({
courseId,
Expand All @@ -32,7 +31,6 @@ const Course = ({
const {
celebrations,
isStaff,
verifiedMode,
} = useModel('courseHomeMeta', courseId);
const sequence = useModel('sequences', sequenceId);
const section = useModel('sections', sequence ? sequence.sectionId : null);
Expand All @@ -55,16 +53,6 @@ const Course = ({
const shouldDisplayTriggers = windowWidth >= breakpoints.small.minWidth;
const daysPerWeek = course?.courseGoals?.selectedGoal?.daysPerWeek;

// 1. open the notification tray if the user has not purchased the course and is on a desktop
// 2. default to close on the first time access
// 3. use the last state if the user has access the course before
const shouldDisplayNotificationTrayOpenOnLoad = windowWidth > breakpoints.medium.minWidth && !!verifiedMode;
if (shouldDisplayNotificationTrayOpenOnLoad) {
setSessionStorage(`notificationTrayStatus.${courseId}`, 'open');
} else if (!getSessionStorage(`notificationTrayStatus.${courseId}`)) {
setSessionStorage(`notificationTrayStatus.${courseId}`, 'closed');
}

useEffect(() => {
const celebrateFirstSection = celebrations && celebrations.firstSection;
setFirstSectionCelebrationOpen(shouldCelebrateOnSectionLoad(
Expand Down
12 changes: 5 additions & 7 deletions src/courseware/course/sidebar/SidebarContextProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import React, {
useEffect, useState, useMemo, useCallback,
} from 'react';

import { useModel } from '../../../generic/model-store';
import { getLocalStorage, setLocalStorage } from '../../../data/localStorage';

import SidebarContext from './SidebarContext';
import { SIDEBARS } from './sidebars';

Expand All @@ -13,6 +15,7 @@ const SidebarProvider = ({
unitId,
children,
}) => {
const { verifiedMode } = useModel('courseHomeMeta', courseId);
const shouldDisplayFullScreen = useWindowSize().width < breakpoints.large.minWidth;
const shouldDisplaySidebarOpen = useWindowSize().width > breakpoints.medium.minWidth;
const query = new URLSearchParams(window.location.search);
Expand All @@ -22,13 +25,8 @@ const SidebarProvider = ({
const [upgradeNotificationCurrentState, setUpgradeNotificationCurrentState] = useState(getLocalStorage(`upgradeNotificationCurrentState.${courseId}`));

useEffect(() => {
// if the user left the tray open in the previous session, open it again
// if the user is not verified, open the tray iff the previous session was open and the user on large screen
const openBySession = getSessionStorage(`notificationTrayStatus.${courseId}`) !== 'closed';
const showNotificationsOnLoad = openBySession || (!!verifiedMode && shouldDisplaySidebarOpen && openBySession );
if (showNotificationsOnLoad) {
setCurrentSidebar(SIDEBARS.NOTIFICATIONS.ID);
}
// if the user hasn't purchased the course, show the notifications sidebar
setCurrentSidebar(!!verifiedMode ? SIDEBARS.NOTIFICATIONS.ID : SIDEBARS.DISCUSSIONS.ID);

Check failure on line 29 in src/courseware/course/sidebar/SidebarContextProvider.jsx

View workflow job for this annotation

GitHub Actions / tests

Redundant double negation
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [unitId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const NotificationTray = ({ intl }) => {
title={intl.formatMessage(messages.notificationTitle)}
ariaLabel={intl.formatMessage(messages.notificationTray)}
sidebarId={ID}
width="50rem"
className={classNames({ 'h-100': !verifiedMode && !shouldDisplayFullScreen })}
>
<div>{verifiedMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import PropTypes from 'prop-types';
import React, { useContext, useEffect } from 'react';
import { getLocalStorage, setLocalStorage } from '../../../../../data/localStorage';
import { getSessionStorage, setSessionStorage } from '../../../../../data/sessionStorage';
import messages from '../../../messages';
import SidebarTriggerBase from '../../common/TriggerBase';
import SidebarContext from '../../SidebarContext';
Expand Down Expand Up @@ -47,17 +46,8 @@ const NotificationTrigger = ({
UpdateUpgradeNotificationLastSeen();
});

const handleClick = () => {
if (getSessionStorage(`notificationTrayStatus.${courseId}`) === 'closed') {
setSessionStorage(`notificationTrayStatus.${courseId}`, 'open');
} else {
setSessionStorage(`notificationTrayStatus.${courseId}`, 'closed');
}
onClick();
};

return (
<SidebarTriggerBase onClick={handleClick} ariaLabel={intl.formatMessage(messages.openNotificationTrigger)}>
<SidebarTriggerBase onClick={onClick} ariaLabel={intl.formatMessage(messages.openNotificationTrigger)}>
<NotificationIcon status={notificationStatus} notificationColor="bg-danger-500" />
</SidebarTriggerBase>
);
Expand Down
2 changes: 1 addition & 1 deletion src/data/sessionStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getSessionStorage(key) {
function setSessionStorage(key, value) {
try {
if (global.sessionStorage) {
global.sessionStorage.setItem(key, JSON.stringify(value));
global.sessionStorage.setItem(key, value);
}
} catch (e) {
// If this fails, just bail.
Expand Down

0 comments on commit 659fe6e

Please sign in to comment.