Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[275-FIX] Fix for max input values for scale types #288

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/javascript/controllers/evaluation_criteria_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ export default class extends Controller {
});
}

checkPointsOrWeightMax(event) {
const input = event.target;
const min = input.min;
const max = input.max;
const value = input.value;

// If field has a value on blur then check if it's valid
if ((value && value < min) || value > max) {
stepchud marked this conversation as resolved.
Show resolved Hide resolved
event.target.reportValidity();
}
}

updateScoringOptions(row, scoringType) {
const options = {
scaleOptions: row.querySelector(".criteria-scale-options"),
Expand Down
95 changes: 70 additions & 25 deletions app/javascript/controllers/evaluation_form_controller.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,99 @@
import { Controller } from "@hotwired/stimulus"
import { Controller } from "@hotwired/stimulus";

// Connects to data-controller="evaluation-form"
export default class extends Controller {
static targets = ["challengeID", "phaseID", "startDate", "datePicker"];

handleChallengeSelect(e) {
let id, phase_id, end_date
[id, phase_id, end_date] = e.target.value.split(".")
let id, phase_id, end_date;
[id, phase_id, end_date] = e.target.value.split(".");
if (id) {
// set values of hidden form fields
this.challengeIDTarget.value = id
this.phaseIDTarget.value = phase_id
// set values of hidden form fields
this.challengeIDTarget.value = id;
this.phaseIDTarget.value = phase_id;

// set the start date of the evaluation form
// set the start date of the evaluation form
// to be the challenge's end date
this.startDateTarget.innerHTML = end_date || "mm/dd/yyyy"
let day, month, year
[month, day, year] = end_date.split("/")
this.datePickerTarget.setAttribute("data-min-date", `${year}-${month}-${day}`)
this.startDateTarget.innerHTML = end_date || "mm/dd/yyyy";
let day, month, year;
[month, day, year] = end_date.split("/");
this.datePickerTarget.setAttribute(
"data-min-date",
`${year}-${month}-${day}`
);

this.updateErrorMessage("evaluation_form_challenge_id", "")
this.updateErrorMessage("evaluation_form_phase_id", "")
this.updateErrorMessage("evaluation_form_challenge_id", "");
this.updateErrorMessage("evaluation_form_phase_id", "");
} else {
this.updateErrorMessage("evaluation_form_challenge_id", "can't be blank")
this.startDateTarget.innerHTML = "mm/dd/yyyy"
this.updateErrorMessage("evaluation_form_challenge_id", "can't be blank");
this.startDateTarget.innerHTML = "mm/dd/yyyy";
}
}

// Opens all accordions, remove existing points/weights, update max points/weights values
updateMaxPoints(e) {
const form = e.target.closest('form[data-controller="evaluation-form"]');
const pointsWeights = form.querySelectorAll(".points-or-weight");
if (e.target.id == 'weighted_scale') {
pointsWeights.forEach((input) => input.max = "100")
} else {
pointsWeights.forEach((input) => input.max = "9999")
const weightedScale = e.target.value === "true";

if (weightedScale && this.hasValuesOverLimit(pointsWeights, 100)) {
if (!this.confirmReset()) {
e.preventDefault();
return;
}
this.clearInputs(pointsWeights);
this.expandAllAccordions(form);
}

this.updateMaxValues(pointsWeights, weightedScale ? 100 : 9999);
}

// Helper: Check if any input values exceed a given limit
hasValuesOverLimit(inputs, limit) {
return Array.from(inputs).some(
(input) => parseInt(input.value.trim()) > limit
);
}

// Helper: Show confirmation dialog for resetting values
confirmReset() {
return window.confirm(
"You have values over 100. Changing the scale type to weighted will reset your weight values."
);
}

// Helper: Clear all input values
clearInputs(inputs) {
inputs.forEach((input) => (input.value = ""));
}

// Helper: Update max values for inputs
updateMaxValues(inputs, maxValue) {
inputs.forEach((input) => (input.max = maxValue));
}

// Helper: Expand all accordions
expandAllAccordions(form) {
const accordionButtons = form.querySelectorAll(".usa-accordion__button");
const accordions = form.querySelectorAll(".usa-accordion__content");

accordionButtons.forEach((button) =>
button.setAttribute("aria-expanded", true)
);
accordions.forEach((content) => content.removeAttribute("hidden"));
}

validatePresence(e) {
if (!e.target.value) {
e.target.classList.add("border-secondary")
this.updateErrorMessage(e.target.id, "can't be blank")

e.target.classList.add("border-secondary");
this.updateErrorMessage(e.target.id, "can't be blank");
} else {
e.target.classList.remove("border-secondary")
this.updateErrorMessage(e.target.id, "")
e.target.classList.remove("border-secondary");
this.updateErrorMessage(e.target.id, "");
}
}

updateErrorMessage(field, message) {
document.getElementById(field + "_error").innerHTML = message
document.getElementById(field + "_error").innerHTML = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
placeholder: "Add criteria points/weight here",
required: true,
disabled: is_template || form_disabled,
data: {"evaluation-criteria-target": "pointsOrWeightField"}
data: {
"evaluation-criteria-target": "pointsOrWeightField",
action: "blur->evaluation-criteria#checkPointsOrWeightMax"
}
%>

<div class="flex-7 font-sans-lg">
Expand Down
4 changes: 2 additions & 2 deletions app/views/evaluation_forms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
type="radio"
name="evaluation_form[weighted_scoring]"
value="false"
data-action="input->evaluation-form#updateMaxPoints"
data-action="click->evaluation-form#updateMaxPoints"
<%= 'checked' if !evaluation_form.weighted_scoring? && evaluation_form.persisted? %>
<%= 'disabled' if disabled %>
required
Expand All @@ -112,7 +112,7 @@
type="radio"
name="evaluation_form[weighted_scoring]"
value="true"
data-action="input->evaluation-form#updateMaxPoints"
data-action="click->evaluation-form#updateMaxPoints"
<%= 'checked' if evaluation_form.weighted_scoring? %>
<%= 'disabled' if disabled %>
>
Expand Down
58 changes: 58 additions & 0 deletions spec/system/evaluation_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,64 @@
save_form
expect(page).to have_content("Evaluation Form Saved")
end

it "expands all criteria and resets values if switching to weighted scale with value over 100" do
visit new_evaluation_form_path

fill_in_base_form_info
select_scale_type("point")

# Fill in criteria with one being over 100
fill_in_numeric_criteria_type(initial: true)
fill_in_criterion_points_weight(0, 10)
fill_in_numeric_criteria_type
fill_in_criterion_points_weight(1, 101)

toggle_all_criteria_accordions(open: false)

check_criteria_accordion_expanded(0, false)
check_criteria_accordion_expanded(1, false)

select_scale_type("weighted")
accept_confirm

check_criteria_accordion_expanded(0, true)
check_criteria_accordion_expanded(1, true)

# Scale type should be weighted
expect_form_scale_type_to_equal(true)
expect_criterion_points_or_weight_to_equal(0, 0)
expect_criterion_points_or_weight_to_equal(1, 0)
end

it "does nothing and prevents switching scale if switching to weighted scale with value over 100 and not accepting" do
visit new_evaluation_form_path

fill_in_base_form_info
select_scale_type("point")

# Fill in criteria with one being over 100
fill_in_numeric_criteria_type(initial: true)
fill_in_criterion_points_weight(0, 10)
fill_in_numeric_criteria_type
fill_in_criterion_points_weight(1, 101)

toggle_all_criteria_accordions(open: false)

check_criteria_accordion_expanded(0, false)
check_criteria_accordion_expanded(1, false)

select_scale_type("weighted")
dismiss_confirm

check_criteria_accordion_expanded(0, false)
check_criteria_accordion_expanded(1, false)

# Scale type should be pointed still
expect_form_scale_type_to_equal(false)
expect_criterion_points_or_weight_to_equal(0, 10)
expect_criterion_points_or_weight_to_equal(1, 101)
end
end

describe "update evaluation form page" do
Expand Down