StockAnalysis / static /js /onboarding.js
igardner's picture
fix: reset corrupt sqlite and reduce lobby load
626842d
Raw History Blame Contribute Delete
5.69 kB
/* Player-led first visit: intimate prompts, explicit skip/consent, no analysis. */
(function () {
const modal = document.getElementById('onboarding-overlay');
const form = document.getElementById('onboarding-form');
const prompt = document.getElementById('onboarding-question');
const answer = document.getElementById('onboarding-answer');
const stepLabel = document.getElementById('onboarding-step');
const nextButton = document.getElementById('onboarding-next');
const skipButton = document.getElementById('onboarding-skip');
const feedback = document.getElementById('onboarding-feedback');
const futureUse = document.getElementById('onboarding-future-use');
const forgetButton = document.getElementById('forget-onboarding');
const forgetStatus = document.getElementById('forget-onboarding-status');
if (!modal || !form) return;
let questions = [];
let answers = {};
let step = 0;
let saving = false;
async function request(url, options) {
const response = await fetch(url, Object.assign({ credentials: 'same-origin' }, options || {}));
const body = await response.json().catch(function () { return {}; });
if (!response.ok || body.ok === false) {
throw new Error(body.error || ('Request failed (' + response.status + ')'));
}
return body.data;
}
function renderStep() {
const question = questions[step];
if (!question) return;
prompt.textContent = question.prompt;
answer.value = answers[question.id] || '';
stepLabel.textContent = 'A question before you enter · ' + String(step + 1).padStart(2, '0') + ' / ' + String(questions.length).padStart(2, '0');
nextButton.textContent = step === questions.length - 1 ? 'Take my seat' : 'Continue';
document.querySelector('.onboarding-consent').hidden = step !== questions.length - 1;
feedback.textContent = '';
answer.focus();
}
async function finish(skip) {
if (saving) return;
saving = true;
nextButton.disabled = true;
skipButton.disabled = true;
feedback.textContent = skip ? 'Saving answers so far…' : 'Saving your answers…';
if (questions[step]) {
const currentAnswer = answer.value.trim();
if (currentAnswer) answers[questions[step].id] = currentAnswer;
else delete answers[questions[step].id];
}
try {
await request('/api/onboarding', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
skip: !!skip,
answers: answers,
future_use: !skip && futureUse.checked,
}),
});
window.ONBOARDING_DONE = true;
document.dispatchEvent(new CustomEvent('parlor:onboarding-complete'));
feedback.textContent = 'The parlor will remember only what you chose to leave behind.';
window.ParlorMotion.closeOnboarding(function () {
const participationAnswer = document.getElementById('participation-answer');
if (participationAnswer) participationAnswer.focus();
});
} catch (error) {
feedback.textContent = 'Could not save that just now: ' + error.message;
nextButton.disabled = false;
skipButton.disabled = false;
saving = false;
}
}
form.addEventListener('submit', function (event) {
event.preventDefault();
if (saving) return;
const question = questions[step];
if (!question) return;
answers[question.id] = answer.value.trim();
if (step < questions.length - 1) {
step += 1;
renderStep();
return;
}
finish(false);
});
skipButton.addEventListener('click', function () { finish(true); });
if (forgetButton) {
forgetButton.addEventListener('click', async function () {
forgetButton.disabled = true;
if (forgetStatus) forgetStatus.textContent = 'Removing your saved answers…';
try {
await request('/api/onboarding', { method: 'DELETE' });
forgetButton.hidden = true;
if (forgetStatus) forgetStatus.textContent = 'Your saved answers were removed.';
} catch (error) {
if (forgetStatus) forgetStatus.textContent = 'Could not remove answers: ' + error.message;
forgetButton.disabled = false;
}
});
}
modal.addEventListener('keydown', function (event) {
if (event.key === 'Escape') {
event.preventDefault();
finish(true);
}
if (event.key !== 'Tab') return;
const focusable = Array.from(modal.querySelectorAll('button:not([disabled]), textarea, input:not([disabled])'))
.filter(function (element) { return !element.closest('[hidden]'); });
if (!focusable.length) return;
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
});
request('/api/onboarding').then(function (state) {
if (state.completed) {
if (forgetButton) forgetButton.hidden = !Object.keys(state.answers || {}).length;
return;
}
questions = state.questions || [];
answers = state.answers || {};
if (!questions.length) return;
modal.hidden = false;
modal.setAttribute('aria-hidden', 'false');
document.body.classList.add('onboarding-active');
window.ParlorMotion.openOnboarding();
renderStep();
}).catch(function (error) {
const queueMessage = document.getElementById('queue-message');
if (queueMessage) queueMessage.textContent = 'The parlor could not open its first-visit questions. You can still join the queue.';
});
})();