Skip to content

Commit

Permalink
Lk/translation only for verified (openedx#1355)
Browse files Browse the repository at this point in the history
* chore: update verified mode logic

* chore: add is staff logic

* chore: add test
  • Loading branch information
leangseu-edx authored Apr 15, 2024
1 parent 2347ce8 commit 7652fa4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
14 changes: 14 additions & 0 deletions plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ exports[`<UnitTranslationPlugin /> render TranslationSelection when translation
unitId="unitId"
/>
`;

exports[`<UnitTranslationPlugin /> render translation when the user is staff 1`] = `
<TranslationSelection
availableLanguages={
Array [
"en",
]
}
courseId="courseId"
id="id"
language="en"
unitId="unitId"
/>
`;
15 changes: 10 additions & 5 deletions plugins/UnitTranslationPlugin/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@ import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import { useModel } from '@src/generic/model-store';
import { VERIFIED_MODES } from '@src/constants';

import TranslationSelection from './translation-selection';
import { fetchTranslationConfig } from './data/api';

const UnitTranslationPlugin = ({ id, courseId, unitId }) => {
const { language } = useModel('coursewareMeta', courseId);
const { enrollmentMode } = useModel('courseHomeMeta', courseId);
const { language, enrollmentMode } = useModel('coursewareMeta', courseId);
const { isStaff } = useModel('courseHomeMeta', courseId);
const [translationConfig, setTranslationConfig] = useState({
enabled: false,
availableLanguages: [],
});

const verifiedMode = enrollmentMode === 'verified';
const hasVerifiedEnrollment = isStaff || (
enrollmentMode !== null
&& enrollmentMode !== undefined
&& VERIFIED_MODES.includes(enrollmentMode)
);

useEffect(() => {
if (verifiedMode) {
if (hasVerifiedEnrollment) {
fetchTranslationConfig(courseId).then(setTranslationConfig);
}
}, []);

const { enabled, availableLanguages } = translationConfig;

if (!verifiedMode || !enabled || !language || !availableLanguages.length) {
if (!hasVerifiedEnrollment || !enabled || !language || !availableLanguages.length) {
return null;
}

Expand Down
48 changes: 33 additions & 15 deletions plugins/UnitTranslationPlugin/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ describe('<UnitTranslationPlugin />', () => {
courseId: 'courseId',
unitId: 'unitId',
};
const mockInitialState = ({ enabled = true, availableLanguages = ['en'] }) => {
useState.mockReturnValue([{ enabled, availableLanguages }, jest.fn()]);
const mockInitialState = ({
enabled = true,
availableLanguages = ['en'],
language = 'en',
enrollmentMode = 'verified',
isStaff = false,
}) => {
useState.mockReturnValueOnce([{ enabled, availableLanguages }, jest.fn()]);

when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language, enrollmentMode });

when(useModel)
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ isStaff });
};
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValue({ language: 'en' })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValue({ enrollmentMode: 'verified' });

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -53,27 +62,36 @@ describe('<UnitTranslationPlugin />', () => {
});

it('render empty when course language has not been set', () => {
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: null });
mockInitialState({});
mockInitialState({
language: null,
});

const wrapper = shallow(<UnitTranslationPlugin {...props} />);

expect(wrapper.isEmptyRender()).toBe(true);
});

it('render empty when student is enroll as verified', () => {
when(useModel)
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ enrollmentMode: 'audit' });
mockInitialState({});
mockInitialState({
enrollmentMode: 'audit',
});

const wrapper = shallow(<UnitTranslationPlugin {...props} />);

expect(wrapper.isEmptyRender()).toBe(true);
});

it('render translation when the user is staff', () => {
mockInitialState({
enrollmentMode: 'audit',
isStaff: true,
});

const wrapper = shallow(<UnitTranslationPlugin {...props} />);

expect(wrapper.snapshot).toMatchSnapshot();
});

it('render TranslationSelection when translation is enabled and language is available', () => {
mockInitialState({});

Expand Down
11 changes: 11 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ export const REDIRECT_MODES = {
HOME_REDIRECT: 'home-redirect',
SURVEY_REDIRECT: 'survey-redirect',
};

export const VERIFIED_MODES = [
'professional',
'verified',
'no-id-professional',
'credit',
'masters',
'executive-education',
'paid-executive-education',
'paid-bootcamp',
];
14 changes: 2 additions & 12 deletions src/courseware/course/chat/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import { Xpert } from '@edx/frontend-lib-learning-assistant';
import { injectIntl } from '@edx/frontend-platform/i18n';

import { VERIFIED_MODES } from '@src/constants';
import { useModel } from '../../../generic/model-store';

const Chat = ({
Expand All @@ -20,21 +21,10 @@ const Chat = ({
} = useSelector(state => state.specialExams);
const course = useModel('coursewareMeta', courseId);

const VERIFIED_MODES = [
'professional',
'verified',
'no-id-professional',
'credit',
'masters',
'executive-education',
'paid-executive-education',
'paid-bootcamp',
];

const hasVerifiedEnrollment = (
enrollmentMode !== null
&& enrollmentMode !== undefined
&& [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
&& VERIFIED_MODES.includes(enrollmentMode)
);

const validDates = () => {
Expand Down

0 comments on commit 7652fa4

Please sign in to comment.