forked from SimplesIP/pabx-app
Douglas.Strappasson
8 months ago
12 changed files with 317 additions and 27 deletions
@ -1,23 +1,95 @@ |
|||||||
// Use um seletor de classe comum para todos os botões
|
import ApiConsumer from "./ApiConsumer.js"; |
||||||
document.addEventListener('click', async function(event) { |
|
||||||
const botao = event.target.closest('.botao-enviar');
|
document.addEventListener('click', async function (event) { |
||||||
|
const botao = event.target.closest('.botao-enviar'); |
||||||
|
let imgElement;
|
||||||
|
|
||||||
if (botao) { |
if (botao) { |
||||||
try { |
try { |
||||||
const idContract = botao.dataset.id; |
// Verificar se a requisição já está em andamento
|
||||||
const url = `/crm/controller/ClickSignController.php?idContract=${idContract}`; |
if (botao.dataset.processando === 'true') { |
||||||
const response = await fetch(url); |
alert('Aguarde o fim do processamento de envio.'); |
||||||
|
return; |
||||||
if (!response.ok) { |
} |
||||||
throw new Error(`Erro na requisição: ${response.statusText}`); |
|
||||||
|
botao.dataset.processando = 'true'; |
||||||
|
imgElement = botao.querySelector('img'); |
||||||
|
var originalTitle = imgElement.getAttribute('title'); |
||||||
|
|
||||||
|
// Alterar o ícone imediatamente após o clique
|
||||||
|
if (imgElement) { |
||||||
|
// Salvar o src original para restaurar mais tarde
|
||||||
|
const srcOriginal = imgElement.src; |
||||||
|
|
||||||
|
// Alterar o src da imagem para 'imgSite/wait.svg'
|
||||||
|
imgElement.src = 'imgSite/wait.svg'; |
||||||
|
|
||||||
|
// Alterar o título da imagem enquanto processa o envio
|
||||||
|
imgElement.setAttribute('title', 'Processando envio...'); |
||||||
|
|
||||||
|
const idContract = botao.dataset.id; |
||||||
|
const url = `/crm/controller/ClickSignController.php?idContract=${idContract}`; |
||||||
|
|
||||||
|
// Configurar o tempo limite para 30 segundos (em milissegundos)
|
||||||
|
const timeoutPromise = new Promise((_, reject) => { |
||||||
|
setTimeout(() => reject(new Error('Tempo limite de 30 segundos excedido')), 30000); |
||||||
|
}); |
||||||
|
|
||||||
|
// Executar a requisição e o timeout em paralelo e esperar pelo primeiro que for resolvido
|
||||||
|
const response = await Promise.race([fetch(url), timeoutPromise]); |
||||||
|
|
||||||
|
if (!response.ok) { |
||||||
|
throw new Error(`Erro na requisição: ${response.statusText}`); |
||||||
|
} |
||||||
|
|
||||||
|
const data = await response.json(); |
||||||
|
alert(data['response']); |
||||||
|
console.log('Resposta do PHP:', data); |
||||||
|
|
||||||
|
if (isSendedToClicksign(idContract)) { |
||||||
|
imgElement.src = 'imgSite/sended.svg'; |
||||||
|
imgElement.setAttribute('title', 'Documento já enviado!'); |
||||||
|
} else { |
||||||
|
// Restaurar o ícone original e o título da imagem após a resposta terminar
|
||||||
|
imgElement.src = srcOriginal; |
||||||
|
imgElement.setAttribute('title', originalTitle); |
||||||
|
} |
||||||
|
} else { |
||||||
|
console.error('Elemento de imagem não encontrado dentro do botão.'); |
||||||
} |
} |
||||||
|
|
||||||
const data = await response.json(); |
|
||||||
alert(data['response']); |
|
||||||
console.log('Resposta do PHP:', data); |
|
||||||
|
|
||||||
} catch (error) { |
} catch (error) { |
||||||
console.error('Erro:', error.message); |
console.error('Erro:', error.message); |
||||||
|
const srcError = 'imgSite/error_sync.svg'; |
||||||
|
|
||||||
|
// Em caso de erro, altera o ícone e o título da imagem
|
||||||
|
if (imgElement) { |
||||||
|
imgElement.src = srcError; |
||||||
|
imgElement.setAttribute('title', 'Houve uma falha!'); |
||||||
|
alert('Houve uma falha no envio. Por favor, tente novamente.'); |
||||||
|
} else { |
||||||
|
console.error('Elemento de imagem não encontrado dentro do botão.'); |
||||||
|
} |
||||||
|
} finally { |
||||||
|
// Marcar que a requisição terminou
|
||||||
|
botao.dataset.processando = 'false'; |
||||||
} |
} |
||||||
} |
} |
||||||
}); |
}); |
||||||
|
|
||||||
|
|
||||||
|
async function isSendedToClicksign(idContract) { |
||||||
|
const response = await ApiConsumer.getSendedStatus(idContract); |
||||||
|
|
||||||
|
if (!response.ok) { |
||||||
|
throw new Error(`Erro na requisição: ${response.statusText}`); |
||||||
|
} |
||||||
|
|
||||||
|
const data = await response.json(); |
||||||
|
|
||||||
|
if (data['sended'] === true) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
Before Width: | Height: | Size: 933 B After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in new issue