Browse Source

Make sure we only run the stimulus init once per page load

pull/77/head
baarkerlounger 4 years ago
parent
commit
5f2c1e4c13
  1. 53
      app/javascript/controllers/soft_validations_controller.js
  2. 2
      app/views/form/_validation_override_question.html.erb
  3. 1
      spec/features/case_log_spec.rb

53
app/javascript/controllers/soft_validations_controller.js

@ -1,30 +1,35 @@
import { Controller } from "@hotwired/stimulus" import { Controller } from "@hotwired/stimulus"
export default class extends Controller { export default class extends Controller {
static values = { run: Number }
initialize() { initialize() {
let url = window.location.href + "/soft_validations" if(this.runValue == 0){
let xhr = new XMLHttpRequest() let url = window.location.href + "/soft_validations"
let div = document.getElementById("soft-validations") let xhr = new XMLHttpRequest()
xhr.open("GET", url, true) let div = document.getElementById("soft-validations")
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded") xhr.open("GET", url, true)
xhr.onreadystatechange = function () { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
if (this.readyState == 4 && this.status == 200) { xhr.onreadystatechange = function () {
let response = JSON.parse(this.response) if (this.readyState == 4 && this.status == 200) {
if(response["show"]){ let response = JSON.parse(this.response)
div.style.display = "block" if(response["show"]){
let innerHTML = div.innerHTML div.style.display = "block"
innerHTML = innerHTML.replace("soft-validations-placeholder-message", response["label"]) let innerHTML = div.innerHTML
innerHTML = innerHTML.replace("soft-validations-placeholder-hint-text", response["hint"]) innerHTML = innerHTML.replace("soft-validations-placeholder-message", response["label"])
div.innerHTML = innerHTML innerHTML = innerHTML.replace("soft-validations-placeholder-hint-text", response["hint"])
} else { div.innerHTML = innerHTML
div.style.display = "none" } else {
let buttons = document.getElementsByName(`case_log[override_net_income_validation][]`) div.style.display = "none"
Object.entries(buttons).forEach(([idx, button]) => { let buttons = document.getElementsByName(`case_log[override_net_income_validation][]`)
button.checked = false; Object.entries(buttons).forEach(([idx, button]) => {
}) button.checked = false;
} })
} }
} }
xhr.send() }
xhr.send()
this.runValue++
}
} }
} }

2
app/views/form/_validation_override_question.html.erb

@ -2,7 +2,7 @@
<%= f.govuk_check_boxes_fieldset page_info["soft_validations"]&.keys&.first, <%= f.govuk_check_boxes_fieldset page_info["soft_validations"]&.keys&.first,
legend: { text: "soft-validations-placeholder-message", size: "l" }, legend: { text: "soft-validations-placeholder-message", size: "l" },
hint: { text: "soft-validations-placeholder-hint-text" }, hint: { text: "soft-validations-placeholder-hint-text" },
form_group: { "data-controller": "soft-validations" } do %> form_group: { "data-controller": "soft-validations", "data-run": "0" } do %>
<%= f.govuk_check_box page_info["soft_validations"]&.keys&.first, page_info["soft_validations"]&.keys&.first, <%= f.govuk_check_box page_info["soft_validations"]&.keys&.first, page_info["soft_validations"]&.keys&.first,
label: { text: "Yes" } label: { text: "Yes" }

1
spec/features/case_log_spec.rb

@ -405,7 +405,6 @@ RSpec.describe "Test Features" do
choose("case-log-net-income-frequency-weekly-field", allow_label_click: true) choose("case-log-net-income-frequency-weekly-field", allow_label_click: true)
click_button("Save and continue") click_button("Save and continue")
expect(page).to have_content("Are you sure this is correct?") expect(page).to have_content("Are you sure this is correct?")
binding.pry
check("case-log-override-net-income-validation-override-net-income-validation-field", allow_label_click: true) check("case-log-override-net-income-validation-override-net-income-validation-field", allow_label_click: true)
click_button("Save and continue") click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{case_log.id}/net_income_uc_proportion") expect(page).to have_current_path("/case_logs/#{case_log.id}/net_income_uc_proportion")

Loading…
Cancel
Save