|
| 1 | +/** |
| 2 | + * @module "actions.notifications" |
| 3 | + * @desc Actions related to notifications data. |
| 4 | + */ |
| 5 | + |
| 6 | +import _ from 'lodash'; |
| 7 | +import { createActions } from 'redux-actions'; |
| 8 | +import { getService } from '../services/notifications'; |
| 9 | + |
| 10 | +/** |
| 11 | + * TODO: We will need to change this based on API and |
| 12 | + * frontend mapping we need later. |
| 13 | + */ |
| 14 | +function processData(data) { |
| 15 | + const retData = _.map(data, (item) => { |
| 16 | + const object = {}; |
| 17 | + object.id = item.id; |
| 18 | + object.sourceId = item.contents.id; |
| 19 | + object.sourceName = item.contents.name || item.contents.title; |
| 20 | + object.eventType = item.type; |
| 21 | + object.isRead = item.read; |
| 22 | + object.isSeen = item.seen; |
| 23 | + object.contents = item.contents.message || item.contents.title; |
| 24 | + object.version = item.version; |
| 25 | + object.date = item.createdAt; |
| 26 | + return object; |
| 27 | + }); |
| 28 | + return retData; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * @static |
| 33 | + * @desc Creates an action that signals beginning of notifications |
| 34 | + * loading. |
| 35 | + * @return {Action} |
| 36 | + */ |
| 37 | +function getNotificationsInit() { |
| 38 | + return { }; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @static |
| 43 | + * @desc Creates an action that loads member achievements. |
| 44 | + * @param {String} tokenV3 v3 auth token. |
| 45 | + * @return {Action} |
| 46 | + */ |
| 47 | +async function getNotificationsDone(tokenV3) { |
| 48 | + let data; |
| 49 | + try { |
| 50 | + data = await getService(tokenV3).getNotifications(); |
| 51 | + } catch (e) { |
| 52 | + data = []; |
| 53 | + } |
| 54 | + return processData(data.items || []); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * @static |
| 59 | + * @desc Creates an action that signals beginning of mark notification as read |
| 60 | + * loading. |
| 61 | + * @return {Action} |
| 62 | + */ |
| 63 | +function markNotificationAsReadInit() { |
| 64 | + return { }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @static |
| 69 | + * @desc Creates an action that marks notification as read. |
| 70 | + * @param {String} tokenV3 v3 auth token. |
| 71 | + * @return {Action} |
| 72 | + */ |
| 73 | +async function markNotificationAsReadDone(item, tokenV3) { |
| 74 | + try { |
| 75 | + await getService(tokenV3).markNotificationAsRead(item.id); |
| 76 | + } catch (e) { |
| 77 | + return e; |
| 78 | + } |
| 79 | + return item; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @static |
| 84 | + * @desc Creates an action that signals beginning of mark all notification as read |
| 85 | + * loading. |
| 86 | + * @return {Action} |
| 87 | + */ |
| 88 | +function markAllNotificationAsReadInit() { |
| 89 | + return { }; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @static |
| 94 | + * @desc Creates an action that marks all notification as read. |
| 95 | + * @param {String} tokenV3 v3 auth token. |
| 96 | + * @return {Action} |
| 97 | + */ |
| 98 | +async function markAllNotificationAsReadDone(tokenV3) { |
| 99 | + try { |
| 100 | + await getService(tokenV3).markAllNotificationAsRead(); |
| 101 | + } catch (e) { |
| 102 | + return e; |
| 103 | + } |
| 104 | + return true; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +/** |
| 109 | + * @static |
| 110 | + * @desc Creates an action that signals beginning of mark all notification as seen |
| 111 | + * loading. |
| 112 | + * @return {Action} |
| 113 | + */ |
| 114 | +function markAllNotificationAsSeenInit() { |
| 115 | + return { }; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @static |
| 120 | + * @desc Creates an action that marks all notification as seen. |
| 121 | + * @param {String} tokenV3 v3 auth token. |
| 122 | + * @return {Action} |
| 123 | + */ |
| 124 | +async function markAllNotificationAsSeenDone(items, tokenV3) { |
| 125 | + try { |
| 126 | + await getService(tokenV3).markAllNotificationAsSeen(items); |
| 127 | + } catch (e) { |
| 128 | + return e; |
| 129 | + } |
| 130 | + return items; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +/** |
| 135 | + * @static |
| 136 | + * @desc Creates an action that signals beginning of dismiss all challenge notifications |
| 137 | + * loading. |
| 138 | + * @return {Action} |
| 139 | + */ |
| 140 | +function dismissChallengeNotificationsInit() { |
| 141 | + return { }; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @static |
| 146 | + * @desc Creates an action that dismisses all challenge notifications |
| 147 | + * @param {String} tokenV3 v3 auth token. |
| 148 | + * @return {Action} |
| 149 | + */ |
| 150 | +async function dismissChallengeNotificationsDone(challengeId, tokenV3) { |
| 151 | + try { |
| 152 | + await getService(tokenV3).dismissChallengeNotifications(challengeId); |
| 153 | + } catch (e) { |
| 154 | + return e; |
| 155 | + } |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +export default createActions({ |
| 161 | + NOTIFICATIONS: { |
| 162 | + GET_NOTIFICATIONS_INIT: getNotificationsInit, |
| 163 | + GET_NOTIFICATIONS_DONE: getNotificationsDone, |
| 164 | + MARK_NOTIFICATION_AS_READ_INIT: markNotificationAsReadInit, |
| 165 | + MARK_NOTIFICATION_AS_READ_DONE: markNotificationAsReadDone, |
| 166 | + MARK_ALL_NOTIFICATION_AS_READ_INIT: markAllNotificationAsReadInit, |
| 167 | + MARK_ALL_NOTIFICATION_AS_READ_DONE: markAllNotificationAsReadDone, |
| 168 | + MARK_ALL_NOTIFICATION_AS_SEEN_INIT: markAllNotificationAsSeenInit, |
| 169 | + MARK_ALL_NOTIFICATION_AS_SEEN_DONE: markAllNotificationAsSeenDone, |
| 170 | + DISMISS_CHALLENGE_NOTIFICATIONS_INIT: dismissChallengeNotificationsInit, |
| 171 | + DISMISS_CHALLENGE_NOTIFICATIONS_DONE: dismissChallengeNotificationsDone, |
| 172 | + }, |
| 173 | +}); |
0 commit comments