Skip to content

feat(AU-1900): Connect FeedbackWidget with backend services #1325

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

Merged
merged 2 commits into from
Mar 19, 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
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
NODE_ENV='development'

ACCESS_TOKEN_COOKIE_NAME='edx-jwt-cookie-header-payload'
AI_TRANSLATIONS_URL='http://localhost:18760'
BASE_URL='http://localhost:2000'
CONTACT_URL='http://localhost:18000/contact'
CREDENTIALS_BASE_URL='http://localhost:18150'
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
NODE_ENV='test'

ACCESS_TOKEN_COOKIE_NAME='edx-jwt-cookie-header-payload'
AI_TRANSLATIONS_URL='http://localhost:18760'
BASE_URL='http://localhost:2000'
CONTACT_URL='http://localhost:18000/contact'
CREDENTIALS_BASE_URL='http://localhost:18150'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,55 @@

exports[`<FeedbackWidget /> renders 1`] = `
<div
className="sequence w-100"
className="sequence-container d-inline-flex flex-row"
>
<div
className="ml-4 mr-2"
className="sequence w-100"
>
<ActionRow>
Rate this page translation
<Spacer />
<div>
<IconButton
alt="positive-feedback"
className="m-1"
iconAs="Icon"
id="positive-feedback-button"
onClick={[MockFunction sendFeedback]}
src="ThumbUpOutline"
variant="secondary"
/>
<IconButton
alt="negative-feedback"
className="mr-2"
iconAs="Icon"
id="negative-feedback-button"
onClick={[MockFunction sendFeedback]}
src="ThumbDownOffAlt"
variant="secondary"
/>
</div>
<div
className="mb-1 text-light action-row-divider"
>
|
</div>
<div>
<IconButton
alt="close-feedback"
className="ml-1 mr-2 float-right"
iconAs="Icon"
id="close-feedback-button"
onClick={[MockFunction closeFeedbackWidget]}
src="Close"
variant="secondary"
/>
</div>
</ActionRow>
<div
className="ml-4 mr-2"
>
<ActionRow>
Rate this page translation
<Spacer />
<div>
<IconButton
alt="positive-feedback"
className="m-1"
iconAs="Icon"
id="positive-feedback-button"
onClick={[Function]}
src="ThumbUpOutline"
variant="secondary"
/>
<IconButton
alt="negative-feedback"
className="mr-2"
iconAs="Icon"
id="negative-feedback-button"
onClick={[Function]}
src="ThumbDownOffAlt"
variant="secondary"
/>
</div>
<div
className="mb-1 text-light action-row-divider"
>
|
</div>
<div>
<IconButton
alt="close-feedback"
className="ml-1 mr-2 float-right"
iconAs="Icon"
id="close-feedback-button"
onClick={[MockFunction closeFeedbackWidget]}
src="Close"
variant="secondary"
/>
</div>
</ActionRow>
</div>
</div>
</div>
`;
37 changes: 37 additions & 0 deletions plugins/UnitTranslationPlugin/feedback-widget/data/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from 'axios';

import { getConfig, camelCaseObject } from '@edx/frontend-platform';

export async function getTranslationFeedback({
courseId,
translationLanguage,
unitId,
userId,
}) {
const aiTranslationsUrl = getConfig().AI_TRANSLATIONS_URL;
const courseIdEncoded = encodeURIComponent(courseId);
const unitIdEncoded = encodeURIComponent(unitId);
const paramsString = `translation_language=${translationLanguage}&course_id=${courseIdEncoded}&unit_id=${unitIdEncoded}&user_id=${userId}`;
const fetchFeedbackUrl = new URL(`${aiTranslationsUrl}/api/v1/whole-course-translation-feedback?${paramsString}`);
const { data } = await axios.get(fetchFeedbackUrl);
return camelCaseObject(data);
}

export async function createTranslationFeedback({
courseId,
feedbackValue,
translationLanguage,
unitId,
userId,
}) {
const aiTranslationsUrl = getConfig().AI_TRANSLATIONS_URL;
const createFeedbackUrl = new URL(`${aiTranslationsUrl}/api/v1/whole-course-translation-feedback/`);
const { data } = await axios.post(createFeedbackUrl, {
course_id: courseId,
feedback_value: feedbackValue,
translation_language: translationLanguage,
unit_id: unitId,
user_id: userId,
});
return camelCaseObject(data);
}
125 changes: 67 additions & 58 deletions plugins/UnitTranslationPlugin/feedback-widget/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';

import { useIntl } from '@edx/frontend-platform/i18n';
import { ActionRow, IconButton, Icon } from '@edx/paragon';
import { Close, ThumbUpOutline, ThumbDownOffAlt } from '@edx/paragon/icons';
Expand All @@ -10,7 +11,7 @@ import useFeedbackWidget from './useFeedbackWidget';

