Skip to content

Commit

Permalink
fix: add upgrade eligibility gate for refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Dec 16, 2024
1 parent d366bc0 commit 5d0fba3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/components/MessageForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
getChatResponse,
updateCurrentMessage,
} from '../../data/thunks';
import { useCourseUpgrade } from '../../hooks';
import { usePromptExperimentDecision } from '../../experiments';

import './MessageForm.scss';

const MessageForm = ({ courseId, shouldAutofocus, unitId }) => {
const { upgradeable } = useCourseUpgrade();

const { apiIsLoading, currentMessage, apiError } = useSelector(state => state.learningAssistant);
const dispatch = useDispatch();
const inputRef = useRef();
Expand All @@ -35,7 +38,7 @@ const MessageForm = ({ courseId, shouldAutofocus, unitId }) => {
if (currentMessage) {
dispatch(acknowledgeDisclosure(true));
dispatch(addChatMessage('user', currentMessage, courseId, promptExperimentVariationKey));
dispatch(getChatResponse(courseId, unitId, promptExperimentVariationKey));
dispatch(getChatResponse(courseId, unitId, upgradeable, promptExperimentVariationKey));
}
};

Expand Down
12 changes: 11 additions & 1 deletion src/components/MessageForm/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import {
acknowledgeDisclosure,
addChatMessage,
getChatResponse,
getLearningAssistantChatSummary,
updateCurrentMessage,
} from '../../data/thunks';
import { useCourseUpgrade } from '../../hooks';

import MessageForm from '.';

jest.mock('../../hooks', () => ({
useCourseUpgrade: jest.fn(),
}));

jest.mock('../../utils/surveyMonkey', () => ({
showControlSurvey: jest.fn(),
showVariationSurvey: jest.fn(),
Expand Down Expand Up @@ -81,6 +87,9 @@ describe('<MessageForm />', () => {
beforeEach(() => {
jest.resetAllMocks();
usePromptExperimentDecision.mockReturnValue([]);
useCourseUpgrade.mockReturnValue({
upgradeable: true,
});
});

describe('when rendered', () => {
Expand Down Expand Up @@ -137,7 +146,7 @@ describe('<MessageForm />', () => {

expect(acknowledgeDisclosure).toHaveBeenCalledWith(true);
expect(addChatMessage).toHaveBeenCalledWith('user', currentMessage, defaultProps.courseId, undefined);
expect(getChatResponse).toHaveBeenCalledWith(defaultProps.courseId, defaultProps.unitId, undefined);
expect(getChatResponse).toHaveBeenCalledWith(defaultProps.courseId, defaultProps.unitId, true, undefined);
expect(mockDispatch).toHaveBeenCalledTimes(3);
});

Expand Down Expand Up @@ -187,6 +196,7 @@ describe('<MessageForm />', () => {
expect(getChatResponse).toHaveBeenCalledWith(
defaultProps.courseId,
defaultProps.unitId,
true,
OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS.UPDATED_PROMPT,
);
expect(mockDispatch).toHaveBeenCalledTimes(3);
Expand Down
8 changes: 4 additions & 4 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function addChatMessage(role, content, courseId, promptExperimentVariatio
};
}

export function getChatResponse(courseId, unitId, promptExperimentVariationKey = undefined) {
export function getChatResponse(courseId, unitId, upgradeable, promptExperimentVariationKey = undefined) {
return async (dispatch, getState) => {
const { userId } = getAuthenticatedUser();
const { messageList } = getState().learningAssistant;
Expand All @@ -69,9 +69,9 @@ export function getChatResponse(courseId, unitId, promptExperimentVariationKey =
const customQueryParams = promptExperimentVariationKey ? { responseVariation: promptExperimentVariationKey } : {};
const message = await fetchChatResponse(courseId, messageList, unitId, customQueryParams);

// Refresh chat summary only on the first message so we can tell if the user has initiated an audit trial
// NOTE: This is a bit of a hacky solution that may be refined later
if (messageList.length === 1) {
// Refresh chat summary only on the first message for an upgrade eligible user
// so we can tell if the user has just initiated an audit trial
if (messageList.length === 1 && upgradeable) {
// eslint-disable-next-line no-use-before-define
dispatch(getLearningAssistantChatSummary(courseId));
}
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use-course-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function useCourseUpgrade() {
auditTrialDaysRemaining = Math.ceil((auditTrialExpirationDate - Date.now()) / millisecondsInOneDay);

auditTrialExpired = auditTrialDaysRemaining < 0;
// console.log({ upgradeUrl }, { auditTrialDaysRemaining });
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Xpert = ({
};

useEffect(() => {
dispatch(getLearningAssistantChatSummary(courseId));
dispatch(getLearningAssistantChatSummary(courseId, isUpgradeEligible));
}, [dispatch, courseId]);

return isEnabled ? (
Expand Down

0 comments on commit 5d0fba3

Please sign in to comment.