From ff7366d42a29627ecdf2dbb245a330ff7535cd43 Mon Sep 17 00:00:00 2001 From: Alison Langston <46360176+alangsto@users.noreply.github.com> Date: Thu, 22 Feb 2024 15:25:37 -0500 Subject: [PATCH] feat: refactor date visibility (#1298) --- src/courseware/course/Course.jsx | 18 ------------------ src/courseware/course/chat/Chat.jsx | 24 +++++++++++++++++++++--- src/courseware/course/chat/Chat.test.jsx | 18 +++++++++++++----- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/courseware/course/Course.jsx b/src/courseware/course/Course.jsx index 6da4358948..c2b28cb26e 100644 --- a/src/courseware/course/Course.jsx +++ b/src/courseware/course/Course.jsx @@ -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 ( @@ -109,7 +92,6 @@ const Course = ({ courseId={courseId} contentToolsEnabled={course.showCalculator || course.notes.enabled} unitId={unitId} - validDates={chatValidDates()} /> )} diff --git a/src/courseware/course/chat/Chat.jsx b/src/courseware/course/chat/Chat.jsx index 76acd7cf16..72739d1c44 100644 --- a/src/courseware/course/chat/Chat.jsx +++ b/src/courseware/course/chat/Chat.jsx @@ -5,6 +5,8 @@ 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, @@ -12,11 +14,11 @@ const Chat = ({ courseId, contentToolsEnabled, unitId, - validDates, }) => { const { activeAttempt, exam, } = useSelector(state => state.specialExams); + const course = useModel('coursewareMeta', courseId); const VERIFIED_MODES = [ 'professional', @@ -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. @@ -63,7 +82,6 @@ Chat.propTypes = { courseId: PropTypes.string.isRequired, contentToolsEnabled: PropTypes.bool.isRequired, unitId: PropTypes.string.isRequired, - validDates: PropTypes.bool.isRequired, }; Chat.defaultProps = { diff --git a/src/courseware/course/chat/Chat.test.jsx b/src/courseware/course/chat/Chat.test.jsx index f2bbb038e0..067565f34b 100644 --- a/src/courseware/course/chat/Chat.test.jsx +++ b/src/courseware/course/chat/Chat.test.jsx @@ -1,5 +1,6 @@ import { BrowserRouter } from 'react-router-dom'; import React from 'react'; +import { Factory } from 'rosie'; import { initializeMockApp, @@ -72,7 +73,6 @@ describe('Chat', () => { enabled courseId={courseId} contentToolsEnabled={false} - validDates /> , { store }, @@ -100,7 +100,6 @@ describe('Chat', () => { enabled courseId={courseId} contentToolsEnabled={false} - validDates /> , { store }, @@ -156,7 +155,6 @@ describe('Chat', () => { enabled={test.enabled} courseId={courseId} contentToolsEnabled={false} - validDates /> , { store }, @@ -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( { enabled courseId={courseId} contentToolsEnabled={false} - validDates={false} /> , { store }, @@ -208,7 +217,6 @@ describe('Chat', () => { enabled courseId={courseId} contentToolsEnabled={false} - validDates /> , { store },