turnero: countdown en padre para que alerta sobreviva al cerrar el modal

El iframe pasa targetMs al padre via postMessage. El padre corre su propio
intervalo (5s) que dispara la notificación si el modal está cerrado.
Si el iframe alcanza el 0 primero, cancela el del padre para no duplicar.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-21 09:38:25 -05:00
co-authored by Claude Sonnet 4.6
parent 019d2f4fb0
commit 2cd7525bb4
2 changed files with 27 additions and 6 deletions
+26 -5
View File
@@ -1538,18 +1538,39 @@ function cerrarModalConsentimiento(forzar) {
if (turnoActivo) actualizarConsentimientos(turnoActivo.id);
}
var _mpParentCdTimer = null;
function _mpParentNotificar(lbl) {
if ('Notification' in window && Notification.permission === 'granted') {
new Notification('⏰ Toma de muestras', { body: lbl || '¡Siguiente muestra!', icon: '/favicon.ico', requireInteraction: true });
}
}
function _mpParentCancelCd() {
if (_mpParentCdTimer) { clearInterval(_mpParentCdTimer); _mpParentCdTimer = null; }
}
window.addEventListener('message', function(e) {
if (e.data && e.data.type === 'tomaProgresivaIniciada') {
_tomaProgresivaActiva = true;
if ('Notification' in window && Notification.permission === 'default') Notification.requestPermission();
}
if (e.data && e.data.type === 'tomaProgresivaAlerta') {
var lbl = (e.data.label || '¡Siguiente muestra!');
if ('Notification' in window && Notification.permission === 'granted') {
new Notification('⏰ Toma de muestras', { body: lbl, icon: '/favicon.ico', requireInteraction: true });
// Countdown en el padre para que sobreviva si el modal se cierra
_mpParentCancelCd();
if (e.data.targetMs) {
var _pTarget = e.data.targetMs, _pLabel = e.data.label || '';
_mpParentCdTimer = setInterval(function() {
if (Date.now() >= _pTarget) {
_mpParentCancelCd();
_mpParentNotificar(_pLabel);
}
}, 5000);
}
}
if (e.data && e.data.type === 'tomaProgresivaAlerta') {
// El iframe ya disparó la alerta — cancelar el del padre para no duplicar
_mpParentCancelCd();
_mpParentNotificar(e.data.label || '');
}
if (e.data && e.data.type === 'turneroFirmado') {
_mpParentCancelCd();
_tomaProgresivaActiva = false;
_setBtnCerrarConsent(true);
cerrarModalConsentimiento(true);
+1 -1
View File
@@ -2180,7 +2180,7 @@ function _mpIniciarCountdown(targetMsOrMins, nextLabel, nextFirmaId) {
if (box._mpTick) clearInterval(box._mpTick);
// Notificar al padre que bloquee el cierre
try { window.parent.postMessage({ type: 'tomaProgresivaIniciada' }, '*'); } catch(e) {}
try { window.parent.postMessage({ type: 'tomaProgresivaIniciada', targetMs: targetMs, label: nextLabel || '' }, '*'); } catch(e) {}
var _tcCdEl = nextFirmaId ? document.getElementById('tc-cd-' + nextFirmaId) : null;