-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_page.js
144 lines (122 loc) · 4.64 KB
/
user_page.js
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
const apiPath = "https://resume-back-zwhd.onrender.com/api";
document.addEventListener("DOMContentLoaded", function() {
getUserInfo();
document.getElementById('edit-resume').addEventListener('click', function(event) {
event.preventDefault();
window.location.href = 'resume.html';
});
});
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
return null;
}
async function getUserInfo() {
const contentDiv = document.getElementById('usr-page-content');
const spinnerDiv = document.getElementById('spinner');
contentDiv.hidden = true;
spinnerDiv.hidden = false;
const accessToken = getCookie('access_token');
if (!accessToken) {
console.error('No access token found');
return;
}
let resumeData = {};
let fetched = false;
try {
const response = await fetch(`${apiPath}/get-user-resume`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if (!response.ok) {
// Lidar com erros de resposta
if (response.status === 403) {
console.log('Access denied. Invalid or expired token.');
} else {
console.log('Error fetching user info:', response.statusText);
}
}
const data = await response.json();
if (data.error) {
console.log('No resume found for this user');
} else {
resumeData = data;
fetched = true;
}
} catch (error) {
console.log('Error fetching user info:', error);
}
if (fetched) {
const name = resumeData.name;
const email = resumeData.email;
const phone = resumeData.phone;
const location = resumeData.location;
const birthdate = resumeData.birthdate;
const gender = resumeData.gender;
const area = resumeData.area;
const formation = resumeData.formation;
const description = resumeData.description;
const public = resumeData.public;
let age = calcularIdade(birthdate);
let firstName = name.split(' ')[0];
document.getElementById('user-name').textContent = "Bem-vindo, " + firstName + "!";
document.getElementById('name').textContent = name;
document.getElementById('age').textContent = age;
document.getElementById('description').textContent = description;
document.getElementById('location').textContent = location;
document.getElementById('phone').textContent = phone;
document.getElementById('email').textContent = email;
document.getElementById('gender').textContent = gender;
document.getElementById('area').textContent = area;
document.getElementById('formation').textContent = formation;
document.getElementById('visibility').textContent = public ? "Sim" : "Não";
} else {
document.getElementById('resume-title').textContent = "Você ainda não possui um currículo cadastrado."
document.getElementById('edit-resume').textContent = "Criar currículo"
document.getElementById('generate-pdf').hidden = true;
let cardDiv = document.getElementById('resume-card');
cardDiv.innerHTML = "";
cardDiv.removeAttribute('class');
}
contentDiv.hidden = false;
spinnerDiv.hidden = true;
}
async function logout() {
const accessToken = getCookie('access_token');
try {
const response = await fetch(`${apiPath}/logout`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if (!response.ok) {
console.error('Error logging out:', response.statusText);
return;
}
const data = await response.json();
if (data.error) {
console.error('Error logging out:', data.error);
} else {
console.log('User logged out');
window.location.href = 'index.html';
}
} catch (error) {
console.error('Error logging out:', error);
}
}
function calcularIdade(birthdate) {
const hoje = new Date();
const dataNascimento = new Date(birthdate);
let idade = hoje.getFullYear() - dataNascimento.getFullYear();
const mes = hoje.getMonth() - dataNascimento.getMonth();
if (mes < 0 || (mes === 0 && hoje.getDate() < dataNascimento.getDate())) {
idade--;
}
return idade;
}