Skip to content

Commit 45827bf

Browse files
committed
updates to download validation for format and multiple phases
1 parent 30290ef commit 45827bf

1 file changed

Lines changed: 114 additions & 6 deletions

File tree

app/javascript/src/plans/download.js

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,121 @@ $(() => {
2323
}
2424

2525
if (frmt === 'csv') {
26-
$('#phase_id').find('option[value="All"').hide();
27-
$('#phase_id option:eq(1)').attr('selected', 'selected');
28-
$('#phase_id').val($('#phase_id option:eq(1)').val());
26+
if ($('#phase_id option').length > 1) {
27+
$('#phase_id').find('option[value="All"').hide();
28+
$('#phase_id').val($('#phase_id option:eq(1)').val());
29+
$('#phase_id option:eq(1)').attr('selected', 'selected');
30+
}
2931
} else if (frmt === 'pdf' || frmt === 'html' || frmt === 'docx' || frmt === 'text') {
30-
$('#phase_id').find('option[value="All"').show();
31-
$('#phase_id').val($('#phase_id option:first').val());
32-
$('#phase_id option:first').attr('selected', 'selected');
32+
if ($('#phase_id option').length > 1) {
33+
$('#phase_id').find('option[value="All"').show();
34+
$('#phase_id').val($('#phase_id option:first').val());
35+
$('#phase_id option:first').attr('selected', 'selected');
36+
}
3337
}
3438
}).trigger('change');
39+
40+
// --- Start of format and phase validation code ---
41+
const downloadForm = document.getElementById('download_form');
42+
const formatSelect = document.getElementById('download_format_select');
43+
const phaseSelect = document.getElementById('phase_id'); // This might be null if phase_options.length <= 1
44+
const flashNoticeDiv = document.getElementById('client-side-flash-notice');
45+
const downloadButton = downloadForm.querySelector('button[type="submit"]');
46+
47+
if (downloadForm && formatSelect && flashNoticeDiv && downloadButton) {
48+
downloadForm.addEventListener('submit', function(event) {
49+
let errorMessage = "";
50+
let isFormatValid = true;
51+
let isPhaseValid = true; // Assume valid initially, or if phaseSelect doesn't exist
52+
53+
const selectedFormat = formatSelect.value;
54+
const selectedPhase = phaseSelect ? phaseSelect.value : "N/A"; // Use "N/A" or similar if no phase select, to distinguish from actual ""
55+
56+
// Check if Format is selected
57+
if (selectedFormat === "") {
58+
isFormatValid = false;
59+
}
60+
61+
// Check if Phase is selected (only if phaseSelect exists)
62+
if (phaseSelect && selectedPhase === "") {
63+
isPhaseValid = false;
64+
}
65+
66+
// Construct the error message based on validity flags
67+
if (!isFormatValid && !isPhaseValid) {
68+
errorMessage = "Please select a format and a phase before downloading the plan.";
69+
} else if (!isFormatValid) {
70+
errorMessage = "Please select a format before downloading the plan.";
71+
} else if (!isPhaseValid) { // This implies phaseSelect exists and format is valid
72+
errorMessage = "Please select a phase to download.";
73+
}
74+
75+
// Apply/Remove is-invalid class based on individual validity
76+
if (isFormatValid) {
77+
formatSelect.classList.remove('is-invalid');
78+
} else {
79+
formatSelect.classList.add('is-invalid');
80+
}
81+
82+
if (phaseSelect) { // Only apply if phaseSelect actually exists on the page
83+
if (isPhaseValid) {
84+
phaseSelect.classList.remove('is-invalid');
85+
} else {
86+
phaseSelect.classList.add('is-invalid');
87+
}
88+
}
89+
90+
if (errorMessage !== "") {
91+
// Prevent the form from submitting if there's any error
92+
event.preventDefault();
93+
94+
// Display the flash notice
95+
flashNoticeDiv.textContent = errorMessage;
96+
flashNoticeDiv.style.display = 'block'; // Make it visible
97+
98+
// Optional: Scroll to the top to make sure the message is visible
99+
window.scrollTo({ top: 0, behavior: 'smooth' });
100+
101+
} else {
102+
// All valid, allow the form to submit
103+
// Hide any previously shown validation message
104+
flashNoticeDiv.style.display = 'none';
105+
}
106+
});
107+
108+
// Optional: Clear validation message when user actually selects an option
109+
formatSelect.addEventListener('change', function() {
110+
// Re-evaluate validity based on current selections
111+
const currentFormatValid = (formatSelect.value !== "");
112+
const currentPhaseValid = (phaseSelect ? (phaseSelect.value !== "") : true); // If no phaseSelect, it's always valid
113+
114+
// Hide main flash notice if both are now valid
115+
if (currentFormatValid && currentPhaseValid) {
116+
flashNoticeDiv.style.display = 'none';
117+
}
118+
// Always remove the specific field's invalid class if it's now valid
119+
if (currentFormatValid) {
120+
formatSelect.classList.remove('is-invalid');
121+
}
122+
});
123+
124+
// Optional: Clear phase validation message when user selects a phase
125+
if (phaseSelect) { // Only add this if phaseSelect exists
126+
phaseSelect.addEventListener('change', function() {
127+
// Re-evaluate validity based on current selections
128+
const currentFormatValid = (formatSelect.value !== "");
129+
const currentPhaseValid = (phaseSelect.value !== "");
130+
131+
// Hide main flash notice if both are now valid
132+
if (currentFormatValid && currentPhaseValid) {
133+
flashNoticeDiv.style.display = 'none';
134+
}
135+
// Always remove the specific field's invalid class if it's now valid
136+
if (currentPhaseValid) {
137+
phaseSelect.classList.remove('is-invalid');
138+
}
139+
});
140+
}
141+
}
142+
// --- End of format and phase validation code ---
35143
});

0 commit comments

Comments
 (0)