UNPKG

@kiwicom/smart-faq

Version:

265 lines (225 loc) 9.39 kB
/*:: import type { ChatConfig, GuaranteeChatBookingInfo, } from '../context/GuaranteeChatInfo';*/ /*:: type WebchatInstance = { getConfig: () => { setData: Object => void, }, };*/ "use strict"; require("core-js/modules/es.array.concat"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.promise"); Object.defineProperty(exports, "__esModule", { value: true }); exports.setData = exports.initialize = void 0; require("regenerator-runtime/runtime"); function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } // @flow var initialize = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(chatConfig /*: ChatConfig*/ , elementId /*: string*/ ) { var guid, deploymentKey, orgId, queueName, chatConfiguration; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: guid = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.CHAT_GUID; deploymentKey = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.CHAT_DEPLOYMENT_KEY; orgId = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.CHAT_ORG_ID; queueName = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.CHAT_QUEUE_NAME; if (guid && deploymentKey && orgId && queueName) { _context.next = 6; break; } throw new Error('Secrets guid, deploymentKey, orgId or queueName for Guarantee chat not provided.'); case 6: _context.next = 8; return injectScript(guid, deploymentKey); case 8: if (!(!window.webchat || window.webchat.isChatRendered())) { _context.next = 14; break; } // required by Purecloud for reconnect window.PURECLOUD_WEBCHAT_FRAME_CONFIG = { containerEl: elementId }; chatConfiguration = getConfig(orgId, queueName); _context.next = 13; return window.ININ.webchat.create(chatConfiguration); case 13: window.webchat = _context.sent; case 14: case "end": return _context.stop(); } } }, _callee); })); return function initialize(_x, _x2) { return _ref.apply(this, arguments); }; }(); exports.initialize = initialize; var setData = function setData(webchat /*: WebchatInstance*/ , guaranteeChatBookingInfo /*: ?GuaranteeChatBookingInfo*/ ) { var _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9, _ref10, _ref11; var bid = (_ref2 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.bid) !== null && _ref2 !== void 0 ? _ref2 : null; var status = (_ref3 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.status) !== null && _ref3 !== void 0 ? _ref3 : null; var departureCity = (_ref4 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.departureCity) !== null && _ref4 !== void 0 ? _ref4 : ''; var departureAirport = (_ref5 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.departureAirport) !== null && _ref5 !== void 0 ? _ref5 : ''; var arrivalCity = (_ref6 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.arrivalCity) !== null && _ref6 !== void 0 ? _ref6 : ''; var arrivalAirport = (_ref7 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.arrivalAirport) !== null && _ref7 !== void 0 ? _ref7 : ''; var phone = (_ref8 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.phone) !== null && _ref8 !== void 0 ? _ref8 : ''; var email = (_ref9 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.email) !== null && _ref9 !== void 0 ? _ref9 : ''; var firstName = (_ref10 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.firstName) !== null && _ref10 !== void 0 ? _ref10 : ''; var lastName = (_ref11 = guaranteeChatBookingInfo === null || guaranteeChatBookingInfo === void 0 ? void 0 : guaranteeChatBookingInfo.lastName) !== null && _ref11 !== void 0 ? _ref11 : ''; webchat.getConfig().setData({ firstName: firstName, lastName: lastName, addressStreet: '', addressCity: '', addressPostalCode: '', addressState: '', phoneNumber: phone, customField1Label: 'BID', customField1: bid, customField2Label: 'Departure', customField2: "".concat(departureCity, " (").concat(departureAirport, ")"), customField3Label: 'Arrival', customField3: "".concat(arrivalCity, " (").concat(arrivalAirport, ")"), bid: bid, status: status, phone: phone, email: email, departureCity: departureCity, departureAirport: departureAirport, arrivalCity: arrivalCity, arrivalAirport: arrivalAirport }); }; exports.setData = setData; var waitForInjectedScript = function waitForInjectedScript() { var attempt /*: number*/ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; if (attempt === 5) { return Promise.reject(new Error('Purecloud chat script not loaded before timeout.')); } return new Promise(function (resolve) { setTimeout(function () { if (window.ININ) { return resolve(false); } return resolve(waitForInjectedScript(attempt + 1)); }, 200 * attempt); }); }; var injectScript = /*#__PURE__*/ function () { var _ref12 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(guid /*: string*/ , deploymentKey /*: string*/ ) { var body; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: body = document && document.body; if (body) { _context2.next = 3; break; } return _context2.abrupt("return", Promise.reject(new Error('Unexpected: no DOM body present.'))); case 3: if (!document.getElementById('purecloud-webchat-js')) { _context2.next = 8; break; } if (window.ININ) { _context2.next = 7; break; } _context2.next = 7; return waitForInjectedScript(); case 7: return _context2.abrupt("return", Promise.resolve(false)); case 8: return _context2.abrupt("return", new Promise(function (resolve, reject) { var script = document.createElement('script'); script.src = 'https://apps.mypurecloud.com/webchat/jsapi-v1.js'; script.id = 'purecloud-webchat-js'; script.setAttribute('region', 'eu-west-1'); script.setAttribute('org-guid', guid); script.setAttribute('deployment-key', deploymentKey); script.onload = function () { if (!window.ININ) { reject(new Error('Purecloud chat script not loaded successfully.')); return; } resolve(true); }; script.onerror = function () { return reject(new Error(false)); }; body.appendChild(script); })); case 9: case "end": return _context2.stop(); } } }, _callee2); })); return function injectScript(_x3, _x4) { return _ref12.apply(this, arguments); }; }(); var getConfig = function getConfig(orgId /*: string*/ , queueName /*: string*/ ) { var kiwiLogo = { width: 48, height: 40, url: 'https://images.kiwi.com/whitelabels/0x40/kiwicom-mobile.png' }; return { webchatAppUrl: 'https://apps.mypurecloud.ie/webchat', webchatServiceUrl: 'https://realtime.mypurecloud.ie:443', orgId: orgId, orgName: 'kiwicom', queueName: queueName, logLevel: process.env.NODE_ENV === 'development' ? 'DEBUG' : 'INFO', locale: 'en', reconnectEnabled: true, companyLogoSmall: kiwiLogo, agentAvatar: kiwiLogo, welcomeMessage: 'Thanks for chatting with us.', cssClass: 'webchat-frame', css: { width: '100%', height: '100%' } }; };