diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 88e50718ba1..558c0467a46 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -128,7 +128,7 @@ def update_identite @dossier = dossier @no_description = true - if @dossier.individual.update(individual_params) + if @dossier.update(dossier_params) @dossier.update!(autorisation_donnees: true, identity_updated_at: Time.zone.now) flash.notice = t('.identity_saved') @@ -578,8 +578,8 @@ def render_siret_error(error_message) render :siret end - def individual_params - params.require(:individual).permit(:gender, :nom, :prenom, :birthdate) + def dossier_params + params.require(:dossier).permit(:for_tiers, :mandataire_first_name, :mandataire_last_name, individual_attributes: [:gender, :nom, :prenom, :birthdate, :email]) end def siret_params diff --git a/app/javascript/controllers/for_tiers_controller.ts b/app/javascript/controllers/for_tiers_controller.ts new file mode 100644 index 00000000000..370cc34b71b --- /dev/null +++ b/app/javascript/controllers/for_tiers_controller.ts @@ -0,0 +1,51 @@ +import { ApplicationController } from './application_controller'; + +export class ForTiersController extends ApplicationController { + static values = { procedureid: Number }; + static targets = [ + 'mandataireFirstName', + 'mandataireLastName', + 'forTiers', + 'mandataireBlock', + 'beneficiaireNotificationBlock', + 'email', + 'notificationMethod' + ]; + + declare mandataireFirstNameTarget: HTMLInputElement; + declare mandataireLastNameTarget: HTMLInputElement; + declare forTiersTargets: NodeListOf; + declare mandataireBlockTarget: HTMLElement; + declare beneficiaireNotificationBlockTarget: HTMLElement; + declare notificationMethodTargets: NodeListOf; + declare emailTarget: HTMLInputElement; + + connect() { + this.toggleFieldRequirements(); + this.addEventListeners(); + } + + addEventListeners() { + this.forTiersTargets.forEach((radio) => { + radio.addEventListener('change', () => this.toggleFieldRequirements()); + }); + } + + toggleFieldRequirements() { + const forTiersSelected = Array.from(this.forTiersTargets).some( + (radio) => radio.checked && radio.value === 'true' + ); + this.mandataireBlockTarget.style.display = forTiersSelected + ? 'block' + : 'none'; + this.beneficiaireNotificationBlockTarget.style.display = forTiersSelected + ? 'block' + : 'none'; + this.mandataireFirstNameTarget.required = forTiersSelected; + this.mandataireLastNameTarget.required = forTiersSelected; + this.emailTarget.required = forTiersSelected; + this.notificationMethodTargets.forEach( + (radio) => (radio.required = forTiersSelected) + ); + } +}