UNPKG

react-native-webengage-inbox

Version:
146 lines (129 loc) 3.47 kB
import { NativeModules } from "react-native"; import { enableDevMode, weLogs } from "./utils/weLogs"; const { WEInboxBridge } = NativeModules; function notificationListResponse(result, error, resolve, reject) { weLogs.debug("notificationListResponse reeceived result-", result); if (error) { weLogs.debug("Error in NotificationListResponse - ", error); reject(error); } else { let responseData = { ...result }; const { messageList: messageString = {} } = result; if (messageString) { const messageList = JSON.parse(messageString); responseData.messageList = messageList; } weLogs.debug("notificationList -", responseData); resolve(responseData); } } function initWENotificationInbox() { WEInboxBridge.initWENotificationInbox(); } function getNotificationList(offset = null) { let jsonOffset = null; if (offset) { jsonOffset = JSON.stringify(offset); } weLogs.debug("JSON offset passed to Native- ", jsonOffset); return new Promise((resolve, reject) => { WEInboxBridge.getNotificationList(jsonOffset, (result, error) => notificationListResponse(result, error, resolve, reject) ); }); } function getNotificationCount() { return new Promise((resolve, reject) => { WEInboxBridge.getNotificationCount((result, error) => { if (error) { reject(error); } else { resolve(result); } }); }); } function markRead(notificationItem = {}) { if (!notificationItem) { weLogs.debug("No Notification Item passed to markRead"); return; } const { status = "" } = notificationItem; if (status?.toLowerCase() !== "read") { WEInboxBridge.markRead(notificationItem); } else { weLogs.debug("markRead - status is already read"); } } function markUnread(notificationItem = {}) { if (!notificationItem) { weLogs.debug("No Notification Item passed to markRead"); return; } const { status = "" } = notificationItem; if (status?.toLowerCase() !== "unread") { WEInboxBridge.markUnread(notificationItem); } else { weLogs.debug("markRead - status is already Unread"); } } function trackClick(notificationItem) { if (notificationItem) { WEInboxBridge.trackClick(notificationItem); } else { weLogs.debug("No Notification Item passed to trackClick"); } } function trackView(notificationItem) { if (notificationItem) { WEInboxBridge.trackView(notificationItem); } else { weLogs.debug("No Notification Item passed to trackView"); } } function markDelete(notificationItem) { if (notificationItem) { WEInboxBridge.markDelete(notificationItem); } else { weLogs.debug("No Notification Item passed to markDelete"); } } function readAll(list) { if (list?.length) { WEInboxBridge.readAll(list); } else { weLogs.debug("readAll - list is empty"); } } function unReadAll(list) { if (list?.length) { WEInboxBridge.unReadAll(list); } else { weLogs.debug("unReadAll - list is empty"); } } function deleteAll(list) { if (list?.length) { WEInboxBridge.deleteAll(list); } else { weLogs.debug("deleteAll - list is empty"); } } function resetNotificationCount() { WEInboxBridge.onNotificationIconClick(); } export { getNotificationList, getNotificationCount, readAll, unReadAll, deleteAll, markRead, markUnread, markDelete, trackClick, trackView, resetNotificationCount, initWENotificationInbox, enableDevMode, };