Skip to content

Commit

Permalink
feat: refactor date visibility (openedx#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Feb 22, 2024
1 parent 6def666 commit ff7366d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
18 changes: 0 additions & 18 deletions src/courseware/course/Course.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ const Course = ({

const SidebarProviderComponent = enableNewSidebar === 'true' ? NewSidebarProvider : SidebarProvider;

const chatValidDates = () => {
const date = new Date();
const utcDate = date.toISOString();

const enrollmentStartDate = course.enrollmentStart || utcDate;
const startDate = course.start || enrollmentStartDate;
const enrollmentEndDate = course.enrollmentEnd || utcDate;
const endDate = course.end || enrollmentEndDate;

return (
startDate <= enrollmentStartDate
&& enrollmentStartDate <= utcDate
&& utcDate <= enrollmentEndDate
&& enrollmentEndDate <= endDate
);
};

return (
<SidebarProviderComponent courseId={courseId} unitId={unitId}>
<Helmet>
Expand All @@ -109,7 +92,6 @@ const Course = ({
courseId={courseId}
contentToolsEnabled={course.showCalculator || course.notes.enabled}
unitId={unitId}
validDates={chatValidDates()}
/>
</>
)}
Expand Down
24 changes: 21 additions & 3 deletions src/courseware/course/chat/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import PropTypes from 'prop-types';
import { Xpert } from '@edx/frontend-lib-learning-assistant';
import { injectIntl } from '@edx/frontend-platform/i18n';

import { useModel } from '../../../generic/model-store';

const Chat = ({
enabled,
enrollmentMode,
isStaff,
courseId,
contentToolsEnabled,
unitId,
validDates,
}) => {
const {
activeAttempt, exam,
} = useSelector(state => state.specialExams);
const course = useModel('coursewareMeta', courseId);

const VERIFIED_MODES = [
'professional',
Expand All @@ -35,10 +37,27 @@ const Chat = ({
&& [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
);

const validDates = () => {
const date = new Date();
const utcDate = date.toISOString();

const enrollmentStartDate = course.enrollmentStart || utcDate;
const startDate = course.start || enrollmentStartDate;
const enrollmentEndDate = course.enrollmentEnd || utcDate;
const endDate = course.end || enrollmentEndDate;

return (
startDate <= enrollmentStartDate
&& enrollmentStartDate <= utcDate
&& utcDate <= enrollmentEndDate
&& enrollmentEndDate <= endDate
);
};

const shouldDisplayChat = (
enabled
&& (hasVerifiedEnrollment || isStaff) // display only to verified learners or staff
&& validDates
&& validDates()
// it is necessary to check both whether the user is in an exam, and whether or not they are viewing an exam
// this will prevent the learner from interacting with the tool at any point of the exam flow, even at the
// entrance interstitial.
Expand All @@ -63,7 +82,6 @@ Chat.propTypes = {
courseId: PropTypes.string.isRequired,
contentToolsEnabled: PropTypes.bool.isRequired,
unitId: PropTypes.string.isRequired,
validDates: PropTypes.bool.isRequired,
};

Chat.defaultProps = {
Expand Down
18 changes: 13 additions & 5 deletions src/courseware/course/chat/Chat.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserRouter } from 'react-router-dom';
import React from 'react';
import { Factory } from 'rosie';

import {
initializeMockApp,
Expand Down Expand Up @@ -72,7 +73,6 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down Expand Up @@ -100,7 +100,6 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down Expand Up @@ -156,7 +155,6 @@ describe('Chat', () => {
enabled={test.enabled}
courseId={courseId}
contentToolsEnabled={false}
validDates
/>
</BrowserRouter>,
{ store },
Expand All @@ -173,6 +171,18 @@ describe('Chat', () => {
});

it('if course end date has passed, component should not be visible', async () => {
store = await initializeTestStore({
specialExams: {
activeAttempt: {
attempt_id: 1,
},
},
courseMetadata: Factory.build('courseMetadata', {
start: '2014-02-03T05:00:00Z',
end: '2014-02-05T05:00:00Z',
}),
});

render(
<BrowserRouter>
<Chat
Expand All @@ -181,7 +191,6 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
validDates={false}
/>
</BrowserRouter>,
{ store },
Expand All @@ -208,7 +217,6 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down

0 comments on commit ff7366d

Please sign in to comment.