Skip to content

Commit

Permalink
make edit view conditionally display the second nationality dropdown …
Browse files Browse the repository at this point in the history
…depending on checkbox state
  • Loading branch information
RandomTannenbaum committed Feb 27, 2024
1 parent dbaeacf commit dab4f5a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def new

def update
@person.roles.build if @person.roles.empty?
if params['has_nationality2']['{}'].to_i.zero?
params['person']['nationality2'] = nil
end
super
end

Expand Down
7 changes: 5 additions & 2 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

import { application } from "./application"

import Remote_modal_controller from "./remote_modal_controller"
application.register("remote-modal", Remote_modal_controller)
import NationalityTwoController from "./nationality_two_controller"
application.register("nationality-two", NationalityTwoController)

import RemoteModalController from "./remote_modal_controller"
application.register("remote-modal", RemoteModalController)
18 changes: 18 additions & 0 deletions app/javascript/controllers/nationality_two_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
setVisibility() {
const natTwoElements = document.getElementsByClassName('nationality-two')
const natTwoCheckbox = document.getElementById('nat-two-checkbox')
for(const element of natTwoElements) {
!natTwoCheckbox.checked ? element.classList.add('d-none') : element.classList.remove('d-none')
}
}

connect() {
this.setVisibility();
}
nationalityTwoVisible() {
this.setVisibility()
}
}
9 changes: 4 additions & 5 deletions app/views/people/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@
%td= form.date_field :birthdate, class: "form-control w-100"
%th.fw-normal Doppelbürger
%tr
%td= check_box :has_nationality2, {}, checked: @person.nationality2?
%td{"data-controller"=>"nationality-two"}= check_box :has_nationality2, {}, {"checked" => @person.nationality2?, "data-action" => "nationality-two#nationalityTwoVisible", "id" => "nat-two-checkbox"}
%th.fw-normal Erste Nationalität
%tr
%td= form.collection_select :nationality, ISO3166::Country.all.sort, :alpha2, :name, {}, class: "form-select w-100"
- if @person.nationality2?
%th.fw-normal Zweite Nationalität
%tr
%td= form.collection_select :nationality2, ISO3166::Country.all.sort, :alpha2, :name, {}, class: "form-select w-100"
%th.fw-normal.nationality-two Zweite Nationalität
%tr.nationality-two
%td= form.collection_select :nationality2, ISO3166::Country.all.sort, :alpha2, :name, {}, class: "form-select w-100"
%th.fw-normal Zivilstand
%tr
%td= form.collection_select :marital_status, Person.marital_statuses, :first, :first, { selected: @person.marital_status }, class: "form-select w-100"
Expand Down

0 comments on commit dab4f5a

Please sign in to comment.