Skip to content

Commit

Permalink
feat: add segment events (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Aug 16, 2023
1 parent d615f84 commit 0f3aabd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"react-router": "5.3.4",
"react-router-dom": "5.3.4",
"redux": "4.2.1",
"regenerator-runtime": "0.13.11"
"regenerator-runtime": "0.13.11",
"uuid": "9.0.0"
},
"peerDependencies": {
"@reduxjs/toolkit": "^1.5.1"
Expand Down
10 changes: 9 additions & 1 deletion src/components/ToggleXpertButton/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { ChevronRight } from 'react-feather';
import PropTypes from 'prop-types';
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import { ReactComponent as NewXeySvg } from '../../assets/new_xey.svg';
import './index.css';

const ToggleXpert = ({ isOpen, setIsOpen }) => {
const ToggleXpert = ({ isOpen, setIsOpen, courseId }) => {
const handleClick = () => {
// log event if the tool is opened
if (!isOpen) {
sendTrackEvent('edx.ui.lms.learning_assistant.launch', {
course_id: courseId,
});
}
setIsOpen(!isOpen);
};

Expand Down Expand Up @@ -39,6 +46,7 @@ const ToggleXpert = ({ isOpen, setIsOpen }) => {
ToggleXpert.propTypes = {
isOpen: PropTypes.bool.isRequired,
setIsOpen: PropTypes.func.isRequired,
courseId: PropTypes.string.isRequired,
};

export default ToggleXpert;
2 changes: 2 additions & 0 deletions src/data/slice.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable no-param-reassign */
import { createSlice } from '@reduxjs/toolkit';
import { v4 as uuidv4 } from 'uuid';

export const learningAssistantSlice = createSlice({
name: 'learning-assistant',
initialState: {
currentMessage: '',
messageList: [],
apiError: false,
conversationId: uuidv4(),
},
reducers: {
setCurrentMessage: (state, { payload }) => {
Expand Down
9 changes: 8 additions & 1 deletion src/data/thunks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import fetchChatResponse from './api';
import {
setCurrentMessage,
Expand All @@ -8,14 +9,20 @@ import {

export function addChatMessage(role, content) {
return (dispatch, getState) => {
const { messageList } = getState().learningAssistant;
const { messageList, conversationId } = getState().learningAssistant;
const message = {
role,
content,
timestamp: new Date(),
};
const updatedMessageList = [...messageList, message];
dispatch(setMessageList({ messageList: updatedMessageList }));
sendTrackEvent('edx.ui.lms.learning_assistant.message', {
id: conversationId,
timestamp: message.timestamp,
role: message.role,
content: message.content,
});
};
}

Expand Down
13 changes: 9 additions & 4 deletions src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import PropTypes from 'prop-types';
import { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import ToggleXpert from '../components/ToggleXpertButton';
import Sidebar from '../components/Sidebar';
import { getChatResponse } from '../data/thunks';

const Xpert = () => {
const Xpert = ({ courseId }) => {
const { messageList } = useSelector(state => state.learningAssistant);
const [sidebarIsOpen, setSidebarIsOpen] = useState(false);

useEffect(() => {
if (messageList[messageList.length - 1].role === 'user') {
getChatResponse();
getChatResponse(courseId);
}
}, [messageList]);
}, [messageList, courseId]);

return (
<div>
<ToggleXpert isOpen={sidebarIsOpen} setIsOpen={setSidebarIsOpen} />
<ToggleXpert isOpen={sidebarIsOpen} setIsOpen={setSidebarIsOpen} courseId={courseId} />
<Sidebar
isOpen={sidebarIsOpen}
setIsOpen={setSidebarIsOpen}
Expand All @@ -25,4 +26,8 @@ const Xpert = () => {
);
};

Xpert.propTypes = {
courseId: PropTypes.string.isRequired,
};

export default Xpert;

0 comments on commit 0f3aabd

Please sign in to comment.