adicionado funcionalidade pra atualizar a lista de atendimentos no backend trazida pela api do electron, correções para telas no main e renderer
This commit is contained in:
parent
6e8ea6e52f
commit
eb5f91b8c9
|
|
@ -13,62 +13,6 @@
|
|||
<span id="count">0</span>
|
||||
</button>
|
||||
<script src="floating.js"></script>
|
||||
<script>
|
||||
$(function(){
|
||||
let apiUrl = 'https://autoatend.linco.work/api/v1/';
|
||||
let token = localStorage.getItem('authToken');
|
||||
let colabId = localStorage.getItem('idOperator');
|
||||
|
||||
function obtemProximos() {
|
||||
if (!token && !colabId) {
|
||||
console.warn("Token or colabId not found in localStorage. API requests will not be made.");
|
||||
return; // Stop the function if token or colabId is missing
|
||||
}
|
||||
|
||||
$.ajax(apiUrl + 'get-proximos/'+colabId, {
|
||||
method: 'GET',
|
||||
headers: { 'Authorization': 'Bearer '+token },
|
||||
// data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
dataType: 'JSON',
|
||||
success: function(response) {
|
||||
console.log('Resposta:', response);
|
||||
// Ensure the response is valid JSON before parsing
|
||||
try {
|
||||
let proximos = response;
|
||||
localStorage.setItem('proximos', JSON.stringify(proximos));
|
||||
let count = proximos.length;
|
||||
$('#float-button span#count').text(count); // Atualiza o texto do botão. Use .text() instead of .html()
|
||||
$('#float-button').removeClass(['without-items', 'has-items']).addClass( count > 0 ? 'has-items' : 'without-items');
|
||||
|
||||
} catch (e) {
|
||||
console.error("Error parsing JSON response:", e);
|
||||
console.error("Response text:", response); // Log the raw response for debugging
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Erro na requisição:', status, error);
|
||||
console.error('Response Text:', xhr.responseText); // Log the response text for debugging
|
||||
// Optionally, handle different error codes:
|
||||
if (xhr.status === 401) {
|
||||
console.warn("Unauthorized. Token might be invalid.");
|
||||
// You could redirect the user to a login page here.
|
||||
} else if (xhr.status === 404) {
|
||||
console.warn("Resource not found. Check the API endpoint.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Call obtemProximos initially
|
||||
obtemProximos();
|
||||
|
||||
// Set interval to call obtemProximos every 10 seconds
|
||||
setInterval(obtemProximos, 30000);
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -3,7 +3,7 @@ const countSpan = document.getElementById('count');
|
|||
|
||||
// Atualiza a contagem e a cor do botão quando recebe do main process
|
||||
window.electronAPI.onUpdateCount((value) => {
|
||||
countSpan.textContent = value;
|
||||
countSpan.innerHTML = value ?? 0;
|
||||
// Verifica a contagem para mudar a cor
|
||||
if (value > 0) {
|
||||
floatButton.classList.add('has-items'); // Adiciona a classe para cor avermelhada
|
||||
|
|
|
|||
69
index.html
69
index.html
|
|
@ -6,14 +6,6 @@
|
|||
<title>Aguardando atendimento</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<script src="js/jquery/jquery.js"></script>
|
||||
<style>
|
||||
body {
|
||||
margin: 10px;
|
||||
padding: 0;
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f4f4f4;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="list-view">
|
||||
|
|
@ -23,80 +15,27 @@
|
|||
<!-- Itens serão carregados aqui -->
|
||||
</ul>
|
||||
<button id="next-button" disabled>Iniciar atendimento</button>
|
||||
<button id="sendto-button" disabled>Encaminhar</button>
|
||||
<!-- <button id="sendto-button" disabled>Encaminhar</button> -->
|
||||
<button id="logout-button">Trocar Colaborador</button>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="obs-view" style="display: none;">
|
||||
<h1>Observações</h1>
|
||||
<p>Item selecionado: <span id="selected-item-name"></span></p>
|
||||
<p>Atendendo: <span id="selected-item-name"></span></p>
|
||||
<input type="hidden" name="idAtend" id="idAtend">
|
||||
<textarea id="observation-text" rows="10" cols="50" placeholder="Digite suas observações..."></textarea>
|
||||
<button id="save-button">Salvar</button>
|
||||
</div>
|
||||
|
||||
<div id="encaminhar-view" style="display: none;">
|
||||
<h1>Observações</h1>
|
||||
<p>Item selecionado: <span id="selected-item-name"></span></p>
|
||||
<p>Atendendo: <span id="selected-item-name"></span></p>
|
||||
<textarea id="observation-text" rows="10" cols="50" placeholder="Digite suas observações..."></textarea>
|
||||
<button id="save-button">Salvar</button>
|
||||
</div>
|
||||
|
||||
<script src="renderer.js"></script>
|
||||
<script>
|
||||
$(function(){
|
||||
|
||||
let apiUrl = 'https://autoatend.linco.work/api/v1/';
|
||||
let token = localStorage.getItem('authToken');
|
||||
let colabId = localStorage.getItem('idOperator');
|
||||
|
||||
function obtemProximosLocalStorage(){
|
||||
let datastorage = localStorage.getItem('proximos');
|
||||
|
||||
// Adiciona os outros itens apenas para visualização (opcional)
|
||||
const proximos = JSON.parse(datastorage);
|
||||
|
||||
itemList.innerHTML = ''; // Limpa a lista anterior
|
||||
if (!proximos || proximos.length === 0) {
|
||||
itemList.innerHTML = '<li>Nenhum item encontrado.</li>';
|
||||
nextButton.disabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Seleciona o primeiro item por padrão (ou o próximo disponível)
|
||||
// Aqui, vamos apenas pegar o primeiro da lista atual
|
||||
const itemToProcess = proximos[0]; // Pega o primeiro item
|
||||
if (itemToProcess) {
|
||||
selectedItemId = itemToProcess.id;
|
||||
selectedItemName = itemToProcess.clientName;
|
||||
const li = document.createElement('li');
|
||||
li.textContent = `${itemToProcess.senhaGen}: ${itemToProcess.clientName.toUpperCase()} - ${itemToProcess.attendanceType.toUpperCase()} - ${itemToProcess.descricaoServico.toUpperCase()}`;
|
||||
li.dataset.id = itemToProcess.id; // Armazena o ID no elemento
|
||||
li.classList.add('selected'); // Marca como selecionado visualmente (precisa de CSS)
|
||||
itemList.appendChild(li);
|
||||
nextButton.disabled = false;
|
||||
} else {
|
||||
itemList.innerHTML = '<li>Nenhum item para processar.</li>';
|
||||
nextButton.disabled = true;
|
||||
selectedItemId = null;
|
||||
selectedItemName = '';
|
||||
}
|
||||
|
||||
// Adiciona os outros itens apenas para visualização (opcional)
|
||||
proximos.slice(1).forEach(item => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = `${item.senhaGen}: ${item.clientName.toUpperCase()} - ${item.attendanceType.toUpperCase()} - ${item.descricaoServico.toUpperCase()}`;
|
||||
itemList.appendChild(li);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
setTimeout(()=>{
|
||||
obtemProximosLocalStorage();
|
||||
},5000);
|
||||
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
106
main.js
106
main.js
|
|
@ -17,14 +17,14 @@ const apiUrl = 'https://autoatend.linco.work/api/v1/';
|
|||
|
||||
autoUpdater.autoDownload = false;
|
||||
autoUpdater.autoInstallOnAppQuit = true;
|
||||
// autoUpdater.autoDownloadOnStartup = true;
|
||||
|
||||
|
||||
if(!pjson.isBuildNow){
|
||||
require('electron-reload')(__dirname,{
|
||||
electron: require(`${__dirname}/node_modules/electron`)
|
||||
})
|
||||
}
|
||||
//// carrega o electron-reload, buga ao salvar o main.js por conta do fetchDataFromAPI
|
||||
// if(!pjson.isBuildNow){
|
||||
// require('electron-reload')(__dirname,{
|
||||
// electron: require(`${__dirname}/node_modules/electron`)
|
||||
// })
|
||||
// }
|
||||
|
||||
// Função modificada para buscar dados da API
|
||||
async function readData() {
|
||||
|
|
@ -39,25 +39,91 @@ async function readData() {
|
|||
}
|
||||
|
||||
// Tenta buscar dados da API
|
||||
return await fetchDataFromAPI(token,colabId); //não usada aqui vai ser disparada via jquery dentro do modulo
|
||||
return await fetchDataFromAPI();
|
||||
|
||||
} catch (error) {
|
||||
console.error("Erro ao buscar dados da API:", error);
|
||||
|
||||
// Fallback: tenta ler do arquivo local em caso de falha na API
|
||||
try {
|
||||
const rawData = fs.readFileSync(dataPath, 'utf-8');
|
||||
return JSON.parse(rawData);
|
||||
} catch (localError) {
|
||||
console.error("Erro ao ler data.json local:", localError);
|
||||
return []; // Retorna array vazio em caso de erro
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Função para buscar dados da API
|
||||
async function fetchDataFromAPI(token,colabId) {
|
||||
//refactorado
|
||||
async function fetchDataFromAPI() {
|
||||
//executa uma vez e a cada 30 segundos
|
||||
getAndUpdateDataStorage(); setInterval(()=>{
|
||||
getAndUpdateDataStorage();
|
||||
},30000);
|
||||
}
|
||||
|
||||
// Função para coletar a lista de atendimentos do servidor, vai ser chamada uma vez e a cada 30s
|
||||
async function getAndUpdateDataStorage (){
|
||||
|
||||
const token = await getAuthToken();
|
||||
const colabId = await floatingWin.webContents.executeJavaScript("localStorage.getItem('idOperator')")
|
||||
|
||||
const url = apiUrl + 'get-proximos/'+colabId; // URL de exemplo para enviar a solicitação
|
||||
|
||||
if (!token && !colabId) {
|
||||
console.warn("Token or colabId not found in localStorage. API requests will not be made.");
|
||||
return; // Stop the function if token or colabId is missing
|
||||
}
|
||||
|
||||
const request = net.request({
|
||||
method: 'GET',
|
||||
url: url,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
}
|
||||
});
|
||||
|
||||
request.on('response', (response) => {
|
||||
let rawData = '';
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
rawData += chunk;
|
||||
});
|
||||
|
||||
response.on('end', () => {
|
||||
try {
|
||||
const parsedData = JSON.parse(rawData);
|
||||
let proximos = parsedData;
|
||||
|
||||
if (response.statusCode === 200) {
|
||||
if(!mainWin.isVisible){
|
||||
//envia os dados para o loadData atualiza a tela somente se não estiver sendo exibida
|
||||
mainWin.webContents.send('load-data', proximos);
|
||||
}
|
||||
//grava os registros no localstorage
|
||||
floatingWin.webContents.executeJavaScript("localStorage.setItem('proximos','"+JSON.stringify(proximos)+"')");
|
||||
let count = proximos.length;
|
||||
//lista a contagem no botão flutuante
|
||||
floatingWin.webContents.send('update-count', count);
|
||||
console.log(`Dados carregados com sucesso!`);
|
||||
} else {
|
||||
console.error(`Erro na requisição: Status code ${response.statusCode}`, parsedData);
|
||||
// Lidar com o erro adequadamente, talvez enviando uma mensagem para a janela principal
|
||||
mainWin.webContents.send('api-error', {
|
||||
message: `Erro ao chamar atendimentos: ${parsedData.message || 'Erro desconhecido'}`
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Erro ao analisar a resposta JSON:", error);
|
||||
mainWin.webContents.send('api-error', {
|
||||
message: `Erro ao processar resposta do servidor.`
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
request.on('error', (error) => {
|
||||
console.error("Erro na requisição:", error);
|
||||
mainWin.webContents.send('api-error', {
|
||||
message: `Erro ao chamar atendimento: ${error.message}`
|
||||
});
|
||||
});
|
||||
|
||||
request.end();
|
||||
|
||||
}
|
||||
|
||||
// Função para verificar se o token existe no localStorage
|
||||
|
|
@ -468,8 +534,8 @@ ipcMain.on('iniciar-atendimento', (event, itemId) => {
|
|||
});
|
||||
|
||||
request.on('response', (response) => {
|
||||
console.log(`STATUS: ${response.statusCode}`);
|
||||
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
|
||||
// console.log(`STATUS: ${response.statusCode}`);
|
||||
// console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
|
||||
response.on('data', (chunk) => {
|
||||
console.log(`BODY: ${chunk}`);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"name": "autoatendcolab",
|
||||
"version": "1.0.0",
|
||||
"main": "main.js",
|
||||
"isBuildNow": false,
|
||||
"isBuildNow": true,
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"nodemon": "nodemon --exec electron .",
|
||||
|
|
|
|||
18
preload.js
18
preload.js
|
|
@ -1,23 +1,27 @@
|
|||
const { app, contextBridge, ipcRenderer } = require('electron');
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
onLoadData: (callback) => ipcRenderer.on('load-data', (_event, value) => callback(value)),
|
||||
//carrega todos os atendimentos na fila
|
||||
onLoadData: (callback) => ipcRenderer.on('load-data', (_event, data) => callback(data)),
|
||||
|
||||
//seleciona o atendimento atual
|
||||
selectAtendID: (callback) => ipcRenderer.on('select-atend-id', (_event, data) => callback(data)),
|
||||
|
||||
//inicia o atendimento atual
|
||||
iniciaAtendimento: (itemId) => ipcRenderer.send('iniciar-atendimento', itemId),
|
||||
|
||||
logoutApp: () => ipcRenderer.send('logout'),
|
||||
//salva a observação do atendimento
|
||||
saveObservation: (data) => ipcRenderer.send('save-observation', data),
|
||||
|
||||
//sai do colaborador atual
|
||||
logoutApp: () => ipcRenderer.send('logout'),
|
||||
|
||||
|
||||
//sistema de atualizações
|
||||
updVersion: (callback) => ipcRenderer.on('update_version',(_event,arg) => callback(arg)),
|
||||
updMessage: (callback) => ipcRenderer.on('update_message', (_event,data) => callback(data)),
|
||||
updPercent: (callback) => ipcRenderer.on('update_percent', (_event,data) => callback(data)),
|
||||
atualVersion: (callback) => ipcRenderer.on('atual_version', (_event,data) => callback(data)),
|
||||
updNVersion: (callback) => ipcRenderer.on('update_new_version', (_event,data) => callback(data)),
|
||||
|
||||
// updateMessage: (data) => ipcRenderer.send('update_message', data),
|
||||
// updPercent: (data) => ipcRenderer.send('update_percent', data),
|
||||
// atualVersion: (data) => ipcRenderer.send('atual_version', data),
|
||||
// updNVersion: (data) => ipcRenderer.send('update_new_version', data),
|
||||
|
||||
});
|
||||
84
renderer.js
84
renderer.js
|
|
@ -8,34 +8,47 @@ const observationText = document.getElementById('observation-text');
|
|||
const saveButton = document.getElementById('save-button');
|
||||
const selectedItemNameSpan = document.getElementById('selected-item-name');
|
||||
const queueNumber = document.getElementById('queue-number');
|
||||
const idAtend = document.getElementById('idAtend');
|
||||
|
||||
let currentData = [];
|
||||
let selectedItemId = null;
|
||||
let selectedItemName = '';
|
||||
|
||||
// window.electronAPI.onLoadData(() => {
|
||||
// populateList();
|
||||
// // Reseta a view para a lista sempre que os dados são carregados
|
||||
// showListView();
|
||||
// });
|
||||
window.electronAPI.onLoadData((data) => {
|
||||
if(!data){
|
||||
return;
|
||||
}
|
||||
// Reseta a view para a lista sempre que os dados são carregados
|
||||
populateList(data[0]);
|
||||
showListView();
|
||||
});
|
||||
|
||||
//chama o proximo da fila ao abrir a janela de atendimentos
|
||||
window.electronAPI.selectAtendID((data)=>{
|
||||
selectedItemId = data.id;
|
||||
queueNumber.innerHTML = data ? 'Chamando: '+ /*data.senhaGen + ' - '+*/ data.clientName.toUpperCase() + ' - ' + data.descricaoServico.toUpperCase() : 'Ninguem aguardando atendimento';
|
||||
if(!data){
|
||||
queueNumber.innerHTML = 'Ninguem aguardando atendimento';
|
||||
window.close();
|
||||
return;
|
||||
}
|
||||
// Reseta a view para a lista sempre que os dados são carregados ao clicar no botão para abrir a janela
|
||||
populateList(data);
|
||||
showListView();
|
||||
selectedItemId = data.id ?? null;
|
||||
queueNumber.innerHTML = data ? 'NA VEZ: '+ /*data.senhaGen + ' - '+*/ data.clientName.toUpperCase() + ' - ' + data.descricaoServico.toUpperCase() : 'Ninguem aguardando atendimento';
|
||||
|
||||
});
|
||||
|
||||
// Função para popular a lista de itens
|
||||
function populateList() {
|
||||
function populateList(currentData) {
|
||||
let datastorage = localStorage.getItem('proximos');
|
||||
|
||||
// Adiciona os outros itens apenas para visualização (opcional)
|
||||
const proximos = JSON.parse(datastorage);
|
||||
|
||||
|
||||
itemList.innerHTML = ''; // Limpa a lista anterior
|
||||
if (!proximos || proximos.length === 0) {
|
||||
if (!proximos || proximos.length === 0 || !currentData) {
|
||||
itemList.innerHTML = '<li>Nenhum item encontrado.</li>';
|
||||
nextButton.disabled = true;
|
||||
nextButton.disabled = !currentData;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +66,7 @@ function populateList() {
|
|||
nextButton.disabled = false;
|
||||
} else {
|
||||
itemList.innerHTML = '<li>Nenhum item para processar.</li>';
|
||||
nextButton.disabled = true;
|
||||
nextButton.disabled = !currentData;
|
||||
selectedItemId = null;
|
||||
selectedItemName = '';
|
||||
}
|
||||
|
|
@ -61,7 +74,8 @@ function populateList() {
|
|||
// Adiciona os outros itens apenas para visualização (opcional)
|
||||
proximos.slice(1).forEach(item => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = `${item.senhaGen}: ${item.clientName.toUpperCase()} - ${item.attendanceType.toUpperCase()} - ${item.descricaoServico.toUpperCase()}`;
|
||||
//${item.senhaGen}:
|
||||
li.textContent = `${item.clientName.toUpperCase()} - ${item.attendanceType.toUpperCase()} - ${item.descricaoServico.toUpperCase()}`;
|
||||
itemList.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
|
@ -69,13 +83,10 @@ function populateList() {
|
|||
|
||||
//mostra a tela de listagem e permite iniciar o atendimento
|
||||
function showListView() {
|
||||
// populateList();
|
||||
|
||||
listView.style.display = 'block';
|
||||
encaminharView.style.display = 'none';
|
||||
observationView.style.display = 'none';
|
||||
observationText.value = ''; // Limpa a textarea
|
||||
|
||||
// nextButton.disabled = !selectedItemId; // Habilita/desabilita baseado na seleção
|
||||
}
|
||||
|
||||
|
|
@ -121,44 +132,3 @@ logoutButton.addEventListener('click',()=>{
|
|||
// });
|
||||
|
||||
// Inicialmente, mostra a view da lista (estará vazia até receber dados)
|
||||
|
||||
|
||||
|
||||
//não vai ser usado timer pois é usado no floatbuttom, ele atualiza com timer
|
||||
//essa função vai ser chamada apenas quando algum registro já estiver sido atendido
|
||||
function updRemoteList() {
|
||||
if (!token && !colabId) {
|
||||
console.warn("Token or colabId not found in localStorage. API requests will not be made.");
|
||||
return; // Stop the function if token or colabId is missing
|
||||
}
|
||||
|
||||
$.ajax(apiUrl + 'get-proximos/'+colabId, {
|
||||
method: 'GET',
|
||||
headers: { 'Authorization': 'Bearer '+token },
|
||||
processData: false,
|
||||
contentType: false,
|
||||
dataType: 'JSON',
|
||||
success: function(response) {
|
||||
console.log('Resposta:', response);
|
||||
// Ensure the response is valid JSON before parsing
|
||||
try {
|
||||
localStorage.setItem('proximos', JSON.stringify(response));
|
||||
} catch (e) {
|
||||
console.error("Error parsing JSON response:", e);
|
||||
console.error("Response text:", response); // Log the raw response for debugging
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Erro na requisição:', status, error);
|
||||
console.error('Response Text:', xhr.responseText); // Log the response text for debugging
|
||||
// Optionally, handle different error codes:
|
||||
if (xhr.status === 401) {
|
||||
console.warn("Unauthorized. Token might be invalid.");
|
||||
// You could redirect the user to a login page here.
|
||||
} else if (xhr.status === 404) {
|
||||
console.warn("Resource not found. Check the API endpoint.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
body {
|
||||
font-family: sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
padding: 10px;
|
||||
background-color: transparent; /* Para janela flutuante */
|
||||
overflow: hidden; /* Evita barras de rolagem indesejadas na flutuante */
|
||||
-webkit-app-region: drag;
|
||||
|
|
@ -104,7 +104,7 @@ button:disabled {
|
|||
}
|
||||
|
||||
textarea {
|
||||
width: 95%; /* Ajuste conforme necessário */
|
||||
width: calc(100% - 10px);
|
||||
margin-top: 10px;
|
||||
margin-bottom: 10px;
|
||||
border: 1px solid #ccc;
|
||||
|
|
|
|||
Loading…
Reference in New Issue