Skip to content

Commit

Permalink
Tela consulta usuários funcionando;
Browse files Browse the repository at this point in the history
Falta edita/excluir
  • Loading branch information
ramosbrp committed Jun 29, 2024
1 parent b6c8426 commit f9d2d67
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
39 changes: 39 additions & 0 deletions controllers/getUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use classes\DatabaseConnection;

require_once '../vendor/autoload.php';

function createDatabaseConnection()
{
// global $client; // Usar o cliente de telemetria global
try {
$db = new DatabaseConnection();
$conn = $db->getConnection();
// echo"<script>console.log('ok')</script>";

return $conn;

} catch (PDOException $e) {
// $client->trackException($e);
error_log("Error connecting to SQL Server: " . $e->getMessage());
die("Error connecting to SQL Server.");
}
}

function getUsers(){
$conn = createDatabaseConnection();
$query = "SELECT * FROM usuario";
$stms = $conn->query($query);
$stms->execute();

$results = $stms->fetchAll(PDO::FETCH_ASSOC);
return $results;
}

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// echo"<script>console.log('ok')</script>";
$responseArray = getUsers();

echo json_encode($responseArray);
}
26 changes: 26 additions & 0 deletions js/consultaUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Variáveis
const userTableBody = document.getElementById('user-table-body');

// Event Listeners
document.addEventListener('DOMContentLoaded', () => {
buscaUsers();
})

const buscaUsers = async () => {
const response = await fetch('../controllers/getUsers.php', {
method: 'GET'
});

if (!response.ok)
throw new Error("Error");

const data = await response.json();
data.reverse();
console.log(data);

data.forEach((item)=>{
var row = userTableBody.insertRow();
row.insertCell().textContent = item.nome;
row.insertCell().textContent = item.email;
});
}
47 changes: 3 additions & 44 deletions pages/consulta_usr.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,56 +63,15 @@ <h1 class="my-4">Consulta de Usuários</h1>
<th>Ações</th>
</tr>
</thead>
<tbody>
<tbody id="user-table-body" >
<!-- linhas de usuário aqui -->
</tbody>
</table>
</div>

<script>
// Função para exibir os usuários cadastrados na tabela de usuários
function displayUsers() {
var userTableBody = document.querySelector("#userTable tbody");

// Limpa as linhas da tabela antes de adicionar os usuários
userTableBody.innerHTML = "";

// Itera sobre os itens do localStorage
for (var i = 0; i < localStorage.length; i++) {
var username = localStorage.key(i); // Obtém o nome de usuário
var userDetails;

// Tenta analisar os detalhes do usuário do localStorage
try {
userDetails = JSON.parse(localStorage.getItem(username)); // Obtém os detalhes do usuário
} catch (error) {
console.error("Erro ao analisar os detalhes do usuário:", error);
continue; // Pula para o próximo usuário se ocorrer um erro de análise JSON
}

// Cria uma nova linha na tabela para o usuário
var newRow = userTableBody.insertRow();
newRow.innerHTML = `
<td>${username}</td>
<td>${userDetails.email}</td>
<td><button class="btn btn-sm btn-danger deleteBtn">Excluir</button></td>
`;
}

// Adiciona o evento de clique aos botões de exclusão
var deleteButtons = document.querySelectorAll(".deleteBtn");
deleteButtons.forEach(function (button) {
button.addEventListener("click", function () {
var usernameToDelete = button.parentElement.parentElement.cells[0].textContent;
localStorage.removeItem(usernameToDelete); // Remove o usuário do localStorage
displayUsers(); // Atualiza a tabela de usuários
});
});
}

window.onload = displayUsers;
</script>


<script src="../js/consultaUser.js"></script>
<script src="../js/script.js"></script>
<script type="module" src="../js/header.js"></script>
</body>
Expand Down

0 comments on commit f9d2d67

Please sign in to comment.