Skip to content

Instantly share code, notes, and snippets.

@kylebuch8
Last active April 18, 2017 17:34
Show Gist options
  • Save kylebuch8/cdf96555780f6059009083bbd98ce7ba to your computer and use it in GitHub Desktop.
Save kylebuch8/cdf96555780f6059009083bbd98ce7ba to your computer and use it in GitHub Desktop.
/*
* show modal to certain accounts
*
* check to see if the user is logged in by looking at the account_number
* property on portal.user_info
*
* Show the modal if the following conditions are true:
* - the current date is between the start and end dates for showing the modal
* - the API call to /api/flagged-accounts returns flagged: true
* - the user has not already seen the modal
*/
(function (global) {
define(['jquery', 'session', 'bootstrap'], function ($, session) {
function init() {
var startDate = new Date('2017-02-28').getTime();
var endDate = new Date('2017-06-30').getTime();
var currentDate = new Date().getTime();
var accountNumber = session.getUserInfo().account_number;
var storageKey = 'rhnmodal'
var hasSeenModal = localStorage.getItem('rhnmodal');
var apiUrl = '/api/flagged-accounts';
var modalHtml = '<div class="modal fade" tabindex="-1" role="dialog" id="rhnModal">' +
'<div class="modal-dialog" role="document">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
'<h4 class="modal-title">ACTION REQUIRED</h4>' +
'</div>' +
'<div class="modal-body">' +
'<p><strong>Migrate to Red Hat Subscription Management</strong></p>' +
'<p>You have systems registered to RHN that need to migrate to RHSM.</p>' +
'<p>Red Hat is transitioning from the Red Hat Network (RHN) hosted interface to the Red Hat Subscription Management (RHSM) interface in July 2017.</p>' +
'<p>' +
'<strong>Take action:</strong><br>' +
'<a href="/products/red-hat-subscription-management/#migration" class="cta-link">Learn how to migrate</a>' +
'</p>' +
'</div>' +
'<div class="modal-footer">' +
'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
if (!accountNumber) {
return;
}
if (currentDate < startDate || currentDate >= endDate) {
return;
}
if (hasSeenModal === 'true') {
return;
}
function handleApiResponse(data) {
if (!data.flagged) {
return;
}
$('body').append(modalHtml);
$('#rhnModal')
.modal('show')
.on('shown.bs.modal', function () {
localStorage.setItem(storageKey, true);
});
}
$.get(apiUrl).then(handleApiResponse);
}
session.onInit(init);
});
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment