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

fix: case court report modal ui changes #6185

Merged
23 changes: 22 additions & 1 deletion app/assets/stylesheets/pages/casa_cases.scss
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,25 @@ body.casa_cases-show {
flex-direction: column;
gap: .2rem;
}
}
}

#case-selection:required, .select2.select2-container {
background: transparent;
border: 1px solid #e5e5e5;
border-radius: 10px;
padding: 8px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}

.select-wrapper {
display: flex;
flex-direction: column;
gap: 8px;
}

span[role=combobox] {
padding-top: 2px;
}

39 changes: 38 additions & 1 deletion app/javascript/src/casa_case.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,25 @@ function showAlert (html) {
flashContainer && flashContainer.replaceWith(alertEl)
}

function validateForm (formEl, errorEl) {
if (!formEl) {
return
}

// check html validations, checkValidity returns false if doesn't pass validation
if (errorEl && !formEl.checkValidity()) {
errorEl.classList.remove('d-none')
}
}

function handleGenerateReport (e) {
e.preventDefault()

const formData = Object.fromEntries(new FormData(e.currentTarget.form))
const form = e.currentTarget.form

const formData = Object.fromEntries(new FormData(form))
const errorEl = document.querySelector('.select-required-error')
validateForm(form, errorEl ?? null)
if (formData.case_number.length === 0) return

const generateBtn = e.currentTarget
Expand Down Expand Up @@ -127,20 +141,43 @@ function handleGenerateReport (e) {
})
}

function clearSelectErrors () {
const errorEl = document.querySelector('.select-required-error')

if (!errorEl) return

errorEl.classList.add('d-none')
}

function handleModalClose () {
const selectEl = document.querySelector('#case-selection')

if (!selectEl) return

clearSelectErrors()
// this line taken from docs https://select2.org/programmatic-control/add-select-clear-items
$('#case-selection').val(null).trigger('change')
}

$(() => { // JQuery's callback for the DOM loading
$('button.copy-court-button').on('click', copyOrdersFromCaseWithConfirmation)

if ($('button.copy-court-button').length) {
disableBtn($('button.copy-court-button')[0])
}

$('#case-selection').on('change', clearSelectErrors)

$('select.siblings-casa-cases').on('change', () => {
if ($('select.siblings-casa-cases').find(':selected').text()) {
enableBtn($('button.copy-court-button')[0])
} else {
disableBtn($('button.copy-court-button')[0])
}
})
// modal id is defined in _generate_docx.html.erb so would like to be able to implement modal close logic in that file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good comment. I am not sure of the right solution here.

// but not sure how to
$('#generate-docx-report-modal').on('hidden.bs.modal', () => handleModalClose())

$('#btnGenerateReport').on('click', handleGenerateReport)

Expand Down
19 changes: 11 additions & 8 deletions app/views/case_court_reports/_generate_docx.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@

<% select_case_prompt = show_search ? "Search by volunteer name or case number" : "Select case number" %>
<% select2_class = show_search ? " select2" : "" %>
<%= select_tag :case_number,
options_for_select(select_options),
prompt: select_case_prompt,
include_blank: false,
id: "case-selection",
class: "custom-select#{select2_class}",
data: { dropdown_parent: "##{id}" } %>

<div class="select-wrapper">
<%= select_tag :case_number,
options_for_select(select_options),
prompt: select_case_prompt,
include_blank: false,
id: "case-selection",
class: "custom-select#{select2_class}",
required: true,
data: { dropdown_parent: "##{id}", width: "100%" } %>
<p class="select-required-error text-danger d-none">Case selection is required.</p>
</div>
<%= form.hidden_field :time_zone, id: "user-time-zone" %>
</div>
<div class="dates-container">
Expand Down
20 changes: 20 additions & 0 deletions spec/system/case_court_reports/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@
expect(page).to have_selector("#btnGenerateReport .lni-download", visible: true)
expect(page).not_to have_selector("#btnGenerateReport[disabled]")
expect(page).to have_selector("#spinner", visible: :hidden)

# when 'Generate Report' button is clicked without a selection, should display an error saying to make a selection
expect(page).to have_selector(".select-required-error", visible: :visible)

test_case_number = casa_cases.find(&:in_transition_age?).case_number.to_s

# when we make a selection, the error is no longer visible
page.select test_case_number, from: "case-selection"
expect(page).not_to have_selector(".select-required-error", visible: :visible)

# test the flow for clearing case selection error message by closing modal
click_button "Close"
page.find(modal_selector).click
click_button "Generate Report"
# expect the error message to be visible because we tried to generate the doc without making a selection
expect(page).to have_selector(".select-required-error", visible: :visible)
# expect error message to be gone after we close and re-open the modal
click_button "Close"
page.find(modal_selector).click
expect(page).not_to have_selector(".select-required-error", visible: :visible)
end
end

Expand Down
Loading