diff --git a/src/components/Sidebar/index.jsx b/src/components/Sidebar/index.jsx index 097397a3..718b60d8 100644 --- a/src/components/Sidebar/index.jsx +++ b/src/components/Sidebar/index.jsx @@ -20,7 +20,8 @@ const Sidebar = ({ isOpen, setIsOpen, unitId, -}) => { + auditTrialNotExpired + &, > { const { apiError, disclosureAcknowledged, @@ -98,7 +99,10 @@ const Sidebar = ({ ) } - {getMessageForm()} + { + auditTrialNotExpired + && getMessageForm() + } ); diff --git a/src/data/api.js b/src/data/api.js index 4b1411cb..8a9033b8 100644 --- a/src/data/api.js +++ b/src/data/api.js @@ -30,30 +30,16 @@ async function fetchLearningAssistantEnabled(courseId) { return data; } - -async function fetchLearningAssistantAuditTrial(userId) { - // TODO: Insert correct URL once get endpoint is implemented. - const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${userId}/`); +async function fetchLearningAssistantMessageHistory(courseId) { + const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/history`); const { data } = await getAuthenticatedHttpClient().get(url.href); return data; } - async function fetchLearningAssistantAuditTrial(userId) { // TODO: Insert correct URL once get endpoint is implemented. - const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${userId}/`); - - const { data } = await getAuthenticatedHttpClient().get(url.href); - // TODO: Extract below values from data: - // Whether or not a trial exists - // Days remaining in trial - // When trial was created - return data; -} - -async function fetchLearningAssistantMessageHistory(courseId) { - const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/history`); + const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/data/${userId}/`); const { data } = await getAuthenticatedHttpClient().get(url.href); return data; diff --git a/src/data/slice.js b/src/data/slice.js index 9b3bb165..5d7df191 100644 --- a/src/data/slice.js +++ b/src/data/slice.js @@ -12,9 +12,8 @@ export const initialState = { sidebarIsOpen: false, isEnabled: false, auditTrial: { - trialExists: false, - daysRemaining: 0, - trialCreated: null, // TODO: what do we use for a datetime value here? + startDate: 0, + expirationDate: null, // TODO: what do we use for a datetime value here? }, }; @@ -50,7 +49,8 @@ export const learningAssistantSlice = createSlice({ state.isEnabled = payload; }, setAuditTrial: (state, { payload }) => { - state.auditTrial = payload; + state.auditTrial.startDate = payload.start_date; + state.auditTrial.expirationDate = payload.expiration_date; }, }, }); diff --git a/src/data/thunks.js b/src/data/thunks.js index ac353e1e..6f389607 100644 --- a/src/data/thunks.js +++ b/src/data/thunks.js @@ -136,11 +136,13 @@ export function getIsEnabled(courseId) { }; } -export function getIsAuditTrial(userId) { +export function getAuditTrial(userId) { return async (dispatch) => { try { const data = await fetchLearningAssistantAuditTrial(userId); - dispatch(setAuditTrial(data.enabled)); + if (!_.isEmpty(data)) { // If returned data is not empty + dispatch(setAuditTrial(data)); + } } catch (error) { dispatch(setApiError()); } diff --git a/src/widgets/Xpert.jsx b/src/widgets/Xpert.jsx index 155f13de..f3fb84b7 100644 --- a/src/widgets/Xpert.jsx +++ b/src/widgets/Xpert.jsx @@ -2,7 +2,7 @@ import PropTypes from 'prop-types'; import { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import { updateSidebarIsOpen, getIsEnabled, getIsAuditTrial } from '../data/thunks'; +import { updateSidebarIsOpen, getIsEnabled, getAuditTrial } from '../data/thunks'; import ToggleXpert from '../components/ToggleXpertButton'; import Sidebar from '../components/Sidebar'; import { ExperimentsProvider } from '../experiments'; @@ -15,11 +15,6 @@ const Xpert = ({ courseId, contentToolsEnabled, unitId }) => { const { isEnabled, sidebarIsOpen, - // TODO: How do we plan to use this value to gate things? - // i.e. how to use values such as: - // auditTrial.trialExists - // auditTrial.daysRemaining - // auditTrial.trialCreated auditTrial, } = useSelector(state => state.learningAssistant); @@ -32,9 +27,17 @@ const Xpert = ({ courseId, contentToolsEnabled, unitId }) => { }, [dispatch, courseId]); useEffect(() => { - dispatch(getIsAuditTrial(userId)); + dispatch(getAuditTrial(userId)); }, [dispatch, userId]); + const isAuditTrialNotExpired = () => { + const auditTrialExpired = (Date.now() - auditTrial.expirationDate) > 0; + if (auditTrialExpired) { + return true + } + return false + } + return isEnabled ? ( <> @@ -49,6 +52,7 @@ const Xpert = ({ courseId, contentToolsEnabled, unitId }) => { isOpen={sidebarIsOpen} setIsOpen={setSidebarIsOpen} unitId={unitId} + auditTrialNotExpired={isAuditTrialNotExpired} />