// ─────────────────────────────────────────────────────────────
// Jones & Company — Home Valuation Page
// Wix Velo Page Code (paste into the Page Code panel)
//
// Prerequisites in Wix dashboard before using this code:
// 1. Install "Wix CRM" from the App Market (free) — gives you
// wix-crm-backend for creating contacts & activities.
// 2. In Wix Editor: add an HTML Component element, give it the
// ID #valuationEmbed and paste velo-html-component.html
// into its "Enter Code" panel.
// 3. In Site Settings → Business Info, confirm your business
// email is set — that is where the notification will go.
// ─────────────────────────────────────────────────────────────
import { contacts } from 'wix-crm-backend'; // Wix CRM contact creation
import { triggeredEmails } from 'wix-crm-backend'; // Triggered email (optional)
import wixWindow from 'wix-window';
// ── Listen for postMessage from the HTML Component ──────────
$w.onReady(function () {
$w('#valuationEmbed').onMessage(async (event) => {
const msg = event.data;
// Only handle our form submission message
if (!msg || msg.type !== 'VALUATION_SUBMIT') return;
const d = msg.data; // shorthand for form data object
try {
// ── 1. CREATE / UPDATE CONTACT IN WIX INBOX / CRM ─────
const nameParts = d.name.trim().split(' ');
const firstName = nameParts[0] || '';
const lastName = nameParts.slice(1).join(' ') || '';
const contactInfo = {
name: {
first: firstName,
last: lastName,
},
emails: [{ tag: 'main', email: d.email }],
phones: d.phone ? [{ tag: 'mobile', phone: d.phone }] : [],
addresses: [{
tag: 'home',
street: d.address,
city: d.city,
postalCode: d.zip,
country: 'US',
}],
// Store property details as custom fields
// (add these custom fields in your Wix CRM settings first)
customFields: {
'custom.bedrooms': { value: d.beds || 'Not specified' },
'custom.bathrooms': { value: d.baths || 'Not specified' },
'custom.sqft': { value: d.sqft || 'Not specified' },
'custom.condition': { value: d.condition || 'Not specified' },
'custom.source': { value: 'Home Valuation Form' },
},
};
const { contactId } = await contacts.createContact(contactInfo);
// ── 2. LOG AN ACTIVITY ON THE CONTACT ──────────────────
await contacts.createActivity(contactId, {
activityType: 'FORM_SUBMITTED',
info: {
activityDetails: {
summary: `Home valuation request submitted for: ${d.address}, ${d.city} ${d.zip}`,
additionalInfoUrl: '',
},
fields: [
{ name: 'Property Address', value: `${d.address}, ${d.city}, ${d.zip}` },
{ name: 'Bedrooms', value: d.beds || '—' },
{ name: 'Bathrooms', value: d.baths || '—' },
{ name: 'Square Footage', value: d.sqft || '—' },
{ name: 'Condition', value: d.condition || '—' },
],
},
});
// ── 3. SEND EMAIL NOTIFICATION TO YOUR TEAM ─────────────
// Option A — Wix Triggered Email (recommended):
// • Go to Ascend → Email Marketing → Triggered Emails
// • Create a new triggered email with the variables below
// • Copy its emailId and paste it in place of 'valuationRequest'
//
// await triggeredEmails.emailContact('valuationRequest', contactId, {
// variables: {
// clientName: d.name,
// clientEmail: d.email,
// clientPhone: d.phone || 'Not provided',
// propertyAddr: `${d.address}, ${d.city} ${d.zip}`,
// beds: d.beds || '—',
// baths: d.baths || '—',
// sqft: d.sqft || '—',
// condition: d.condition || '—',
// },
// });
// Option B — Simple site notification via wix-inbox
// (uncomment if you prefer Wix Inbox notifications instead)
//
// import { inbox } from 'wix-inbox-backend';
// await inbox.sendMessage({
// channelId: contactId,
// message: {
// type: 'basic',
// payload: {
// text: `New valuation request from ${d.name} for ${d.address}, ${d.city} ${d.zip}`,
// },
// },
// });
console.log('✅ Valuation form processed. Contact ID:', contactId);
} catch (err) {
console.error('❌ Valuation form error:', err);
// Optionally post an error message back to the iframe:
// $w('#valuationEmbed').postMessage({ type: 'SUBMIT_ERROR' });
}
});
});
top of page
bottom of page