const FeedbackWidget = ({
courseId,
languageCode,
translationLanguage,
unitId,
userId,
}) => {
Expand All @@ -22,74 +23,82 @@ const FeedbackWidget = ({
showGratitudeText,
} = useFeedbackWidget({
courseId,
languageCode,
translationLanguage,
unitId,
userId,
});
const onThumbsUpClick = useCallback(() => {
sendFeedback(true);
}, [sendFeedback]);
const onThumbsDownClick = useCallback(() => {
sendFeedback(false);
}, [sendFeedback]);
return (
(showFeedbackWidget || showGratitudeText) && (
<div className="sequence w-100">
{
showFeedbackWidget && (
<div className="ml-4 mr-2">
<ActionRow>
{formatMessage(messages.rateTranslationText)}
<ActionRow.Spacer />
<div>
<IconButton
src={ThumbUpOutline}
iconAs={Icon}
alt="positive-feedback"
onClick={sendFeedback}
variant="secondary"
className="m-1"
id="positive-feedback-button"
/>
<IconButton
src={ThumbDownOffAlt}
iconAs={Icon}
alt="negative-feedback"
onClick={sendFeedback}
variant="secondary"
className="mr-2"
id="negative-feedback-button"
/>
</div>
<div className="mb-1 text-light action-row-divider">
|
</div>
<div>
<IconButton
src={Close}
iconAs={Icon}
alt="close-feedback"
onClick={closeFeedbackWidget}
variant="secondary"
className="ml-1 mr-2 float-right"
id="close-feedback-button"
/>
</div>
</ActionRow>
</div>
)
}
{
showGratitudeText && (
<div className="ml-4 mr-4">
<ActionRow className="m-2 justify-content-center">
{formatMessage(messages.gratitudeText)}
</ActionRow>
</div>
)
}
<div className="sequence-container d-inline-flex flex-row">
<div className="sequence w-100">
{
showFeedbackWidget && (
<div className="ml-4 mr-2">
<ActionRow>
{formatMessage(messages.rateTranslationText)}
<ActionRow.Spacer />
<div>
<IconButton
src={ThumbUpOutline}
iconAs={Icon}
alt="positive-feedback"
onClick={onThumbsUpClick}
variant="secondary"
className="m-1"
id="positive-feedback-button"
/>
<IconButton
src={ThumbDownOffAlt}
iconAs={Icon}
alt="negative-feedback"
onClick={onThumbsDownClick}
variant="secondary"
className="mr-2"
id="negative-feedback-button"
/>
</div>
<div className="mb-1 text-light action-row-divider">
|
</div>
<div>
<IconButton
src={Close}
iconAs={Icon}
alt="close-feedback"
onClick={closeFeedbackWidget}
variant="secondary"
className="ml-1 mr-2 float-right"
id="close-feedback-button"
/>
</div>
</ActionRow>
</div>
)
}
{
showGratitudeText && (
<div className="ml-4 mr-4">
<ActionRow className="m-2 justify-content-center">
{formatMessage(messages.gratitudeText)}
</ActionRow>
</div>
)
}
</div>
</div>
)
);
};

FeedbackWidget.propTypes = {
courseId: PropTypes.string.isRequired,
languageCode: PropTypes.string.isRequired,
translationLanguage: PropTypes.string.isRequired,
userId: PropTypes.string.isRequired,
unitId: PropTypes.string.isRequired,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jest.mock('@edx/frontend-platform/i18n', () => {
describe('<FeedbackWidget />', () => {
const props = {
courseId: 'course-v1:edX+DemoX+Demo_Course',
languageCode: 'es',
translationLanguage: 'es',
unitId: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@37b72b3915204b70acb00c55b604b563',
userId: '123',
};
Expand Down
47 changes: 41 additions & 6 deletions plugins/UnitTranslationPlugin/feedback-widget/useFeedbackWidget.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

const useFeedbackWidget = () => {
const [showFeedbackWidget, setShowFeedbackWidget] = useState(true);
import { createTranslationFeedback, getTranslationFeedback } from './data/api';

const useFeedbackWidget = ({
courseId,
translationLanguage,
unitId,
userId,
}) => {
const [showFeedbackWidget, setShowFeedbackWidget] = useState(false);
const [showGratitudeText, setShowGratitudeText] = useState(false);

const closeFeedbackWidget = useCallback(() => {
Expand All @@ -12,18 +19,46 @@ const useFeedbackWidget = () => {
setShowFeedbackWidget(true);
}, [setShowFeedbackWidget]);

useEffect(async () => {
const translationFeedback = await getTranslationFeedback({
courseId,
translationLanguage,
unitId,
userId,
});
setShowFeedbackWidget(!translationFeedback);
}, [
courseId,
translationLanguage,
unitId,
userId,
]);

const openGratitudeText = useCallback(() => {
setShowGratitudeText(true);
setTimeout(() => {
setShowGratitudeText(false);
}, 3000);
}, [setShowGratitudeText]);

const sendFeedback = useCallback(() => {
// Create feedback
const sendFeedback = useCallback(async (feedbackValue) => {
await createTranslationFeedback({
courseId,
feedbackValue,
translationLanguage,
unitId,
userId,
});
closeFeedbackWidget();
openGratitudeText();
}, [closeFeedbackWidget, openGratitudeText]);
}, [
courseId,
translationLanguage,
unitId,
userId,
closeFeedbackWidget,
openGratitudeText,
]);

return {
closeFeedbackWidget,
Expand Down
Loading
Loading