Spaces:
Running
Running
Ig0tU commited on
Commit ·
42d4b91
1
Parent(s): 9cd8722
Add Live Site integration, Phone/Subject fields, and origin tracking
Browse files- public/dashboard.html +6 -1
- public/live-site-integration.js +113 -0
- server.js +17 -7
public/dashboard.html
CHANGED
|
@@ -183,8 +183,13 @@
|
|
| 183 |
<span class="submission-name">\${data.name}</span> ·
|
| 184 |
<a href="mailto:\${data.email}" class="submission-email">\${data.email}</a>
|
| 185 |
</div>
|
| 186 |
-
<div>
|
|
|
|
|
|
|
|
|
|
| 187 |
</div>
|
|
|
|
|
|
|
| 188 |
<div class="submission-message">\${data.message.replace(/</g, "<").replace(/>/g, ">")}</div>
|
| 189 |
`;
|
| 190 |
|
|
|
|
| 183 |
<span class="submission-name">\${data.name}</span> ·
|
| 184 |
<a href="mailto:\${data.email}" class="submission-email">\${data.email}</a>
|
| 185 |
</div>
|
| 186 |
+
<div>
|
| 187 |
+
<span style="background:#e0e7ff; color:#3730a3; padding:2px 6px; border-radius:4px; font-size:12px; margin-right:8px;">\${data.origin}</span>
|
| 188 |
+
\${date}
|
| 189 |
+
</div>
|
| 190 |
</div>
|
| 191 |
+
\${data.phone ? \`<div style="font-size:13px; color:#666; margin-top:4px;">📞 \${data.phone}</div>\` : ''}
|
| 192 |
+
\${data.subject ? \`<div style="font-weight:600; margin-top:12px; font-size:15px;">Subject: \${data.subject}</div>\` : ''}
|
| 193 |
<div class="submission-message">\${data.message.replace(/</g, "<").replace(/>/g, ">")}</div>
|
| 194 |
`;
|
| 195 |
|
public/live-site-integration.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
(function () {
|
| 2 |
+
// ---------------------------------------------------------
|
| 3 |
+
// WALLAPI LIVE SITE INTEGRATION SCRIPT
|
| 4 |
+
// Include this file on your live site (e.g. contact.html)
|
| 5 |
+
// to hijack the existing forms and send them to the HF API.
|
| 6 |
+
// ---------------------------------------------------------
|
| 7 |
+
|
| 8 |
+
// 1. SET YOUR HUGGING FACE API URL HERE
|
| 9 |
+
const API_URL = 'https://acecalisto3-wallapi.hf.space/api/submit';
|
| 10 |
+
|
| 11 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 12 |
+
|
| 13 |
+
// Find all forms on the page that we want to hijack
|
| 14 |
+
// You might need to adjust this selector based on your exact HTML.
|
| 15 |
+
// The curl output showed: <form data-validate>
|
| 16 |
+
const forms = document.querySelectorAll('form[data-validate], .contact-form form');
|
| 17 |
+
|
| 18 |
+
console.log(`[WallAPI Integration] Found ${forms.length} forms to hook into.`);
|
| 19 |
+
|
| 20 |
+
forms.forEach(form => {
|
| 21 |
+
// Find the submit button inside this specific form
|
| 22 |
+
const submitBtn = form.querySelector('button[type="submit"], input[type="submit"]');
|
| 23 |
+
|
| 24 |
+
// Create a status message container if one doesn't exist
|
| 25 |
+
let statusDiv = form.querySelector('.wallapi-live-status');
|
| 26 |
+
if (!statusDiv) {
|
| 27 |
+
statusDiv = document.createElement('div');
|
| 28 |
+
statusDiv.className = 'wallapi-live-status';
|
| 29 |
+
statusDiv.style.marginTop = '15px';
|
| 30 |
+
statusDiv.style.padding = '10px';
|
| 31 |
+
statusDiv.style.borderRadius = '6px';
|
| 32 |
+
statusDiv.style.display = 'none';
|
| 33 |
+
form.appendChild(statusDiv);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
form.addEventListener('submit', async (e) => {
|
| 37 |
+
e.preventDefault();
|
| 38 |
+
|
| 39 |
+
// Disable button so they don't click twice
|
| 40 |
+
if (submitBtn) submitBtn.disabled = true;
|
| 41 |
+
const originalBtnText = submitBtn ? (submitBtn.textContent || submitBtn.value) : '';
|
| 42 |
+
if (submitBtn) {
|
| 43 |
+
if (submitBtn.tagName === 'INPUT') submitBtn.value = 'Sending...';
|
| 44 |
+
else submitBtn.textContent = 'Sending...';
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Gather data from the form
|
| 48 |
+
// Based on your HTML: id="name", id="email", id="phone", id="subject"
|
| 49 |
+
// We use document.getElementById or form.querySelector to be safe.
|
| 50 |
+
|
| 51 |
+
const getName = () => form.querySelector('[name="name"]')?.value || '';
|
| 52 |
+
const getEmail = () => form.querySelector('[name="email"]')?.value || '';
|
| 53 |
+
const getPhone = () => form.querySelector('[name="phone"]')?.value || '';
|
| 54 |
+
const getSubject = () => form.querySelector('[name="subject"]')?.value || '';
|
| 55 |
+
|
| 56 |
+
// For the message, usually it's a textarea named 'message' or 'comments'
|
| 57 |
+
const getMessage = () => {
|
| 58 |
+
const msgEl = form.querySelector('textarea[name="message"], textarea[name="comments"]');
|
| 59 |
+
return msgEl ? msgEl.value : 'No message provided.';
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
const formData = {
|
| 63 |
+
name: getName(),
|
| 64 |
+
email: getEmail(),
|
| 65 |
+
phone: getPhone(),
|
| 66 |
+
subject: getSubject(),
|
| 67 |
+
message: getMessage(),
|
| 68 |
+
origin: 'Live Website: ' + window.location.hostname
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
try {
|
| 72 |
+
const response = await fetch(API_URL, {
|
| 73 |
+
method: 'POST',
|
| 74 |
+
headers: {
|
| 75 |
+
'Content-Type': 'application/json'
|
| 76 |
+
},
|
| 77 |
+
body: JSON.stringify(formData)
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
const data = await response.json();
|
| 81 |
+
|
| 82 |
+
statusDiv.style.display = 'block';
|
| 83 |
+
if (response.ok) {
|
| 84 |
+
statusDiv.textContent = 'Message sent successfully!';
|
| 85 |
+
statusDiv.style.backgroundColor = '#d1fae5';
|
| 86 |
+
statusDiv.style.color = '#065f46';
|
| 87 |
+
statusDiv.style.border = '1px solid #10b981';
|
| 88 |
+
// form.reset(); // Optional: reset form fields after success
|
| 89 |
+
} else {
|
| 90 |
+
statusDiv.textContent = data.error || 'Server error occurred.';
|
| 91 |
+
statusDiv.style.backgroundColor = '#fee2e2';
|
| 92 |
+
statusDiv.style.color = '#b91c1c';
|
| 93 |
+
statusDiv.style.border = '1px solid #ef4444';
|
| 94 |
+
}
|
| 95 |
+
} catch (err) {
|
| 96 |
+
console.error('[WallAPI Integration] Submit error:', err);
|
| 97 |
+
statusDiv.style.display = 'block';
|
| 98 |
+
statusDiv.textContent = 'Network error. Please try again later.';
|
| 99 |
+
statusDiv.style.backgroundColor = '#fee2e2';
|
| 100 |
+
statusDiv.style.color = '#b91c1c';
|
| 101 |
+
statusDiv.style.border = '1px solid #ef4444';
|
| 102 |
+
} finally {
|
| 103 |
+
// Re-enable button
|
| 104 |
+
if (submitBtn) {
|
| 105 |
+
submitBtn.disabled = false;
|
| 106 |
+
if (submitBtn.tagName === 'INPUT') submitBtn.value = originalBtnText;
|
| 107 |
+
else submitBtn.textContent = originalBtnText;
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
});
|
| 111 |
+
});
|
| 112 |
+
});
|
| 113 |
+
})();
|
server.js
CHANGED
|
@@ -59,7 +59,7 @@ const spreadsheetId = process.env.GOOGLE_SHEET_ID;
|
|
| 59 |
// API Endpoint to receive form submissions
|
| 60 |
app.post('/api/submit', async (req, res) => {
|
| 61 |
try {
|
| 62 |
-
const { name, email, message } = req.body;
|
| 63 |
|
| 64 |
if (!name || !email || !message) {
|
| 65 |
return res.status(400).json({ error: 'Name, email, and message are required fields.' });
|
|
@@ -73,13 +73,12 @@ app.post('/api/submit', async (req, res) => {
|
|
| 73 |
const auth = await getAuth();
|
| 74 |
const sheets = google.sheets({ version: 'v4', auth });
|
| 75 |
|
| 76 |
-
// Assuming the first sheet is where data goes
|
| 77 |
-
// We are going to append to Columns A to
|
| 78 |
-
// Wait, the user might not have set up headers, so we just append values.
|
| 79 |
const TIMESTAMP = new Date().toISOString();
|
| 80 |
|
| 81 |
// The range specifies where to append. A simple 'Sheet1' usually works,
|
| 82 |
-
// or just the spreadsheet name. Let's use 'Sheet1!A:
|
| 83 |
const range = 'Sheet1'; // Default sheet name in most English locales
|
| 84 |
|
| 85 |
const response = await sheets.spreadsheets.values.append({
|
|
@@ -87,7 +86,15 @@ app.post('/api/submit', async (req, res) => {
|
|
| 87 |
range,
|
| 88 |
valueInputOption: 'USER_ENTERED',
|
| 89 |
requestBody: {
|
| 90 |
-
values: [[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
},
|
| 92 |
});
|
| 93 |
|
|
@@ -98,7 +105,10 @@ app.post('/api/submit', async (req, res) => {
|
|
| 98 |
timestamp: TIMESTAMP,
|
| 99 |
name,
|
| 100 |
email,
|
| 101 |
-
message
|
|
|
|
|
|
|
|
|
|
| 102 |
});
|
| 103 |
|
| 104 |
return res.status(200).json({ success: true, message: 'Form submitted successfully!' });
|
|
|
|
| 59 |
// API Endpoint to receive form submissions
|
| 60 |
app.post('/api/submit', async (req, res) => {
|
| 61 |
try {
|
| 62 |
+
const { name, email, message, phone, subject, origin } = req.body;
|
| 63 |
|
| 64 |
if (!name || !email || !message) {
|
| 65 |
return res.status(400).json({ error: 'Name, email, and message are required fields.' });
|
|
|
|
| 73 |
const auth = await getAuth();
|
| 74 |
const sheets = google.sheets({ version: 'v4', auth });
|
| 75 |
|
| 76 |
+
// Assuming the first sheet is where data goes.
|
| 77 |
+
// We are going to append to Columns A to G (Timestamp, Name, Email, Message, Phone, Subject, Origin).
|
|
|
|
| 78 |
const TIMESTAMP = new Date().toISOString();
|
| 79 |
|
| 80 |
// The range specifies where to append. A simple 'Sheet1' usually works,
|
| 81 |
+
// or just the spreadsheet name. Let's use 'Sheet1!A:G' or just 'A:G'
|
| 82 |
const range = 'Sheet1'; // Default sheet name in most English locales
|
| 83 |
|
| 84 |
const response = await sheets.spreadsheets.values.append({
|
|
|
|
| 86 |
range,
|
| 87 |
valueInputOption: 'USER_ENTERED',
|
| 88 |
requestBody: {
|
| 89 |
+
values: [[
|
| 90 |
+
TIMESTAMP,
|
| 91 |
+
name,
|
| 92 |
+
email,
|
| 93 |
+
message,
|
| 94 |
+
phone || '',
|
| 95 |
+
subject || '',
|
| 96 |
+
origin || 'Hugging Face Widget'
|
| 97 |
+
]],
|
| 98 |
},
|
| 99 |
});
|
| 100 |
|
|
|
|
| 105 |
timestamp: TIMESTAMP,
|
| 106 |
name,
|
| 107 |
email,
|
| 108 |
+
message,
|
| 109 |
+
phone: phone || '',
|
| 110 |
+
subject: subject || '',
|
| 111 |
+
origin: origin || 'Hugging Face Widget'
|
| 112 |
});
|
| 113 |
|
| 114 |
return res.status(200).json({ success: true, message: 'Form submitted successfully!' });
|