package.cjs.react-utils.BrowserPushNotifications.pushHelper.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.registerBrowserNotifications = exports.destroyBrowserSubscription = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _devices = _interopRequireDefault(require("./devices"));
var _registerServiceWorker = require("./registerServiceWorker");
// THIS FILE HAS BEEN ADDED BY NEETO NOTIFICATIONS ENGINE
// AND IS USED FOR BROWSER NOTIFICATIONS
// CONTACT NEETO NOTIFICATIONS TEAM BEFORE MODIFYING THIS FILE
var createBrowserSubscription = /*#__PURE__*/function () {
var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(subscription) {
var payload;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!subscription) {
_context.next = 13;
break;
}
payload = {
device: {
subscription: subscription,
platform: "browser"
}
};
_context.prev = 2;
_context.next = 5;
return _devices["default"].create(payload);
case 5:
localStorage.setItem("deviceId", JSON.stringify(subscription.toJSON().keys.auth));
_context.next = 11;
break;
case 8:
_context.prev = 8;
_context.t0 = _context["catch"](2);
logger.error("Device registration Failed: ", _context.t0);
case 11:
_context.next = 14;
break;
case 13:
logger.error("Something went wrong while registering device for browser subscription");
case 14:
case "end":
return _context.stop();
}
}, _callee, null, [[2, 8]]);
}));
return function createBrowserSubscription(_x) {
return _ref.apply(this, arguments);
};
}();
var destroyBrowserSubscription = exports.destroyBrowserSubscription = /*#__PURE__*/function () {
var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
var deviceId;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 3;
return (0, _registerServiceWorker.unsubscribeBrowserNotifications)();
case 3:
deviceId = JSON.parse(localStorage.getItem("deviceId"));
_context2.next = 6;
return _devices["default"].destroy(deviceId);
case 6:
localStorage.removeItem("deviceId");
_context2.next = 12;
break;
case 9:
_context2.prev = 9;
_context2.t0 = _context2["catch"](0);
logger.error("Something went wrong while unsubscribing browser notification:", _context2.t0);
case 12:
case "end":
return _context2.stop();
}
}, _callee2, null, [[0, 9]]);
}));
return function destroyBrowserSubscription() {
return _ref2.apply(this, arguments);
};
}();
var registerBrowserNotifications = exports.registerBrowserNotifications = /*#__PURE__*/function () {
var _ref3 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
var vapidPublicKey,
response,
_args3 = arguments;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
vapidPublicKey = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : process.env.VAPID_PUBLIC_KEY;
_context3.next = 3;
return (0, _registerServiceWorker.registerServiceWorker)(vapidPublicKey);
case 3:
response = _context3.sent;
_context3.next = 6;
return createBrowserSubscription(response);
case 6:
case "end":
return _context3.stop();
}
}, _callee3);
}));
return function registerBrowserNotifications() {
return _ref3.apply(this, arguments);
};
}();
//# sourceMappingURL=pushHelper.js.map