iswhoa/public/scripts/controllers/picker.js

66 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

import { Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
2025-06-20 12:00:23 +00:00
import { gameState } from "./gamestate.js";
export default class extends Controller {
static targets = ["radio", "submitButton", "answerDetails", "submit", "result"];
static values = {
2025-06-20 12:00:23 +00:00
qid: String,
answer: String
};
connect() {
2025-06-20 12:00:23 +00:00
let hasAnswer = gameState.getQuestionChoice(this.qidValue);
if (hasAnswer) {
this._revealAnswer(hasAnswer.isRight);
2025-06-20 12:00:23 +00:00
2025-06-22 12:06:49 +00:00
let e = this.radioTargets.find((e) => e.value == hasAnswer.cId);
2025-06-20 12:00:23 +00:00
if (e) {
e.checked = true;
}
}
}
checkRadio(ev) {
this.submitTarget.disabled = false;
}
submitAnswer(ev) {
ev.preventDefault();
2025-06-20 12:00:23 +00:00
this.submitTarget.disabled = false;
let e = this.radioTargets.find((e) => e.checked);
if (!e) {
alert("Please select an item");
return;
}
let wasRight = e.value === this.answerValue;
gameState.setQuestionChoice(this.qidValue, e.value, wasRight);
2025-06-20 12:00:23 +00:00
this.element.classList.add("reveal");
window.setTimeout(() => { this._revealAnswer(wasRight); });
2025-06-20 12:00:23 +00:00
}
_revealAnswer(wasRight) {
this.element.classList.add("reveal");
2025-06-20 12:00:23 +00:00
this.radioTargets.forEach(e => {
e.disabled = true;
if (e.value === this.answerValue) {
e.classList.add("answer");
2025-06-20 12:00:23 +00:00
} else {
e.classList.add("wrong");
}
});
2025-06-19 12:14:46 +00:00
if (wasRight) {
this.resultTarget.innerText = "Correct";
} else {
this.resultTarget.innerText = "Sorry, that's incorrect";
}
this.submitButtonTarget.classList.add("hidden");
2025-06-19 12:14:46 +00:00
this.answerDetailsTarget.classList.remove("hidden");
}
};