Skip to content

Commit b694710

Browse files
committed
Properly handle language in participant interview
And ignore the reviewer's fixes #1251 Only the participant's interview tracks language changes in the paradata, so the `useEffect` hook is moved from the `Survey` component to `SurveyParticipant`. This will affect both respondent and interviewer. Reviewer's language changes will be ignored. Set the language when the interview is activated, ie when first activating an interview, or when the interview is opened again, but the language is different from the previous one, the language of the interview will be updated to the current interface's.
1 parent ccae070 commit b694710

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

packages/evolution-frontend/src/actions/Survey.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const fetch = async (url, opts) => {
2828
return await fetchRetry(url, Object.assign({ retry: { retries: 4 }, ...opts }));
2929
};
3030

31+
import i18n from '../config/i18n.config';
3132
import { _isBlank } from 'chaire-lib-common/lib/utils/LodashExtensions';
3233
import * as surveyHelper from 'evolution-common/lib/utils/helpers';
3334
import { prepareSectionWidgets } from './utils';
@@ -768,6 +769,13 @@ export const startSetInterview = (
768769
if (existingBrowserUa !== newBrowserUa) {
769770
valuesByPath['response._browser'] = browserTechData;
770771
}
772+
773+
// Set or update the current language if required
774+
const previousLanguage = _get(interview, 'response._language', null);
775+
const currentLanguage = i18n.language;
776+
if (_isBlank(previousLanguage) || previousLanguage !== currentLanguage) {
777+
valuesByPath['response._language'] = currentLanguage;
778+
}
771779
// Set the interview in the state first
772780
dispatch(setInterviewState(interview));
773781

packages/evolution-frontend/src/components/pageParts/Survey.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ const Survey: React.FC<SurveyProps> = (props: SurveyProps) => {
7171
}
7272
}, [i18n.language]);
7373

74-
React.useEffect(() => {
75-
const languageChange = (language) => {
76-
props.startUpdateInterview({ userAction: { type: 'languageChange', language } });
77-
};
78-
i18n.on('languageChanged', languageChange);
79-
return () => {
80-
i18n.off('languageChanged', languageChange);
81-
};
82-
}, [props.startUpdateInterview]);
83-
8474
const onChangeSection = (
8575
targetSection: string,
8676
activeSection: string,

packages/evolution-frontend/src/components/pages/SurveyParticipant.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ThunkDispatch } from 'redux-thunk';
3131
import { SurveyAction } from '../../store/survey';
3232
import Survey from '../pageParts/Survey';
3333
import { SurveyContext } from '../../contexts/SurveyContext';
34+
import { useTranslation } from 'react-i18next';
3435

3536
type StartSetInterview = (
3637
activeSection: string | undefined,
@@ -46,6 +47,7 @@ const SurveyParticipant: React.FC = () => {
4647
const navigate = useNavigate();
4748
const { sectionShortname: pathSectionShortname, uuid: surveyUuid } = useParams();
4849
const { sections } = useContext(SurveyContext);
50+
const { i18n } = useTranslation();
4951

5052
const { state: interviewContext } = React.useContext(InterviewContext);
5153

@@ -87,6 +89,16 @@ const SurveyParticipant: React.FC = () => {
8789
);
8890
}, [surveyUuid, dispatch]); // Re-run when survey uuid changes
8991

92+
React.useEffect(() => {
93+
const languageChange = (language) => {
94+
startUpdateInterviewAction({ userAction: { type: 'languageChange', language } });
95+
};
96+
i18n.on('languageChanged', languageChange);
97+
return () => {
98+
i18n.off('languageChanged', languageChange);
99+
};
100+
}, [startUpdateInterviewAction]);
101+
90102
// FIXME See if we can use react Suspense instead of this logic for the loading page (https://react.dev/reference/react/Suspense)
91103
if (!interviewLoaded || !interview || !interview.sectionLoaded) {
92104
surveyHelperNew.devLog('%c rendering empty survey', 'background: rgba(0,0,0,0.1);');

0 commit comments

Comments
 (0)