-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (131 loc) · 4.73 KB
/
script.js
File metadata and controls
151 lines (131 loc) · 4.73 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 3. script.js
// Funções para renderizar a interface e lidar com interações
const app = document.getElementById('app');
const tokenKey = 'jwt_token';
function getToken() {
return localStorage.getItem(tokenKey);
}
function setToken(token) {
localStorage.setItem(tokenKey, token);
}
function removeToken() {
localStorage.removeItem(tokenKey);
}
// Verificar hash para token após redirect
function checkHashForToken() {
const hash = window.location.hash.substring(1);
if (hash.startsWith('token=')) {
const token = hash.split('=')[1];
setToken(token);
window.location.hash = ''; // Limpar hash
}
}
// Renderizar tela de login
function renderLogin() {
app.innerHTML = `
<h1>Bem-vindo ao CodeShelf</h1>
<button id="login-btn">Login com GitHub</button>
`;
document.getElementById('login-btn').addEventListener('click', () => {
window.location.href = '/api/auth/github';
});
}
// Renderizar perfil e repositórios
async function renderProfileAndRepos() {
const token = getToken();
if (!token) {
renderLogin();
return;
}
try {
// Obter perfil
const profileRes = await fetch('/api/profile/get', {
headers: { 'Authorization': `Bearer ${token}` }
});
if (!profileRes.ok) throw new Error('Unauthorized');
const user = await profileRes.json();
// Obter repositórios
const reposRes = await fetch('/api/getRepos', {
headers: { 'Authorization': `Bearer ${token}` }
});
const repos = await reposRes.json();
app.innerHTML = `
<div class="profile">
<img src="${user.avatar_url}" alt="${user.name}">
<h2>${user.name || user.login}</h2>
<button id="logout-btn">Logout</button>
</div>
<input id="search-input" type="text" placeholder="Buscar repositórios...">
<div id="repos-grid" class="repos-grid"></div>
<div id="message"></div>
`;
document.getElementById('logout-btn').addEventListener('click', () => {
removeToken();
renderLogin();
});
const searchInput = document.getElementById('search-input');
const reposGrid = document.getElementById('repos-grid');
const message = document.getElementById('message');
function renderRepos(filteredRepos) {
reposGrid.innerHTML = '';
filteredRepos.forEach((repo, index) => {
const card = document.createElement('div');
card.className = 'repo-card';
card.style.animationDelay = `${index * 0.1}s`;
const imageUrl = `https://raw.githubusercontent.com/${repo.full_name}/main/codeshelf-custom-image.jpg?${Date.now()}`;
card.innerHTML = `
<img src="${imageUrl}" alt="Custom Image" onerror="this.src='https://via.placeholder.com/300x150?text=No+Image';">
<h3>${repo.name}</h3>
<p>${repo.description || 'Sem descrição'}</p>
<p>Linguagem: ${repo.language || 'N/A'}</p>
<a href="${repo.html_url}" target="_blank" class="open-btn">Abrir no GitHub</a>
<div class="file-upload">
<label for="file-${repo.id}">Upload Imagem Personalizada</label>
<input type="file" id="file-${repo.id}" accept="image/*">
</div>
`;
reposGrid.appendChild(card);
const fileInput = document.getElementById(`file-${repo.id}`);
fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append('image', file);
formData.append('repo', repo.full_name);
try {
const res = await fetch('/api/saveRepo', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}` },
body: formData
});
if (!res.ok) throw new Error('Erro ao salvar');
message.className = 'message success';
message.textContent = 'Imagem salva com sucesso!';
// Atualizar imagem
card.querySelector('img').src = imageUrl;
} catch (err) {
message.className = 'message error';
message.textContent = err.message;
}
setTimeout(() => { message.textContent = ''; }, 3000);
});
});
}
renderRepos(repos);
searchInput.addEventListener('input', (e) => {
const query = e.target.value.toLowerCase();
const filtered = repos.filter(repo => repo.name.toLowerCase().includes(query));
renderRepos(filtered);
});
} catch (err) {
removeToken();
renderLogin();
}
}
// Inicializar
checkHashForToken();
if (getToken()) {
renderProfileAndRepos();
} else {
renderLogin();
}