diff --git a/src/data/constants/requests.js b/src/data/constants/requests.js index fc9d6d1..d02ba56 100644 --- a/src/data/constants/requests.js +++ b/src/data/constants/requests.js @@ -10,8 +10,11 @@ export const RequestStates = StrictDict({ export const RequestKeys = StrictDict({ initialize: 'initialize', refreshList: 'refreshList', - enrollEntitlementSession: 'enrollEntitlementSession', - leaveEntitlementSession: 'leaveEntitlementSession', + newEntitlementEnrollment: 'newEntitlementEnrollment', + leaveEntitlementEnrollment: 'leaveEntitlementEnrollment', + switchEntitlementSession: 'switchEntitlementSession', + unenrollFromCourse: 'unenrollFromCourse', + updateEmailSettings: 'updateEmailSettings', }); export const ErrorCodes = StrictDict({ diff --git a/src/data/redux/thunkActions/app.js b/src/data/redux/thunkActions/app.js index 8452a50..b916025 100644 --- a/src/data/redux/thunkActions/app.js +++ b/src/data/redux/thunkActions/app.js @@ -1,4 +1,6 @@ import { StrictDict } from 'utils'; +import { handleEvent } from 'data/services/segment/utils'; +import { eventNames } from 'data/services/segment/constants'; import { actions, selectors } from 'data/redux'; import { post } from 'data/services/lms/utils'; @@ -36,25 +38,54 @@ export const sendConfirmEmail = () => (dispatch, getState) => post( selectors.app.emailConfirmation(getState()).sendEmailUrl, ); -export const updateEntitlementSession = (cardId, selection) => (dispatch, getState) => { - const entitlement = selectors.app.courseCard.entitlement(getState(), cardId); - const { uuid } = entitlement; - console.log({ - cardId, - selection, - entitlement, - uuid, +export const newEntitlementEnrollment = (cardId, selection) => (dispatch, getState) => { + const { uuid } = selectors.app.courseCard.entitlement(getState(), cardId); + handleEvent(eventNames.sessionChange({ action: 'new' }), { + fromCourseRun: null, + toCourseRun: selection, }); + return dispatch(requests.newEntitlementEnrollment({ uuid, courseId: selection })); }; -export const unenroll = (courseId) => (dispatch, getState) => post( - selectors.app.courseCard.courseRun(getState(), courseId), -).then(() => dispatch(module.refreshList())); +export const switchEntitlementEnrollment = (cardId, selection) => (dispatch, getState) => { + const { courseId } = selectors.app.courseCard.courseRun(getState(), cardId); + const { uuid } = selectors.app.courseCard.entitlement(getState(), cardId); + handleEvent(eventNames.sessionChange({ action: 'switch' }), { + fromCourseRun: courseId, + toCourseRun: selection, + }); + return dispatch(requests.switchEntitlementEnrollment({ uuid, courseId: selection })); +}; + +export const leaveEntitlementSession = (cardId) => (dispatch, getState) => { + const { courseId } = selectors.app.courseCard.courseRun(getState(), cardId); + const { uuid } = selectors.app.courseCard.entitlement(getState(), cardId); + handleEvent(eventNames.entitlementUnenroll({ action: 'leave' }), { + fromCourseRun: courseId, + toCourseRun: null, + }); + return dispatch(requests.leaveEntitlementSession({ uuid })); +}; + +export const unenrollFromCourse = (courseId, reason) => (dispatch) => { + handleEvent(eventNames.unenrollReason, { + category: 'user-engagement', + displayName: 'v1', + label: reason, + course_id: courseId, + }); + dispatch(requests.unenrollFromCourse({ + courseId, + onSuccess: () => dispatch(module.refreshList()), + })); +}; export default StrictDict({ initialize, refreshList, sendConfirmEmail, - updateEntitlementSession, - unenroll, + newEntitlementEnrollment, + switchEntitlementEnrollment, + leaveEntitlementSession, + unenrollFromCourse, }); diff --git a/src/data/redux/thunkActions/requests.js b/src/data/redux/thunkActions/requests.js index 4a10f09..92ba438 100644 --- a/src/data/redux/thunkActions/requests.js +++ b/src/data/redux/thunkActions/requests.js @@ -4,7 +4,7 @@ import { RequestKeys } from 'data/constants/requests'; import { actions } from 'data/redux'; import api from 'data/services/lms/api'; -// import * as module from './requests'; +import * as module from './requests'; /** * Wrapper around a network request promise, that sends actions to the redux store to @@ -33,44 +33,62 @@ export const networkRequest = ({ }); }; -export const initializeList = ({ onSuccess, onFailure }) => (dispatch) => { - dispatch(networkRequest({ - requestKey: RequestKeys.initialize, - onFailure, - onSuccess, - promise: api.initializeList(), - })); -}; +export const networkAction = (requestKey, promise, options) => (dispatch) => ( + dispatch(module.networkRequest({ + requestKey, + promise, + ...options, + }))); -export const updateEntitlementEnrollment = ({ +export const initializeList = (options) => module.networkAction( + RequestKeys.initialize, + api.initializeList(), + options, +); + +export const newEntitlementEnrollment = ({ uuid, courseId, - onSuccess, - onFailure, -}) => (dispatch) => { - dispatch(networkRequest({ - requestKey: RequestKeys.enrollEntitlementSession, - onFailure, - onSuccess, - promise: api.updateEntitlementEnrollment({ uuid, courseId }), - })); -}; + ...options +}) => module.networkAction( + RequestKeys.newEntitlementEnrollment, + api.updateEntitlementEnrollment({ uuid, courseId }), + options, +); -export const leaveEntitlementSession = ({ +export const switchEntitlementEnrollment = ({ uuid, - onSuccess, - onFailure, -}) => (dispatch) => { - dispatch(networkRequest({ - requestKey: RequestKeys.leaveEntitlementSession, - onFailure, - onSuccess, - promise: api.leaveEntitlementEnrollment({ uuid }), - })); -}; + courseId, + ...options +}) => module.networkAction( + RequestKeys.switchEntitlementSession, + api.updateEntitlementEnrollment({ uuid, courseId }), + options, +); + +export const leaveEntitlementSession = ({ uuid, ...options }) => module.networkAction( + RequestKeys.leaveEntitlementSession, + api.leaveEntitlementEnrollment({ uuid }), + options, +); + +export const unenrollFromCourse = ({ courseId, ...options }) => module.networkAction( + RequestKeys.unenrollFromCourse, + api.unenrollFromCourse({ courseId }), + options, +); + +export const updateEmailSettings = ({ courseId, enable, ...options }) => module.networkAction( + RequestKeys.updateEmailSettings, + api.updateEmailSettings({ courseId, enable }), + options, +); export default StrictDict({ initializeList, - updateEntitlementEnrollment, leaveEntitlementSession, + newEntitlementEnrollment, + switchEntitlementEnrollment, + unenrollFromCourse, + updateEmailSettings, });