mirror of https://github.com/interlegis/sapl.git
5 changed files with 264 additions and 32 deletions
@ -0,0 +1,116 @@ |
|||||
|
{% extends "crud/detail.html" %} |
||||
|
{% load i18n %} |
||||
|
|
||||
|
{% block extra_actions %} |
||||
|
<div class="actions btn-group btn-group-sm ml-2" role="group"> |
||||
|
<button type="button" id="sync-painel-btn" class="btn btn-outline-primary"> |
||||
|
{% trans "Sincronizar Painel" %} |
||||
|
</button> |
||||
|
</div> |
||||
|
<span id="sync-painel-feedback" class="ml-2 small text-muted"></span> |
||||
|
|
||||
|
<script> |
||||
|
(function () { |
||||
|
const btn = document.getElementById('sync-painel-btn'); |
||||
|
if (!btn) { |
||||
|
console.warn('Sincronizar Painel: botão não encontrado na página.'); |
||||
|
return; |
||||
|
} |
||||
|
const feedback = document.getElementById('sync-painel-feedback'); |
||||
|
const endpoint = window.location.origin + "/api/sessao/sessaoplenaria/{{ view.object.pk }}/materias/enviar/"; |
||||
|
console.log('Sincronizar Painel: endpoint', endpoint); |
||||
|
|
||||
|
function getCsrfToken() { |
||||
|
const name = 'csrftoken='; |
||||
|
const decodedCookie = decodeURIComponent(document.cookie || ''); |
||||
|
const ca = decodedCookie.split(';'); |
||||
|
for (let i = 0; i < ca.length; i++) { |
||||
|
let c = ca[i].trim(); |
||||
|
if (c.indexOf(name) === 0) { |
||||
|
return c.substring(name.length, c.length); |
||||
|
} |
||||
|
} |
||||
|
return ''; |
||||
|
} |
||||
|
|
||||
|
function setStatus(message, isError) { |
||||
|
feedback.textContent = message || ''; |
||||
|
if (isError) { |
||||
|
feedback.classList.remove('text-muted'); |
||||
|
feedback.classList.add('text-danger'); |
||||
|
} else { |
||||
|
feedback.classList.remove('text-danger'); |
||||
|
feedback.classList.add('text-muted'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
btn.addEventListener('click', function () { |
||||
|
setStatus('{% trans "Enviando..." %}', false); |
||||
|
btn.disabled = true; |
||||
|
const body = {}; |
||||
|
// Exibe no console o payload que será enviado |
||||
|
try { |
||||
|
console.log('Sincronizar Painel - payload:', JSON.stringify(body)); |
||||
|
} catch (e) { |
||||
|
console.log('Sincronizar Painel - payload (obj):', body); |
||||
|
} |
||||
|
console.log('Sincronizar Painel: iniciando requisição'); |
||||
|
const doRequest = () => { |
||||
|
return fetch(endpoint, { |
||||
|
method: 'POST', |
||||
|
headers: { |
||||
|
'Content-Type': 'application/json', |
||||
|
'X-CSRFToken': getCsrfToken(), |
||||
|
}, |
||||
|
credentials: 'same-origin', |
||||
|
body: JSON.stringify(body) |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
const handleResponse = async (resp) => { |
||||
|
const statusCode = resp.status; |
||||
|
let msg = resp.ok ? '{% trans "Sincronizado com sucesso." %}' : '{% trans "Sincronizado (mensagem da API)." %}'; |
||||
|
let detail = ''; |
||||
|
|
||||
|
try { |
||||
|
const data = await resp.json(); |
||||
|
if (data && data.remote_status_code) { |
||||
|
// se 400 mas com mensagem de atualizado, trata como sucesso |
||||
|
if (data.remote_status_code === 400 && data.remote_response && typeof data.remote_response === 'string') { |
||||
|
const xmlMatch = data.remote_response.match(/<localizedMessage>(.*?)<\/localizedMessage>/i); |
||||
|
if (xmlMatch && xmlMatch[1]) { |
||||
|
detail = xmlMatch[1]; |
||||
|
msg = '{% trans "Sincronizado" %}'; |
||||
|
} else { |
||||
|
msg = '{% trans "Sincronizado" %}'; |
||||
|
} |
||||
|
} else { |
||||
|
msg += ` (HTTP ${data.remote_status_code})`; |
||||
|
} |
||||
|
} else { |
||||
|
msg += ` (HTTP ${statusCode})`; |
||||
|
} |
||||
|
} catch (jsonErr) { |
||||
|
msg += ` (HTTP ${statusCode})`; |
||||
|
} |
||||
|
|
||||
|
const finalMsg = detail ? `${msg} - ${detail}` : msg; |
||||
|
setStatus(finalMsg, false); |
||||
|
}; |
||||
|
|
||||
|
const handleError = (err) => { |
||||
|
setStatus('{% trans "Erro de rede ao sincronizar." %}', true); |
||||
|
console.error(err); |
||||
|
}; |
||||
|
|
||||
|
// usa fetch com credenciais; se falhar, loga o erro no console |
||||
|
doRequest() |
||||
|
.then(handleResponse) |
||||
|
.catch(handleError) |
||||
|
.finally(() => { |
||||
|
btn.disabled = false; |
||||
|
}); |
||||
|
}); |
||||
|
})(); |
||||
|
</script> |
||||
|
{% endblock extra_actions %} |
||||
Loading…
Reference in new issue