-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresume.js
192 lines (163 loc) · 6.51 KB
/
resume.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const apiPath = "https://resume-back-zwhd.onrender.com/api";
function toggleDropdown(id) {
for (const dropdown of document.querySelectorAll('.dropdown-content')) {
if (dropdown.id !== id) {
dropdown.style.display = "none";
}
}
const dropdown = document.getElementById(id);
dropdown.style.display = dropdown.style.display === "block" ? "none" : "block";
}
document.querySelectorAll('.dropdown-option').forEach(option => {
option.addEventListener('click', function() {
const selectedOptionId = this.getAttribute('data-target');
const selectedOption = document.getElementById(selectedOptionId);
selectedOption.textContent = this.getAttribute('data-value');
const dropdownContent = this.closest('.dropdown-content');
dropdownContent.style.display = 'none';
});
});
document.addEventListener("DOMContentLoaded", function() {
const spinnerDiv = document.getElementById('spinner');
const contentDiv = document.getElementById('form-container');
contentDiv.hidden = true;
spinnerDiv.hidden = false;
getUserInfo();
contentDiv.hidden = false;
spinnerDiv.hidden = true;
// Prevent form submission on dropdown button click
var dropdownButtons = document.querySelectorAll(".dropdown-button");
dropdownButtons.forEach(function(button) {
button.addEventListener("click", function(event) {
event.preventDefault();
});
});
document.getElementById('resumeForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
submitResume();
});
const phoneInput = document.getElementById('phone');
phoneInput.addEventListener('input', function(event) {
let input = event.target.value;
// Remove all non-numeric characters
input = input.replace(/\D/g, '');
// Add parentheses and hyphen based on input length
if (input.length > 2) {
input = `(${input.slice(0, 2)}) ${input.slice(2, 7)}` + (input.length > 6 ? `${input.slice(7, 11)}` : '');
}
// Set the formatted value back to the input field
event.target.value = input;
});
});
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() {
let resumeData = {};
const accessToken = getCookie('access_token');
if (!accessToken) {
console.error('No access token found');
return;
}
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.error('Access denied. Invalid or expired token.');
} else {
console.error('Error fetching user info:', response.statusText);
}
return;
}
const data = await response.json();
if (data.error) {
console.error('No resume found for this user');
return;
} else {
resumeData = data;
}
} catch (error) {
console.error('Error fetching user info:', error);
return;
}
console.log(resumeData);
document.getElementById('name').value = resumeData.name || '';
document.getElementById('email').value = resumeData.email || '';
document.getElementById('phone').value = resumeData.phone || '';
document.getElementById('location').value = resumeData.location || '';
document.getElementById('birthdate').value = resumeData.birthdate || '';
document.getElementById('description').value = resumeData.description || '';
document.getElementById('selected-gender').textContent = resumeData.gender || '';
document.getElementById('selected-area').textContent = resumeData.area || '';
document.getElementById('selected-formacao').textContent = resumeData.formation || '';
document.getElementById('visibility-checkbox').checked = resumeData.public || false;
}
async function submitResume() {
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const phone = document.getElementById('phone').value;
const location = document.getElementById('location').value;
const birthdate = document.getElementById('birthdate').value;
const description = document.getElementById('description').value;
const checkbox = document.getElementById('visibility-checkbox');
const visibility = checkbox.checked;
console.log("visivel: " + visibility);
const gender = document.getElementById('selected-gender').textContent.trim();
if (!gender) {
alert('Please select a gender.');
return;
}
const area = document.getElementById('selected-area').textContent.trim();
if (!area) {
alert('Please select an area.');
return;
}
const formation = document.getElementById('selected-formacao').textContent.trim();
if (!formation) {
alert('Please select a formation.');
return;
}
fetch(`${apiPath}/update-resume`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('accessToken')}`
},
body: JSON.stringify({
name: name,
email: email,
phone: phone,
location: location,
birthdate: birthdate,
gender: gender,
area: area,
formation: formation,
description: description,
public: visibility
})
})
.then(response => response.json())
.then(data => {
if (data.error) {
console.error('Error submitting resume: ', data.error);
console.log('Error submitting resume. Please try again.');
} else {
console.log('Resume submitted successfully.');
window.location.href = 'user_page.html';
}
})
.catch((error) => {
console.error('Error submitting resume: ', error);
console.log('Error submitting resume. Please try again.');
});
}