Skip to content

Commit

Permalink
Add CopyCoursesForm for managing form errors
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Dec 9, 2024
1 parent 07eaba3 commit afcf251
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
26 changes: 16 additions & 10 deletions app/controllers/support/providers/copy_courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ module Support
module Providers
class CopyCoursesController < SupportController
before_action :recruitment_cycle

def new
@provider = Provider.find(params[:provider_id])
@target_provider = Provider.find(params[:provider_id])
@copy_courses_form = CopyCoursesForm.new(@target_provider)
end

def create
@provider = Provider.find(params[:provider_id])
@from_provider = recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code])
@target_provider = Provider.find(params[:provider_id])
@copy_courses_form = CopyCoursesForm.new(@target_provider, recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code]))

sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {}
if @copy_courses_form.valid?
sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {}

copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true)
copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true)

Provider.transaction do
@from_provider.courses.map do |course|
copier.execute(course:, new_provider: @provider)
Provider.transaction do
@copy_courses_form.provider.courses.map do |course|
copier.execute(course:, new_provider: @copy_courses_form.target_provider)
end
end
end

redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @provider.id)
redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @copy_courses_form.target_provider.id)
else
render :new
end
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions app/forms/support/copy_courses_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Support
class CopyCoursesForm
include ActiveModel::Model

attr_reader :target_provider, :provider

def initialize(target_provider, provider = nil)
@target_provider = target_provider
@provider = provider
end

validates :target_provider, :provider, presence: true
end
end
42 changes: 22 additions & 20 deletions app/views/support/providers/copy_courses/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<h1 class="govuk-heading-l"><%= @provider.provider_name %></h1>
<h2 class="govuk-heading-m">Copy courses from:</h2>
<%= render PageTitle.new(title: "support.providers.copy_courses.new", has_errors: @copy_courses_form.errors.present?) %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<div>
<%= form_with url: support_recruitment_cycle_provider_copy_courses_path(@recruitment_cycle.year, @provider) do |form| %>
<% error = flash[:error] && flash[:error]["message"] == "Name or provider code" %>
<div class="govuk-form-group<% if error %> govuk-form-group--error<% end %>">
<%= form.label :query, { class: "govuk-label", for: "provider" } do %>
<div>Search for an organisation to copy courses from:</div>
<span class="govuk-hint govuk-!-display-inline">Enter the name or provider code</span>
<% if error %>
<span class="govuk-error-message" id="provider-error" data-qa="provider-error">
Please enter the name or provider code
</span>
<% end %>
<% end %>
<%= form.text_field :provider,
id: "provider",
value: params[:query],
class: "govuk-input",
data: { qa: "provider-search" } %>
<div id="provider-autocomplete"></div>
<%= form_with model: @copy_courses_form, url: support_recruitment_cycle_provider_copy_courses_path(@recruitment_cycle.year, @target_provider) do |form| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<%= form.govuk_error_summary %>
</div>
</div>
<h1 class="govuk-heading-l"><%= @target_provider.provider_name %></h1>
<h2 class="govuk-heading-m">Copy courses from:</h2>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<div class="govuk-form-group">
<%= form.govuk_text_field :provider,
id: "provider",
value: params[:query],
class: "govuk-input",
data: { qa: "provider-search" },
label: -> { tag.div("Search for an organisation to copy courses from:") + tag.div("Enter the name or provider code", class: %w[govuk-hint govuk-!-display-inline]) } %>
<div id="provider-autocomplete"></div>
</div>
</div>
</div>

<div class="govuk-grid-row">
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ en:
show: "View a provider"
edit: "Edit a provider"
edit_contact: "Edit provider contact details"
copy_courses:
new: Copy courses
courses:
index: "Courses"
edit: "Edit course details"
Expand Down Expand Up @@ -1028,6 +1030,8 @@ en:
blank: "Enter a first name"
last_name:
blank: "Enter a last name"
support/copy_courses_form:
blank: Provider can't be blank
support/user_form:
attributes:
email:
Expand Down

0 comments on commit afcf251

Please sign in to comment.