-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (154 loc) · 5.81 KB
/
script.js
File metadata and controls
164 lines (154 loc) · 5.81 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
152
153
154
155
156
157
158
159
160
161
162
163
164
$(document).ready(function() {
// handle the API key submission
$('#submit-api-key').click(function(event) {
event.preventDefault();
var apiKey = $('#api-key-input').val();
// Make a request to validate the API key
$.ajax({
url: "http://localhost:5555/proxy/cacheVcard",
headers: {
'x-api-key': apiKey
},
success: function(response) {
// API key is valid
console.log('API key is valid');
},
error: function(jqXHR) {
// API key is invalid
console.log('API key is invalid');
}
});
});
});
function uploadFile() {
const password = prompt('Enter Password:');
if (!password) {
return; // User canceled, do not proceed with the upload
}
// Check if the password is correct
if (password !== 'upload') {
alert('Invalid password');
return; // Do not proceed with the upload
}
var form = document.getElementById('upload-form');
var formData = new FormData(form);
fetch('https://flask-production-eba1.up.railway.app/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data);
alert('Upload successful!');
})
.catch(error => {
console.error(error);
alert('Upload failed!!');
});
}
const contactsList = document.getElementById('contacts-list');
let currentData = null;
// get called when button is pressed to fetch contacts data
function getContacts() {
if (currentData === 'contacts') {
// Clear the existing content in #contacts-list
contactsList.innerHTML = '';
currentData = null;
} else {
const password = prompt('Enter API Key');
fetch('https://asi2-cache2.onrender.com/proxy/cacheJson', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
password: password
})
})
.then(response => response.json())
.then(data => {
// Clear the existing content in #contacts-list
contactsList.innerHTML = '';
currentData = 'contacts';
for (let contact of data) {
const li = document.createElement('li');
li.innerHTML = `_id: ${contact._id.$oid}<br>
name: ${contact.name}<br>
email: ${contact.email}<br>
phone: ${contact.phone}<br>
address: ${contact.address}<br>
company: ${contact.company}<br>
fullname: ${contact.fullname}`;
contactsList.appendChild(li);
}
})
.catch(error => {
console.error('Error fetching contacts:', error);
});
}
}
// get called when button is pressed to fetch vCard data
function getContactsVcard() {
if (currentData === 'vcard') {
// Clear the existing content in #contacts-list
contactsList.innerHTML = '';
currentData = null;
} else {
const password = window.prompt('Enter password:');
if (password === null) {
// User clicked Cancel, do nothing
return;
}
fetch('https://asi2-cache2.onrender.com/proxy/cacheVcard', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
password
}),
})
.then(response => response.json())
.then(data => {
// Clear the existing content in #contacts-list
contactsList.innerHTML = '';
currentData = 'vcard';
for (let contact of data) {
const li = document.createElement('li');
const vcardLines = contact.vcard.split('\n').map(line => `${line}<br>`).join('');
li.innerHTML = `${vcardLines}<br>`;
contactsList.appendChild(li);
}
})
.catch(error => {
console.error('Error fetching contacts vCard:', error);
});
}
}
$(document).ready(function() {
// handle the form submission
$('#email-form').submit(function(event) {
// prevent the form from submitting normally
event.preventDefault();
// get the entered email
var email = $('#email-input').val();
// make a GET request to the API endpoint with the entered email
$.get("https://flask-production-eba1.up.railway.app/contacts/" + email, function(data) {
// format the JSON data with line breaks and display the result in the #email-output div
var contact = data;
var p = document.createElement('p');
p.innerHTML = `_id: ${contact._id.$oid}<br>
name: ${contact.name}<br>
email: ${contact.email}<br>
phone: ${contact.phone}<br>
address: ${contact.address}<br>
company: ${contact.company}<br>
fullname: ${contact.fullname}`;
$('#email-output').html(p);
})
.fail(function() {
// display an error message if the request fails
$('#email-output').text('Contact with email ' + email + ' does not exist');
});
});
});