Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const form = document.getElementById('contactForm');
+ const messageDiv = document.getElementById('message');
+ const contactsList = document.getElementById('contacts');
+
+ form.addEventListener('submit', async (e) => {
+ e.preventDefault();
+
+ const formData = {
+ name: form.name.value,
+ email: form.email.value,
+ phone: form.phone.value,
+ notes: form.notes.value
+ };
+
+ try {
+ const response = await fetch('/api/contacts', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify(formData)
+ });
+
+ if (!response.ok) {
+ throw new Error('Failed to save contact');
+ }
+
+ showMessage('Contact saved successfully!', 'success');
+ form.reset();
+ loadContacts();
+ } catch (error) {
+ showMessage(error.message, 'error');
+ }
+ });
+
+ function showMessage(text, type) {
+ messageDiv.textContent = text;
+ messageDiv.className = `message ${type}`;
+ setTimeout(() => {
+ messageDiv.className = 'message';
+ }, 3000);
+ }
+
+ async function loadContacts() {
+ try {
+ const response = await fetch('/api/contacts');
+ const contacts = await response.json();
+
+ contactsList.innerHTML = contacts.map(contact => `
+
+
${contact.name}
+ ${contact.phone ? `
${contact.phone}
` : ''}
+ ${contact.notes ? `
${contact.notes}
` : ''}
+
+ `).join('');
+ } catch (error) {
+ showMessage('Failed to load contacts', 'error');
+ }
+ }
+
+ // Load contacts when page loads
+ loadContacts();
+ });