export function blueCreateAlert (
alertMessage,
alertType,
alertId
) {
let alertClass;
switch (alertType) {
case 'success':
alertClass = 'alert-success';
break;
case 'info':
alertClass = 'alert-info';
break;
case 'warning':
alertClass = 'alert-warning';
break;
case 'error':
alertClass = 'alert-danger';
break;
default:
throw 'Invaild message type.';
}
const alert =
`
${alertMessage}
`;
return alert;
}
export function blueRemoveAlertById(alertId, timeToLive = 0) {
const alert = document.getElementById(alertId);
if (!alert) {
throw 'Alert not found.';
}
if (timeToLive > 0) {
const timeoutId = setTimeout(() => {
alert.remove();
clearTimeout(timeoutId);
}, timeToLive);
}
}
export function blueCreateAlertBuilder(selector = '#alerts') {
const alertsElmt = document.querySelector(selector);
if (!alertsElmt) {
throw 'Alerts section not found.';
}
return function (
type = 'info',
message,
timeToLive = 0
) {
const timestamp = new Date().getTime(),
alertId = `alert-${timestamp}`,
alert = blueCreateAlert(message, type, alertId);
alertsElmt.insertAdjacentHTML('afterbegin', alert);
if (timeToLive > 0) {
blueRemoveAlertById(alertId, timeToLive);
}
};
}