Skip to content

Commit 193a184

Browse files
feat: [VAN-1000] - Set weekly goal through welcome email via query param (#956)
1 parent 3e76f7a commit 193a184

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/course-home/outline-tab/OutlineTab.test.jsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @jest-environment jsdom
33
*/
44
import React from 'react';
5+
import { MemoryRouter } from 'react-router-dom';
56
import { Factory } from 'rosie';
67
import { getConfig } from '@edx/frontend-platform';
78
import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/analytics';
@@ -51,9 +52,14 @@ describe('Outline Tab', () => {
5152
axiosMock.onGet(outlineUrl).reply(200, outlineTabData);
5253
}
5354

54-
async function fetchAndRender() {
55+
async function fetchAndRender(path = '') {
5556
await executeThunk(thunks.fetchOutlineTab(courseId), store.dispatch);
56-
await act(async () => render(<OutlineTab />, { store }));
57+
await act(async () => render(
58+
<MemoryRouter initialEntries={[path]}>
59+
<OutlineTab />
60+
</MemoryRouter>,
61+
{ store },
62+
));
5763
}
5864

5965
beforeEach(async () => {
@@ -338,6 +344,27 @@ describe('Outline Tab', () => {
338344
expect(spy).toHaveBeenCalledTimes(0);
339345
});
340346

347+
it('post goal via query param', async () => {
348+
setTabData({
349+
course_goals: {
350+
weekly_learning_goal_enabled: true,
351+
},
352+
});
353+
const spy = jest.spyOn(thunks, 'saveWeeklyLearningGoal');
354+
sendTrackEvent.mockClear();
355+
356+
await fetchAndRender('http://localhost/?weekly_goal=3');
357+
expect(spy).toHaveBeenCalledTimes(1);
358+
expect(sendTrackEvent).toHaveBeenCalledWith('edx.ui.lms.goal.days-per-week.changed', {
359+
org_key: 'edX',
360+
courserun_key: courseId,
361+
is_staff: false,
362+
num_days: 3,
363+
reminder_selected: true,
364+
triggeredFromEmail: true,
365+
});
366+
});
367+
341368
describe('weekly learning goal is not set', () => {
342369
beforeEach(async () => {
343370
setTabData({

src/course-home/outline-tab/widgets/WeeklyLearningGoalCard.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
2+
import { useLocation } from 'react-router-dom';
23
import PropTypes from 'prop-types';
34

45
import { Form, Card, Icon } from '@edx/paragon';
6+
import { history } from '@edx/frontend-platform';
57
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
68
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
79
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
@@ -32,8 +34,9 @@ function WeeklyLearningGoalCard({
3234
const [daysPerWeekGoal, setDaysPerWeekGoal] = useState(daysPerWeek);
3335
// eslint-disable-next-line react/prop-types
3436
const [isGetReminderSelected, setGetReminderSelected] = useState(subscribedToReminders);
37+
const location = useLocation();
3538

36-
function handleSelect(days) {
39+
function handleSelect(days, triggeredFromEmail = false) {
3740
// Set the subscription button if this is the first time selecting a goal
3841
const selectReminders = daysPerWeekGoal === null ? true : isGetReminderSelected;
3942
setGetReminderSelected(selectReminders);
@@ -46,6 +49,7 @@ function WeeklyLearningGoalCard({
4649
is_staff: administrator,
4750
num_days: days,
4851
reminder_selected: selectReminders,
52+
triggeredFromEmail,
4953
});
5054
}
5155
}
@@ -65,6 +69,21 @@ function WeeklyLearningGoalCard({
6569
}
6670
}
6771

72+
useEffect(() => {
73+
const currentParams = new URLSearchParams(location.search);
74+
const weeklyGoal = Number(currentParams.get('weekly_goal'));
75+
if ([1, 3, 5].includes(weeklyGoal)) {
76+
handleSelect(weeklyGoal, true);
77+
78+
// Deleting the weekly_goal query param as it only needs to be set once
79+
// whenever passed in query params.
80+
currentParams.delete('weekly_goal');
81+
history.replace({
82+
search: currentParams.toString(),
83+
});
84+
}
85+
}, [location.search]);
86+
6887
return (
6988
<Card
7089
id="courseHome-weeklyLearningGoal"

0 commit comments

Comments
 (0)