Massage Spa & Salons

Provider Dashboard

Please log in to view your dashboard

Go to Login

function showMsg(type, text) {
const el = document.getElementById(‘dashboard-msg’);
el.textContent = text;
el.className = ‘msg ‘ + type;
window.scrollTo(0, 0);
}

async function checkAuthAndLoad() {
const token = localStorage.getItem(‘mss_token’);
if (!token) {
document.getElementById(‘unauth-msg’).style.display = ‘block’;
return;
}

document.getElementById(‘unauth-msg’).style.display = ‘none’;
document.getElementById(‘dashboard-view’).style.display = ‘block’;

// Fetch existing profile for this user
try {
const res = await fetch(`${WP_URL}/wp-json/wp/v2/profiles?author=me`, {
headers: { ‘Authorization’: ‘Bearer ‘ + token }
});

if (res.ok) {
const profiles = await res.json();
if (profiles.length > 0) {
const p = profiles[0]; // Load the first profile
existingProfileId = p.id;

document.getElementById(‘prof-title’).value = p.title.rendered || ”;

// Basic HTML stripping from WP content
let content = p.content.rendered || ”;
content = content.replace(/]+>/g, ”).trim();
document.getElementById(‘prof-desc’).value = content;

if (p.meta) {
document.getElementById(‘prof-bizname’).value = p.meta.business_name || ”;
document.getElementById(‘prof-address’).value = p.meta.address || ”;
document.getElementById(‘prof-phone’).value = p.meta.contact_phone || ”;
document.getElementById(‘prof-services’).value = p.meta.services || ”;
}
}
}
} catch (e) {
console.error(“Could not load profile”, e);
}
}

async function saveProfile(e) {
e.preventDefault();
const token = localStorage.getItem(‘mss_token’);
if (!token) return;

const btn = document.getElementById(‘btn-save’);
btn.disabled = true;
btn.textContent = ‘Saving…’;

const payload = {
title: document.getElementById(‘prof-title’).value,
content: document.getElementById(‘prof-desc’).value,
status: ‘publish’,
meta: {
business_name: document.getElementById(‘prof-bizname’).value,
address: document.getElementById(‘prof-address’).value,
contact_phone: document.getElementById(‘prof-phone’).value,
services: document.getElementById(‘prof-services’).value
}
};

let url = `${WP_URL}/wp-json/wp/v2/profiles`;
let method = ‘POST’;

// If they already have a profile, update it instead of creating a new one
if (existingProfileId) {
url += ‘/’ + existingProfileId;
}

try {
const res = await fetch(url, {
method: method,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Bearer ‘ + token
},
body: JSON.stringify(payload)
});

const data = await res.json();

if (res.ok) {
showMsg(‘success’, ‘Profile saved successfully! It will appear on the site shortly.’);
if (!existingProfileId) existingProfileId = data.id;
} else {
showMsg(‘error’, data.message || ‘Failed to save profile.’);
}
} catch (err) {
showMsg(‘error’, ‘Network error. Please try again.’);
} finally {
btn.disabled = false;
btn.textContent = ‘Save Profile’;
}
}

function handleLogout() {
localStorage.removeItem(‘mss_token’);
localStorage.removeItem(‘mss_user’);
window.location.href = ‘/login/’;
}

document.addEventListener(‘DOMContentLoaded’, checkAuthAndLoad);