Repository navigation
Expand file tree
/
Copy pathairtable.js
More file actions
105 lines (94 loc) · 3.36 KB
/
Copy pathairtable.js
File metadata and controls
105 lines (94 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// COLAR SEU PERSONAL ACCESS TOKEN AQUI
const apiKey = "patj4YJMOCo2eKk5o.544fe3082198705feb14e9668bd92c73c7e39a7c36bdece90ca549298b813a25"; // << seu token completo
const baseId = "appWObWnrpNMXVv4I";
const tableNameOrId = "tblwXOoXiIbChDRWN";
const airtableUrlBase = `https://api.airtable.com/v0/${baseId}/${encodeURIComponent(tableNameOrId)}`;
// Listar usuários (paginação)
async function listarUsuarios() {
const container = document.getElementById("Usuarios");
if (!container) return console.warn("#Usuarios não encontrado");
container.innerText = "Carregando Usuários...";
try {
let allRecords = [];
let offset = undefined;
do {
const url = new URL(airtableUrlBase);
if (offset) url.searchParams.set("offset", offset);
url.searchParams.set("pageSize", 100);
const resp = await fetch(url.toString(), {
headers: { Authorization: `Bearer ${apiKey}` }
});
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
allRecords = allRecords.concat(data.records || []);
offset = data.offset;
} while (offset);
container.innerHTML = "";
if (allRecords.length === 0) { container.innerText = "Nenhum usuário encontrado."; return; }
allRecords.forEach(record => {
const f = record.fields || {};
const card = document.createElement("div");
card.className = "usuario-card";
card.innerHTML = `
<h3>${f.Nome || "Sem nome"}</h3>
<p><b>Email:</b> ${f.Email || "—"}</p>
<p><b>Telefone:</b> ${f.Telefone || "—"}</p>
<p><b>Tipo:</b> ${f.Tipo || "—"}</p>
`;
container.appendChild(card);
});
} catch (err) {
console.error(err);
container.innerText = "Erro ao carregar usuários. Veja console.";
}
}
// Criar usuário
async function criarUsuario(fields) {
const resp = await fetch(airtableUrlBase, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ records: [{ fields }] })
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
// Inicialização
document.addEventListener("DOMContentLoaded", () => {
listarUsuarios();
const form = document.getElementById("cadastroForm");
if (!form) return;
form.addEventListener("submit", async (ev) => {
ev.preventDefault();
const Nome = (document.getElementById("Nome").value || "").trim();
const Telefone = (document.getElementById("Telefone").value || "").trim();
const Email = (document.getElementById("Email").value || "").trim();
const Tipo = (document.getElementById("Tipo").value || "").trim();
const Senha = (document.getElementById("Senha").value || "").trim();
// const receber = document.getElementById("receber").checked ? "Sim" : "Não";
if (!Nome || !Email || !Tipo || !Senha) {
alert("Por favor preencha Nome, E-mail e Senha.");
return;
}
const fields = {
Nome: Nome,
// Telefone: telefone,
Email: Email,
Senha: Senha,
// ReceberNotificacoes: receber,
// Tipo: "Doador"
Senha: Senha,
};
try {
await criarUsuario(fields);
alert("Cadastro realizado com sucesso!");
form.reset();
listarUsuarios();
} catch (err) {
console.error(err);
alert("Erro ao cadastrar. Veja console.");
}
});
});