@kiwicom/smart-faq
Version:
218 lines (192 loc) • 6.23 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*/
) {
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: 'smartFAQGuarantee'
};
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) {
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 injectScript = function injectScript(guid
/*: string*/
, deploymentKey
/*: string*/
)
/*: Promise<boolean>*/
{
var body = document && document.body;
if (!body) {
return Promise.reject(new Error('Unexpected: no DOM body present.'));
}
if (document.getElementById('purecloud-webchat-js')) {
return Promise.resolve(false);
}
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);
});
};
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%'
}
};
};