Skip to content

Commit

Permalink
Merge pull request #2042 from usagov/USAGOV-2014-Display-Call-Times
Browse files Browse the repository at this point in the history
USAGOV-2014-Display-Call-Times: Update callCenterWaitTimeLookup.js
  • Loading branch information
akf authored Oct 30, 2024
2 parents 53c27c7 + 0cd04bb commit 2fcf74e
Showing 1 changed file with 75 additions and 69 deletions.
144 changes: 75 additions & 69 deletions web/themes/custom/usagov/scripts/callCenterWaitTimeLookup.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
jQuery(document).ready(async function () {
"use strict";

var waittime = (function() {
if (jQuery("html[lang|='en']").length ||
jQuery("html[lang|='es']").length) {
jQuery.ajax({
"url": "https://s3-us-gov-west-1.amazonaws.com/cg-4d6fb302-315f-403e-a96e-a7563ccddd3d/1.0/waittime.json",
"type": "GET",
"success": function (response) {
var json = jQuery.parseJSON(response);
var seconds = -1;
function checkPagePath() {
if (
window.location.pathname === "/phone" ||
window.location.pathname === "/es/llamenos" ||
window.location.pathname === "/chat" ||
window.location.pathname === "/es/chat"
) {
return true;
}
}

if (jQuery("html[lang|='en']").length) {
seconds = json.enEstimatedWaitTimeSeconds;
}
if (jQuery("html[lang|='es']").length) {
seconds = json.spEstimatedWaitTimeSeconds;
}
function getCallCenterWaitTime() {
let jsonSeconds;
let jsonTimestamp;
jQuery.ajax({
"url": "https://s3-us-gov-west-1.amazonaws.com/cg-4d6fb302-315f-403e-a96e-a7563ccddd3d/1.0/waittime.json",
"type": "GET",
"dataType": "json",
"success": function (response) {
if (window.location.pathname === "/phone") {
jsonSeconds = response.call.estimatedWaitTimeSeconds.en;
}
else if (window.location.pathname === "/es/llamenos") {
jsonSeconds = response.call.estimatedWaitTimeSeconds.sp;
}
else if (window.location.pathname === "/chat") {
jsonSeconds = response.chat.estimatedWaitTimeSeconds.en;
}
else if (window.location.pathname === "/es/chat") {
jsonSeconds = response.chat.estimatedWaitTimeSeconds.sp;
}

if (seconds >= 0) {
var minutes = Math.floor(seconds / 60);
var remainingMinutes = minutes % 60;
seconds = seconds - (minutes * 60);
var content = "Estimated wait time: ";
var secondsText = " second";
var minuteText = " minute";
var noneText = "None";
jsonTimestamp = response.timestamp;
createDisplayWaitTime(jsonSeconds, jsonTimestamp);
},
"error": function (xhr, status, error) {
console.log(error);
},
});
}

if (jQuery("html[lang|='es']").length) {
content = "Tiempo de espera estimado: ";
secondsText = " segundo";
minuteText = " minuto";
noneText = "Ninguno";
}
function checkTimeStamp(timestamp) {
let timeOfEstimate = timestamp ? timestamp : 0;
if (Date.now() / 1000 - timeOfEstimate < 600) {
return true;
}
}

if (remainingMinutes > 0) {
content += remainingMinutes + minuteText;
if (remainingMinutes > 1) {
content += "s";
}
}
function createDisplayWaitTime(actualSeconds, timestamp) {
// If the estimated time was captured over 10 minutes ago, remain silent.
if (checkTimeStamp(timestamp)) {
let displayTime;
if (actualSeconds !== -1) {
if (actualSeconds < 60) {
displayTime = 1;
}
else {
displayTime = Math.round(actualSeconds / 60);
}
displayWaitTime(displayTime);
}
}
}

if (seconds > 0) {
if (remainingMinutes > 0) {
content += ", ";
}
content += seconds + secondsText;
if (seconds > 1) {
content += "s";
}
}
function displayWaitTime(displayTime) {
let docLang = [document.documentElement.lang];

if (minutes === 0 && seconds === 0) {
content = content + noneText;
}
let plural = displayTime > 1;
const plurals = plural ? "s" : "";

// If the estimated time was captured over 10 minutes ago, remain silent.
var timeOfEstimate = json.timestamp ? json.timestamp : 0;
if (Date.now()/1000 - timeOfEstimate > 600) {
content = "";
}
const displayText =
docLang[0] === "es"
? `El tiempo aproximado de espera es <strong>${displayTime} minuto${plurals}</strong>.`
: `The current estimated wait time is <strong>${displayTime} minute${plurals}</strong>.`;

jQuery('#callCenterTime').html(content);
}
},
"error": function (xhr, status, error) {
console.log('fail');
console.log(error);
jQuery('#callCenterTime').html(error);
}
});
}
});
jQuery('div[data-call-wait="showTime"]').replaceWith(
`<div class="paragraph paragraph--type--uswds-alert paragraph--view-mode--embed paragraph--id--2485 usa-alert usa-alert--slim usa-alert--no-icon" data-call-wait='showTime'><div class="usa-alert__body"> <div class="field field--name-field-alert-body field--type-text field--label-hidden field__item">${displayText}</div> </div> </div>`
);
}

if (jQuery('#callCenterTime').length > 0) {
waittime();
setInterval(function() {
waittime();
}, 60 * 1000);
// upgrade: only apply to phone pages using Drupal library in usagov.libraries.yml
if (checkPagePath()) {
getCallCenterWaitTime();
}
});
});

0 comments on commit 2fcf74e

Please sign in to comment.