|
| 1 | +module Forms |
| 2 | + class MakeLanguageLiveController < FormsController |
| 3 | + def new |
| 4 | + authorize current_form, :can_make_language_live? |
| 5 | + return redirect_to form_path(form_id: current_form.id) unless current_form.can_make_language_live?(language: params[:language]) |
| 6 | + |
| 7 | + @make_language_live_input = MakeLiveInput.new(form: current_form) |
| 8 | + |
| 9 | + render_new |
| 10 | + end |
| 11 | + |
| 12 | + def create |
| 13 | + authorize current_form, :can_make_language_live? |
| 14 | + |
| 15 | + @make_language_live_input = MakeLiveInput.new(**make_language_live_input_params) |
| 16 | + |
| 17 | + return render_new(status: :unprocessable_content) unless @make_language_live_input.valid? |
| 18 | + return redirect_to form_path(@make_language_live_input.form.id) unless @make_language_live_input.confirmed? |
| 19 | + return redirect_to form_path(form_id: current_form.id) unless current_form.can_make_language_live?(language: params[:language]) |
| 20 | + |
| 21 | + @make_form_live_service = MakeFormLiveService.call(current_form:, current_user:, language: params[:language]) |
| 22 | + @make_form_live_service.make_language_live |
| 23 | + |
| 24 | + if current_form.state_previously_changed? |
| 25 | + OrgAdminAlertsService.new(form: current_form, current_user:).form_made_live |
| 26 | + end |
| 27 | + |
| 28 | + redirect_to make_language_live_show_confirmation_path |
| 29 | + end |
| 30 | + |
| 31 | + def show_confirmation |
| 32 | + authorize current_form, :can_make_language_live? |
| 33 | + @go_to_make_welsh_live_input = GoToMakeWelshLiveInput.new |
| 34 | + |
| 35 | + render_confirmation |
| 36 | + end |
| 37 | + |
| 38 | + def submit_confirmation |
| 39 | + authorize current_form, :can_make_language_live? |
| 40 | + |
| 41 | + @go_to_make_welsh_live_input = GoToMakeWelshLiveInput.new(**go_to_make_welsh_live_input_params) |
| 42 | + |
| 43 | + return render_confirmation(status: :unprocessable_content) unless @go_to_make_welsh_live_input.valid? |
| 44 | + |
| 45 | + if @go_to_make_welsh_live_input.confirmed? |
| 46 | + redirect_to make_language_live_path(language: "cy") |
| 47 | + else |
| 48 | + redirect_to form_path |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + private |
| 53 | + |
| 54 | + def make_language_live_input_params |
| 55 | + params.require(:forms_make_live_input).permit(:confirm).merge(form: current_form, language: params[:language]) |
| 56 | + end |
| 57 | + |
| 58 | + def render_new(status: :ok) |
| 59 | + render "new", status:, locals: { current_form:, language: params[:language] } |
| 60 | + end |
| 61 | + |
| 62 | + def render_confirmation(status: :ok) |
| 63 | + @make_form_live_service = MakeFormLiveService.call(current_form:, current_user:, language: params[:language]) |
| 64 | + |
| 65 | + render "confirmation", status:, locals: { |
| 66 | + current_form:, |
| 67 | + confirmation_page_title: @make_form_live_service.page_title, |
| 68 | + confirmation_page_body: @make_form_live_service.confirmation_page_body, |
| 69 | + language: params[:language], |
| 70 | + } |
| 71 | + end |
| 72 | + |
| 73 | + def go_to_make_welsh_live_input_params |
| 74 | + params.require(:forms_go_to_make_welsh_live_input).permit(:confirm) |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments