Skip to content
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

feat: receive unitId as prop and forward to learning-assistant backend #37

Merged
merged 2 commits into from
Jan 4, 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
9,884 changes: 4,595 additions & 5,289 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions src/components/Disclosure/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { Hyperlink, Icon } from '@edx/paragon';
import { Chat } from '@edx/paragon/icons';
import { getConfig } from '@edx/frontend-platform/config';

import MessageForm from '../MessageForm';
import './Disclosure.scss';

const Disclosure = ({ courseId }) => (
const Disclosure = ({ children }) => (
<div className="disclosure d-flex flex-column align-items-stretch px-4 py-3">
<h2 className="text-light-100">
Xpert
Expand Down Expand Up @@ -45,12 +44,12 @@ const Disclosure = ({ courseId }) => (
</Hyperlink>
.
</p>
<MessageForm courseId={courseId} />
{children}
</div>
);

Disclosure.propTypes = {
courseId: PropTypes.string.isRequired,
children: PropTypes.node.isRequired,
};

export default Disclosure;
5 changes: 3 additions & 2 deletions src/components/MessageForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
updateCurrentMessage,
} from '../../data/thunks';

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

Expand Down Expand Up @@ -67,6 +67,7 @@ const MessageForm = ({ courseId, shouldAutofocus }) => {

MessageForm.propTypes = {
courseId: PropTypes.string.isRequired,
unitId: PropTypes.string.isRequired,
shouldAutofocus: PropTypes.bool,
};

Expand Down
10 changes: 8 additions & 2 deletions src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Sidebar = ({
courseId,
isOpen,
setIsOpen,
unitId,
}) => {
const {
apiError,
Expand Down Expand Up @@ -82,6 +83,10 @@ const Sidebar = ({
});
};

const getMessageForm = () => (
<MessageForm courseId={courseId} shouldAutofocus unitId={unitId} />
);

const getSidebar = () => (
<div className="h-100 d-flex flex-column justify-content-stretch">
<div className="d-flex flex-column align-items-center p-3">
Expand All @@ -106,7 +111,7 @@ const Sidebar = ({
</div>
)
}
<MessageForm courseId={courseId} shouldAutofocus />
{getMessageForm()}
<div className="d-flex justify-content-start">
<Button
className="clear mx-2 mb-2 border-0"
Expand Down Expand Up @@ -136,7 +141,7 @@ const Sidebar = ({
variant="primary"
invertColors={!disclosureAcknowledged}
/>
{disclosureAcknowledged ? (getSidebar()) : (<Disclosure courseId={courseId} />)}
{disclosureAcknowledged ? (getSidebar()) : (<Disclosure>{getMessageForm()}</Disclosure>)}
</div>
)
);
Expand All @@ -146,6 +151,7 @@ Sidebar.propTypes = {
courseId: PropTypes.string.isRequired,
isOpen: PropTypes.bool.isRequired,
setIsOpen: PropTypes.func.isRequired,
unitId: PropTypes.string.isRequired,
};

export default Sidebar;
14 changes: 9 additions & 5 deletions src/data/api.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { getConfig } from '@edx/frontend-platform';
import { getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

async function fetchChatResponse(courseId, messageList) {
async function fetchChatResponse(courseId, messageList, unitId) {

Check warning on line 4 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L4

Added line #L4 was not covered by tests
const payload = messageList.map((message) => ({
role: message?.role,
content: message?.content,
}));

const url = new URL(
`${getConfig().CHAT_RESPONSE_URL}/${courseId}`,
);
let queryParams = { unitId };
queryParams = snakeCaseObject(queryParams);

Check warning on line 11 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L10-L11

Added lines #L10 - L11 were not covered by tests

let queryString = new URLSearchParams(queryParams);
queryString = queryString.toString();

Check warning on line 14 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L13-L14

Added lines #L13 - L14 were not covered by tests

const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}?${queryString}`);

Check warning on line 16 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L16

Added line #L16 was not covered by tests

const { data } = await getAuthenticatedHttpClient().post(url.href, payload);
return data;
Expand Down
4 changes: 2 additions & 2 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function addChatMessage(role, content, courseId) {
};
}

export function getChatResponse(courseId) {
export function getChatResponse(courseId, unitId) {
return async (dispatch, getState) => {
const { messageList } = getState().learningAssistant;

dispatch(setApiIsLoading(true));
try {
const message = await fetchChatResponse(courseId, messageList);
const message = await fetchChatResponse(courseId, messageList, unitId);
dispatch(setApiIsLoading(false));
dispatch(addChatMessage(message.role, message.content, courseId));
} catch (error) {
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { updateSidebarIsOpen } from '../data/thunks';
import ToggleXpert from '../components/ToggleXpertButton';
import Sidebar from '../components/Sidebar';

const Xpert = ({ courseId, contentToolsEnabled }) => {
const Xpert = ({ courseId, contentToolsEnabled, unitId }) => {
const dispatch = useDispatch();

const {
Expand All @@ -14,7 +14,6 @@ const Xpert = ({ courseId, contentToolsEnabled }) => {
const setSidebarIsOpen = (isOpen) => {
dispatch(updateSidebarIsOpen(isOpen));
};

return (
<div>
<ToggleXpert
Expand All @@ -27,6 +26,7 @@ const Xpert = ({ courseId, contentToolsEnabled }) => {
courseId={courseId}
isOpen={sidebarIsOpen}
setIsOpen={setSidebarIsOpen}
unitId={unitId}
/>
</div>
);
Expand All @@ -35,6 +35,7 @@ const Xpert = ({ courseId, contentToolsEnabled }) => {
Xpert.propTypes = {
courseId: PropTypes.string.isRequired,
contentToolsEnabled: PropTypes.bool.isRequired,
unitId: PropTypes.string.isRequired,
};

export default Xpert;
Loading