Skip to content

Commit ff7366d

Browse files
authored
feat: refactor date visibility (#1298)
1 parent 6def666 commit ff7366d

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/courseware/course/Course.jsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,6 @@ const Course = ({
7070

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

73-
const chatValidDates = () => {
74-
const date = new Date();
75-
const utcDate = date.toISOString();
76-
77-
const enrollmentStartDate = course.enrollmentStart || utcDate;
78-
const startDate = course.start || enrollmentStartDate;
79-
const enrollmentEndDate = course.enrollmentEnd || utcDate;
80-
const endDate = course.end || enrollmentEndDate;
81-
82-
return (
83-
startDate <= enrollmentStartDate
84-
&& enrollmentStartDate <= utcDate
85-
&& utcDate <= enrollmentEndDate
86-
&& enrollmentEndDate <= endDate
87-
);
88-
};
89-
9073
return (
9174
<SidebarProviderComponent courseId={courseId} unitId={unitId}>
9275
<Helmet>
@@ -109,7 +92,6 @@ const Course = ({
10992
courseId={courseId}
11093
contentToolsEnabled={course.showCalculator || course.notes.enabled}
11194
unitId={unitId}
112-
validDates={chatValidDates()}
11395
/>
11496
</>
11597
)}

src/courseware/course/chat/Chat.jsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import PropTypes from 'prop-types';
55
import { Xpert } from '@edx/frontend-lib-learning-assistant';
66
import { injectIntl } from '@edx/frontend-platform/i18n';
77

8+
import { useModel } from '../../../generic/model-store';
9+
810
const Chat = ({
911
enabled,
1012
enrollmentMode,
1113
isStaff,
1214
courseId,
1315
contentToolsEnabled,
1416
unitId,
15-
validDates,
1617
}) => {
1718
const {
1819
activeAttempt, exam,
1920
} = useSelector(state => state.specialExams);
21+
const course = useModel('coursewareMeta', courseId);
2022

2123
const VERIFIED_MODES = [
2224
'professional',
@@ -35,10 +37,27 @@ const Chat = ({
3537
&& [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
3638
);
3739

40+
const validDates = () => {
41+
const date = new Date();
42+
const utcDate = date.toISOString();
43+
44+
const enrollmentStartDate = course.enrollmentStart || utcDate;
45+
const startDate = course.start || enrollmentStartDate;
46+
const enrollmentEndDate = course.enrollmentEnd || utcDate;
47+
const endDate = course.end || enrollmentEndDate;
48+
49+
return (
50+
startDate <= enrollmentStartDate
51+
&& enrollmentStartDate <= utcDate
52+
&& utcDate <= enrollmentEndDate
53+
&& enrollmentEndDate <= endDate
54+
);
55+
};
56+
3857
const shouldDisplayChat = (
3958
enabled
4059
&& (hasVerifiedEnrollment || isStaff) // display only to verified learners or staff
41-
&& validDates
60+
&& validDates()
4261
// it is necessary to check both whether the user is in an exam, and whether or not they are viewing an exam
4362
// this will prevent the learner from interacting with the tool at any point of the exam flow, even at the
4463
// entrance interstitial.
@@ -63,7 +82,6 @@ Chat.propTypes = {
6382
courseId: PropTypes.string.isRequired,
6483
contentToolsEnabled: PropTypes.bool.isRequired,
6584
unitId: PropTypes.string.isRequired,
66-
validDates: PropTypes.bool.isRequired,
6785
};
6886

6987
Chat.defaultProps = {

src/courseware/course/chat/Chat.test.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BrowserRouter } from 'react-router-dom';
22
import React from 'react';
3+
import { Factory } from 'rosie';
34

45
import {
56
initializeMockApp,
@@ -72,7 +73,6 @@ describe('Chat', () => {
7273
enabled
7374
courseId={courseId}
7475
contentToolsEnabled={false}
75-
validDates
7676
/>
7777
</BrowserRouter>,
7878
{ store },
@@ -100,7 +100,6 @@ describe('Chat', () => {
100100
enabled
101101
courseId={courseId}
102102
contentToolsEnabled={false}
103-
validDates
104103
/>
105104
</BrowserRouter>,
106105
{ store },
@@ -156,7 +155,6 @@ describe('Chat', () => {
156155
enabled={test.enabled}
157156
courseId={courseId}
158157
contentToolsEnabled={false}
159-
validDates
160158
/>
161159
</BrowserRouter>,
162160
{ store },
@@ -173,6 +171,18 @@ describe('Chat', () => {
173171
});
174172

175173
it('if course end date has passed, component should not be visible', async () => {
174+
store = await initializeTestStore({
175+
specialExams: {
176+
activeAttempt: {
177+
attempt_id: 1,
178+
},
179+
},
180+
courseMetadata: Factory.build('courseMetadata', {
181+
start: '2014-02-03T05:00:00Z',
182+
end: '2014-02-05T05:00:00Z',
183+
}),
184+
});
185+
176186
render(
177187
<BrowserRouter>
178188
<Chat
@@ -181,7 +191,6 @@ describe('Chat', () => {
181191
enabled
182192
courseId={courseId}
183193
contentToolsEnabled={false}
184-
validDates={false}
185194
/>
186195
</BrowserRouter>,
187196
{ store },
@@ -208,7 +217,6 @@ describe('Chat', () => {
208217
enabled
209218
courseId={courseId}
210219
contentToolsEnabled={false}
211-
validDates
212220
/>
213221
</BrowserRouter>,
214222
{ store },

0 commit comments

Comments
 (0)