|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +// Connects to data-controller="questions" |
| 4 | +export default class extends Controller { |
| 5 | + static targets = ["question", "choicesContainer", "destroyField"] |
| 6 | + |
| 7 | + connect() { |
| 8 | + this.questionIndex = this.questionTargets.length |
| 9 | + } |
| 10 | + |
| 11 | + addQuestion(event) { |
| 12 | + event.preventDefault() |
| 13 | + |
| 14 | + const container = document.getElementById("questions-container") |
| 15 | + const timestamp = new Date().getTime() |
| 16 | + |
| 17 | + const newQuestionHTML = ` |
| 18 | + <div class="question-fields border border-gray-300 p-4 rounded-md" data-questions-target="question"> |
| 19 | + <input type="hidden" name="billable[questions_attributes][${timestamp}][id]" value=""> |
| 20 | +
|
| 21 | + <div class="flex gap-4 items-start"> |
| 22 | + <div class="flex-1"> |
| 23 | + <label for="billable_questions_attributes_${timestamp}_question_text">Question</label> |
| 24 | + <textarea name="billable[questions_attributes][${timestamp}][question_text]" |
| 25 | + id="billable_questions_attributes_${timestamp}_question_text" |
| 26 | + rows="2" |
| 27 | + class="block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"></textarea> |
| 28 | + </div> |
| 29 | +
|
| 30 | + <div class="w-48"> |
| 31 | + <label for="billable_questions_attributes_${timestamp}_question_type">Type</label> |
| 32 | + <select name="billable[questions_attributes][${timestamp}][question_type]" |
| 33 | + id="billable_questions_attributes_${timestamp}_question_type" |
| 34 | + class="block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" |
| 35 | + data-action="change->questions#toggleChoices"> |
| 36 | + <option value="radio">Radio Buttons</option> |
| 37 | + <option value="textarea">Text Area</option> |
| 38 | + </select> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | +
|
| 42 | + <div class="choices-container mt-3" data-questions-target="choicesContainer"> |
| 43 | + <label for="billable_questions_attributes_${timestamp}_choices">Choices (one per line)</label> |
| 44 | + <textarea name="billable[questions_attributes][${timestamp}][choices]" |
| 45 | + id="billable_questions_attributes_${timestamp}_choices" |
| 46 | + rows="3" |
| 47 | + placeholder="Beef Chicken Fish Vegetarian" |
| 48 | + class="block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"></textarea> |
| 49 | + </div> |
| 50 | +
|
| 51 | + <input type="hidden" name="billable[questions_attributes][${timestamp}][order]" value="${this.questionIndex}"> |
| 52 | +
|
| 53 | + <div class="mt-3"> |
| 54 | + <a href="#" class="text-red-600 hover:text-red-800" data-action="click->questions#removeQuestion">Remove Question</a> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ` |
| 58 | + |
| 59 | + container.insertAdjacentHTML('beforeend', newQuestionHTML) |
| 60 | + this.questionIndex++ |
| 61 | + } |
| 62 | + |
| 63 | + removeQuestion(event) { |
| 64 | + event.preventDefault() |
| 65 | + const questionDiv = event.target.closest('[data-questions-target="question"]') |
| 66 | + const destroyField = questionDiv.querySelector('[data-questions-target="destroyField"]') |
| 67 | + |
| 68 | + if (destroyField) { |
| 69 | + // Question already exists in database, mark for destruction |
| 70 | + destroyField.value = "1" |
| 71 | + questionDiv.style.display = "none" |
| 72 | + } else { |
| 73 | + // New question, just remove from DOM |
| 74 | + questionDiv.remove() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + toggleChoices(event) { |
| 79 | + const select = event.target |
| 80 | + const questionDiv = select.closest('[data-questions-target="question"]') |
| 81 | + const choicesContainer = questionDiv.querySelector('[data-questions-target="choicesContainer"]') |
| 82 | + |
| 83 | + if (select.value === 'textarea') { |
| 84 | + choicesContainer.style.display = 'none' |
| 85 | + } else { |
| 86 | + choicesContainer.style.display = 'block' |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments