Skip to content

Commit

Permalink
feat: update chat visibility (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Feb 22, 2024
1 parent 8335dec commit 6def666
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
19 changes: 18 additions & 1 deletion src/courseware/course/Course.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ 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 @@ -92,7 +109,7 @@ const Course = ({
courseId={courseId}
contentToolsEnabled={course.showCalculator || course.notes.enabled}
unitId={unitId}
endDate={course.end ? course.end : ''}
validDates={chatValidDates()}
/>
</>
)}
Expand Down
13 changes: 3 additions & 10 deletions src/courseware/course/chat/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Chat = ({
courseId,
contentToolsEnabled,
unitId,
endDate,
validDates,
}) => {
const {
activeAttempt, exam,
Expand All @@ -35,17 +35,10 @@ const Chat = ({
&& [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
);

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

return endDate ? utcDate > endDate : false; // evaluate if end date has passed only if course has end date
};

const shouldDisplayChat = (
enabled
&& (hasVerifiedEnrollment || isStaff) // display only to verified learners or staff
&& !endDatePassed()
&& 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 @@ -70,7 +63,7 @@ Chat.propTypes = {
courseId: PropTypes.string.isRequired,
contentToolsEnabled: PropTypes.bool.isRequired,
unitId: PropTypes.string.isRequired,
endDate: PropTypes.string.isRequired,
validDates: PropTypes.bool.isRequired,
};

Chat.defaultProps = {
Expand Down
30 changes: 5 additions & 25 deletions src/courseware/course/chat/Chat.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const enabledModes = [
'paid-executive-education', 'paid-bootcamp',
];
const disabledModes = [null, undefined, 'xyz', 'audit', 'honor', 'unpaid-executive-education', 'unpaid-bootcamp'];
const currentTime = new Date();

describe('Chat', () => {
let store;
Expand Down Expand Up @@ -73,7 +72,7 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
endDate={new Date(currentTime.getTime() + 10 * 60000).toISOString()}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
endDate={new Date(currentTime.getTime() + 10 * 60000).toISOString()}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down Expand Up @@ -157,7 +156,7 @@ describe('Chat', () => {
enabled={test.enabled}
courseId={courseId}
contentToolsEnabled={false}
endDate={new Date(currentTime.getTime() + 10 * 60000).toISOString()}
validDates
/>
</BrowserRouter>,
{ store },
Expand All @@ -182,7 +181,7 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
endDate={new Date(currentTime.getTime() - 10 * 60000).toISOString()}
validDates={false}
/>
</BrowserRouter>,
{ store },
Expand All @@ -192,25 +191,6 @@ describe('Chat', () => {
expect(chat).not.toBeInTheDocument();
});

it('if course has no end date, component should be visible', async () => {
render(
<BrowserRouter>
<Chat
enrollmentMode="verified"
isStaff
enabled
courseId={courseId}
contentToolsEnabled={false}
endDate={null}
/>
</BrowserRouter>,
{ store },
);

const chat = screen.queryByTestId(mockXpertTestId);
expect(chat).toBeInTheDocument();
});

it('if learner has active exam attempt, component should not be visible', async () => {
store = await initializeTestStore({
specialExams: {
Expand All @@ -228,7 +208,7 @@ describe('Chat', () => {
enabled
courseId={courseId}
contentToolsEnabled={false}
endDate={null}
validDates
/>
</BrowserRouter>,
{ store },
Expand Down

0 comments on commit 6def666

Please sign in to comment.