@kiwicom/smart-faq
Version:
Smart FAQ
277 lines (241 loc) • 7.93 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setData = exports.initialize = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _idx = _interopRequireDefault(require("idx"));
// @flow
var initialize =
/*#__PURE__*/
function () {
var _ref = (0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee(chatConfig
/*: ChatConfig*/
, elementId
/*: string*/
) {
var guid, deploymentKey, orgId, queueName, chatConfiguration;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
guid = (0, _idx.default)(chatConfig, function (_) {
return _.CHAT_GUID;
});
deploymentKey = (0, _idx.default)(chatConfig, function (_) {
return _.CHAT_DEPLOYMENT_KEY;
});
orgId = (0, _idx.default)(chatConfig, function (_) {
return _.CHAT_ORG_ID;
});
queueName = (0, _idx.default)(chatConfig, function (_) {
return _.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, this);
}));
return function initialize(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
exports.initialize = initialize;
var setData = function setData(webchat
/*: WebchatInstance*/
, guaranteeChatBookingInfo
/*: ?GuaranteeChatBookingInfo*/
) {
var bid = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.bid;
}) || null;
var status = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.status;
}) || null;
var departureCity = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.departureCity;
}) || '';
var departureAirport = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.departureAirport;
}) || '';
var arrivalCity = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.arrivalCity;
}) || '';
var arrivalAirport = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.arrivalAirport;
}) || '';
var phone = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.phone;
}) || '';
var email = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.email;
}) || '';
var firstName = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.firstName;
}) || '';
var lastName = (0, _idx.default)(guaranteeChatBookingInfo, function (_) {
return _.lastName;
}) || '';
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('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 _ref2 = (0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee2(guid
/*: string*/
, deploymentKey
/*: string*/
) {
var body;
return _regenerator.default.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(false);
};
body.appendChild(script);
}));
case 9:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
return function injectScript(_x3, _x4) {
return _ref2.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%'
}
};
};
;