Skip to content

Commit

Permalink
feat: added api for account notification type
Browse files Browse the repository at this point in the history
  • Loading branch information
sundasnoreen12 committed Nov 20, 2024
1 parent 301797a commit 235dd9c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
7 changes: 1 addition & 6 deletions src/notification-preferences/NotificationPreferenceApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ import { useIsOnMobile } from '../hooks';
import NotificationTypes from './NotificationTypes';
import { notificationChannels, shouldHideAppPreferences } from './data/utils';
import NotificationPreferenceColumn from './NotificationPreferenceColumn';
import { selectPreferenceAppToggleValue, selectSelectedCourseId, selectAppPreferences } from './data/selectors';
import { selectPreferenceAppToggleValue, selectAppPreferences } from './data/selectors';

const NotificationPreferenceApp = ({ appId }) => {
const intl = useIntl();
const courseId = useSelector(selectSelectedCourseId());
const appToggle = useSelector(selectPreferenceAppToggleValue(appId));
const appPreferences = useSelector(selectAppPreferences(appId));
const mobileView = useIsOnMobile();
const NOTIFICATION_CHANNELS = notificationChannels();
const hideAppPreferences = shouldHideAppPreferences(appPreferences, appId) || false;

if (!courseId) {
return null;
}

return (
!hideAppPreferences && (
<Collapsible.Advanced
Expand Down
2 changes: 1 addition & 1 deletion src/notification-preferences/NotificationSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const NotificationSettings = () => {
</Hyperlink>
</div>
<NotificationCoursesDropdown />
{courseId && <NotificationPreferences courseId={courseId} />}
<NotificationPreferences courseId={courseId} />
</Container>
)
);
Expand Down
6 changes: 4 additions & 2 deletions src/notification-preferences/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export const Actions = {
UPDATE_APP_PREFERENCE: 'updateAppValue',
};

export const fetchNotificationPreferenceSuccess = (courseId, payload) => dispatch => (
dispatch({ type: Actions.FETCHED_PREFERENCES, courseId, payload })
export const fetchNotificationPreferenceSuccess = (courseId, payload, isAccountPreference) => dispatch => (
dispatch({
type: Actions.FETCHED_PREFERENCES, courseId, payload, isAccountPreference,
})
);

export const fetchNotificationPreferenceFetching = () => dispatch => (
Expand Down
14 changes: 11 additions & 3 deletions src/notification-preferences/data/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import {
SUCCESS_STATUS,
FAILURE_STATUS,
} from '../../constants';
import { normalizeAccountPreferences } from './thunks';

export const defaultState = {
showPreferences: false,
courses: {
status: IDLE_STATUS,
courses: [{ id: 'account', name: 'Account' }],
courses: [{ id: '', name: 'Account' }],
pagination: {},
},
preferences: {
status: IDLE_STATUS,
updatePreferenceStatus: IDLE_STATUS,
selectedCourse: 'account',
selectedCourse: '',
preferences: [],
apps: [],
nonEditable: {},
Expand Down Expand Up @@ -66,15 +67,22 @@ const notificationPreferencesReducer = (state = defaultState, action = {}) => {
},
};
case Actions.FETCHED_PREFERENCES:
{
const { preferences } = state;
if (action.isAccountPreference) {
normalizeAccountPreferences(preferences, action.payload);
}

return {
...state,
preferences: {
...state.preferences,
...preferences,
status: SUCCESS_STATUS,
updatePreferenceStatus: SUCCESS_STATUS,
...action.payload,
},
};
}
case Actions.FAILED_PREFERENCES:
return {
...state,
Expand Down
17 changes: 17 additions & 0 deletions src/notification-preferences/data/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ export const patchPreferenceToggle = async (
const { data } = await getAuthenticatedHttpClient().patch(url, patchData);
return data;
};

export const postPreferenceToggle = async (
notificationApp,
notificationType,
notificationChannel,
value,
) => {
const patchData = snakeCaseObject({
notificationApp,
notificationType: snakeCase(notificationType),
notificationChannel,
value,
});
const url = `${getConfig().LMS_BASE_URL}/api/notifications/preferences/update-all/`;
const { data } = await getAuthenticatedHttpClient().post(url, patchData);
return data;
};
58 changes: 46 additions & 12 deletions src/notification-preferences/data/thunks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { camelCaseObject } from '@edx/frontend-platform';
import camelCase from 'lodash.camelcase';
import EMAIL_CADENCE from './constants';
import {
fetchCourseListSuccess,
Expand All @@ -14,6 +15,7 @@ import {
getCourseList,
getCourseNotificationPreferences,
patchPreferenceToggle,
postPreferenceToggle,
} from './service';

const normalizeCourses = (responseData) => {
Expand All @@ -36,8 +38,29 @@ const normalizeCourses = (responseData) => {
};
};

const normalizePreferences = (responseData) => {
const preferences = responseData.notificationPreferenceConfig;
export const normalizeAccountPreferences = (originalData, updateInfo) => {
const {
app, notificationType, channel, updatedValue,
} = updateInfo.data;

const preferenceToUpdate = originalData.preferences.find(
(preference) => preference.appId === app && preference.id === camelCase(notificationType),
);

if (preferenceToUpdate) {
preferenceToUpdate[channel] = updatedValue;
}

return originalData;
};

const normalizePreferences = (responseData, courseId) => {
let preferences;
if (courseId) {
preferences = responseData.notificationPreferenceConfig;
} else {
preferences = responseData.data;
}

const appKeys = Object.keys(preferences);
const apps = appKeys.map((appId) => ({
Expand Down Expand Up @@ -92,7 +115,7 @@ export const fetchCourseNotificationPreferences = (courseId) => (
dispatch(updateSelectedCourse(courseId));
dispatch(fetchNotificationPreferenceFetching());
const data = await getCourseNotificationPreferences(courseId);
const normalizedData = normalizePreferences(camelCaseObject(data));
const normalizedData = normalizePreferences(camelCaseObject(data), courseId);
dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData));
} catch (errors) {
dispatch(fetchNotificationPreferenceFailed());
Expand Down Expand Up @@ -121,15 +144,26 @@ export const updatePreferenceToggle = (
notificationChannel,
!value,
));
const data = await patchPreferenceToggle(
courseId,
notificationApp,
notificationType,
notificationChannel,
value,
);
const normalizedData = normalizePreferences(camelCaseObject(data));
dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData));
let data = null;
if (courseId) {
data = await patchPreferenceToggle(
courseId,
notificationApp,
notificationType,
notificationChannel,
value,
);
const normalizedData = normalizePreferences(camelCaseObject(data), courseId);
dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData));
} else {
data = await postPreferenceToggle(
notificationApp,
notificationType,
notificationChannel,
value,
);
dispatch(fetchNotificationPreferenceSuccess(courseId, camelCaseObject(data), true));
}
} catch (errors) {
dispatch(updatePreferenceValue(
notificationApp,
Expand Down

0 comments on commit 235dd9c

Please sign in to comment.