Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update chat visibility #1297

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since course comes from the Redux store, I think we could read in the course with the useModel hook and compute this data within the Chat component to avoid having Chat specific logic in the Course component. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, technically, this bit of code is untested. If you move this into Chat, you'll have to add this data to the mock Redux store for it to work properly in your test. That would get you better test coverage, but would be more work.

If you want to get this out and fast-follow with the refactor/testing changes, I'm good with that. But let me know if you don't think this is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to get this out and then work on updating the tests/refactor afterwards. Once I merge this I'll start that work.

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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd rename this test, because it's testing something less specific now.

/>
</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
Loading