Skip to content

Commit

Permalink
fix: correct state + add gating for message form
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Nov 26, 2024
1 parent c702ca1 commit 0e65fc3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
8 changes: 6 additions & 2 deletions src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const Sidebar = ({
isOpen,
setIsOpen,
unitId,
}) => {
auditTrialNotExpired
&, > {
const {
apiError,
disclosureAcknowledged,
Expand Down Expand Up @@ -98,7 +99,10 @@ const Sidebar = ({
</div>
)
}
{getMessageForm()}
{
auditTrialNotExpired
&& getMessageForm()
}
</div>
);

Expand Down
20 changes: 3 additions & 17 deletions src/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Check failure on line 16 in src/data/slice.js

View workflow job for this annotation

GitHub Actions / test

Multiple spaces found before '// TODO: what ...'
},
};

Expand Down Expand Up @@ -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;
},
},
});
Expand Down
6 changes: 4 additions & 2 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 143 in src/data/thunks.js

View workflow job for this annotation

GitHub Actions / test

'_' is not defined

Check failure on line 143 in src/data/thunks.js

View workflow job for this annotation

GitHub Actions / test

Multiple spaces found before '// If returned...'
dispatch(setAuditTrial(data));
}
} catch (error) {
dispatch(setApiError());
}
Expand Down
18 changes: 11 additions & 7 deletions src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 7 in src/widgets/Xpert.jsx

View workflow job for this annotation

GitHub Actions / test

Parse errors in imported module '../components/Sidebar': ',' expected. (24:2)

Check failure on line 7 in src/widgets/Xpert.jsx

View workflow job for this annotation

GitHub Actions / test

Parse errors in imported module '../components/Sidebar': ',' expected. (24:2)
import { ExperimentsProvider } from '../experiments';
Expand All @@ -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);

Expand All @@ -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 ? (
<ExperimentsProvider>
<>
Expand All @@ -49,6 +52,7 @@ const Xpert = ({ courseId, contentToolsEnabled, unitId }) => {
isOpen={sidebarIsOpen}
setIsOpen={setSidebarIsOpen}
unitId={unitId}
auditTrialNotExpired={isAuditTrialNotExpired}
/>
</>
</ExperimentsProvider>
Expand Down

0 comments on commit 0e65fc3

Please sign in to comment.