Skip to content

Add notifications settings #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: notifications-settings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Object {
"notifications": Object {
"dismissChallengeNotificationsDone": [Function],
"dismissChallengeNotificationsInit": [Function],
"getNotificationSettingsDone": [Function],
"getNotificationSettingsInit": [Function],
"getNotificationsDone": [Function],
"getNotificationsInit": [Function],
"markAllNotificationAsReadDone": [Function],
Expand All @@ -104,6 +106,8 @@ Object {
"markAllNotificationAsSeenInit": [Function],
"markNotificationAsReadDone": [Function],
"markNotificationAsReadInit": [Function],
"saveNotificationSettingsDone": [Function],
"saveNotificationSettingsInit": [Function],
},
"profile": Object {
"addSkillDone": [Function],
Expand Down
120 changes: 60 additions & 60 deletions dist/dev/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/prod/index.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/actions/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,56 @@ async function dismissChallengeNotificationsDone(challengeId, tokenV3) {
return true;
}

/**
* @static
* @desc Creates an action that signals beginning of notification settings
* loading.
* @return {Action}
*/
async function getNotificationSettingsInit() {
return { };
}

/**
* @static
* @desc Creates an action that loads member achievements.
* @param {String} tokenV3 v3 auth token.
* @return {Action}
*/
async function getNotificationSettingsDone(tokenV3) {
let settingsData;
try {
settingsData = await getService(tokenV3).getNotificationSettings();
} catch (e) {
return { error: e.message };
}
return settingsData.notifications || [];
}

/**
* @static
* @desc Creates an action that signals beginning of save notification settings
* loading.
* @return {Action}
*/
async function saveNotificationSettingsInit() {
return { };
}

/**
* @static
* @desc Creates an action that saves member achievements.
* @param {String} tokenV3 v3 auth token.
* @return {Action}
*/
async function saveNotificationSettingsDone(data, tokenV3) {
try {
await getService(tokenV3).saveNotificationSettings(data);
} catch (e) {
return { error: e.message };
}
return true;
}

export default createActions({
NOTIFICATIONS: {
Expand All @@ -169,5 +219,9 @@ export default createActions({
MARK_ALL_NOTIFICATION_AS_SEEN_DONE: markAllNotificationAsSeenDone,
DISMISS_CHALLENGE_NOTIFICATIONS_INIT: dismissChallengeNotificationsInit,
DISMISS_CHALLENGE_NOTIFICATIONS_DONE: dismissChallengeNotificationsDone,
GET_NOTIFICATION_SETTINGS_INIT: getNotificationSettingsInit,
GET_NOTIFICATION_SETTINGS_DONE: getNotificationSettingsDone,
SAVE_NOTIFICATION_SETTINGS_INIT: saveNotificationSettingsInit,
SAVE_NOTIFICATION_SETTINGS_DONE: saveNotificationSettingsDone,
},
});
75 changes: 75 additions & 0 deletions src/reducers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,77 @@ function onDismissChallengeNotificationsDone(state, { error, payload }) {
}


/**
* Handles NOTIFICATIONS/GET_NOTIFICATION_SETTINGS_INIT action.
* @param {Object} state
* @return {Object} New state
*/
function onGetNotificationSettingsInit(state) {
return { ...state };
}

/**
* Handles NOTIFICATIONS/GET_NOTIFICATION_SETTINGS_DONE action.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetNotificationSettingsDone(state, { error, payload }) {
if (payload.error) {
logger.error('Failed to get notification settings!', error);
fireErrorMessage(
'ERROR: Failed to load the notification settings',
'Please, try again a bit later',
);
return {
...state,
fetchNotificationsFailure: true,
notificationSettings: {},
};
}

return {
...state,
fetchNotificationsFailure: false,
notificationSettings: payload,
};
}

/**
* Handles NOTIFICATIONS/SAVE_NOTIFICATION_SETTINGS_INIT action.
* @param {Object} state
* @return {Object} New state
*/
function onsaveNotificationSettingsInit(state) {
return { ...state };
}

/**
* Handles NOTIFICATIONS/SAVE_NOTIFICATION_SETTINGS_DONE action.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onsaveNotificationSettingsDone(state, { error, payload }) {
if (payload.error) {
logger.error('Failed to get notification settings!', error);
fireErrorMessage(
'ERROR: Failed to load the notification settings',
'Please, try again a bit later',
);
return {
...state,
fetchNotificationsFailure: true,
notificationSettings: {},
};
}

return {
...state,
fetchNotificationsFailure: false,
};
}

/**
* Creates a new Members reducer with the specified initial state.
* @param {Object} initialState Optional. Initial state.
Expand All @@ -232,6 +303,10 @@ function create(initialState = {}) {
[a.markAllNotificationAsSeenDone]: onMarkAllNotificationAsSeenDone,
[a.dismissChallengeNotificationsInit]: onDismissChallengeNotificationsInit,
[a.dismissChallengeNotificationsDone]: onDismissChallengeNotificationsDone,
[a.getNotificationSettingsInit]: onGetNotificationSettingsInit,
[a.getNotificationSettingsDone]: onGetNotificationSettingsDone,
[a.saveNotificationSettingsInit]: onsaveNotificationSettingsInit,
[a.saveNotificationSettingsDone]: onsaveNotificationSettingsDone,
}, initialState);
}

Expand Down
18 changes: 18 additions & 0 deletions src/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ class NotificationService {
return this.private.apiV5.put(`/notifications/${challengeID}/dismiss`)
.then(res => (res.ok ? null : Promise.reject(new Error(res.statusText))));
}

/**
* Gets member's notification settings.
* @return {Promise} Resolves to the notification information object.
*/
async getNotificationSettings() {
return this.private.apiV5.get('/notifications/settings')
.then(res => (res.ok ? res.json() : new Error(res.statusText)));
}

/**
* Save member's notification settings.
* @return {Promise} Resolves to the notification information object.
*/
async saveNotificationSettings(data) {
return this.private.apiV5.put('/notifications/settings', data)
.then(res => (res.ok ? null : Promise.reject(new Error(res.statusText))));
}
}

let lastInstance = null;
Expand Down