From 63a5459d21adb8321e24198663e65b4fe3303892 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Tue, 10 Mar 2020 13:16:56 +0100 Subject: [PATCH] Add error handling to create task modal --- .../javascripts/dashboard/quick_start.js | 10 ++++++- .../dashboard/create_task_modal.scss | 26 ++++++++++++++----- .../dashboard/quick_start_controller.rb | 4 +-- app/services/create_experiment_service.rb | 15 ++++++++--- app/services/create_my_module_service.rb | 9 +++++-- app/services/create_project_service.rb | 17 +++++++++--- .../dashboards/_create_task_modal.html.erb | 4 +-- 7 files changed, 63 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/dashboard/quick_start.js b/app/assets/javascripts/dashboard/quick_start.js index f345867a9..809c3d72c 100644 --- a/app/assets/javascripts/dashboard/quick_start.js +++ b/app/assets/javascripts/dashboard/quick_start.js @@ -10,6 +10,7 @@ var DasboardQuickStartWidget = (function() { function initNewTaskModal() { $('.quick-start-widget .new-task').click(() => { $('#create-task-modal').modal('show'); + $('#create-task-modal .select-block').attr('data-error', ''); }); dropdownSelector.init(projectFilter, { @@ -75,7 +76,7 @@ var DasboardQuickStartWidget = (function() { } }); - $(createTaskButton).click(() => { + $(createTaskButton).click((e) => { var params = {}; if (dropdownSelector.getValues(projectFilter) === '0') { params.project = { @@ -90,8 +91,15 @@ var DasboardQuickStartWidget = (function() { } else { params.experiment = { id: dropdownSelector.getValues(experimentFilter) }; } + e.stopPropagation(); + e.preventDefault(); + $('#create-task-modal .select-block').attr('data-error', ''); $.post($(createTaskButton).data('ajaxUrl'), params, function(data) { window.location.href = data.my_module_path; + }).error((response) => { + var errorsObject = response.responseJSON.error_object; + var errorsText = response.responseJSON.errors.name.join(' '); + $(`#create-task-modal .select-block[data-error-object="${errorsObject}"]`).attr('data-error', errorsText); }); }); } diff --git a/app/assets/stylesheets/dashboard/create_task_modal.scss b/app/assets/stylesheets/dashboard/create_task_modal.scss index e9dc8d0cf..e627f8511 100644 --- a/app/assets/stylesheets/dashboard/create_task_modal.scss +++ b/app/assets/stylesheets/dashboard/create_task_modal.scss @@ -21,6 +21,12 @@ margin-bottom: 5px; user-select: none; } + + &::after { + display: block; + color: $brand-danger; + content: attr(data-error); + } } .dropdown-selector-container { @@ -29,14 +35,18 @@ } } + .new-projects-visibility { + position: relative; + } + .down-arrow { background-color: $color-white; box-shadow: -4px 4px 0 $color-black; display: inline-block; height: 38px; - margin: 0 6px 24px 14px; + margin: 0 11px 20px 14px; opacity: .2; - width: 18px; + width: 40px; } .down-arrow::before { @@ -45,14 +55,15 @@ border-top: 6px solid transparent; content: ""; height: 0; - left: 40px; + left: 46px; position: absolute; - top: 185px; + top: 36px; width: 0; } .project-visibility-container { display: inline-block; + width: 260px; } .project-visibility-title { @@ -64,12 +75,13 @@ .sci-toggles-group { .sci-toggle-item { - width: 143px; + width: 130px; } .sci-toggle-item-label { - margin-left: -143px; - width: 143px; + margin-left: -130px; + margin-bottom: 0; + width: 130px; } } diff --git a/app/controllers/dashboard/quick_start_controller.rb b/app/controllers/dashboard/quick_start_controller.rb index 8f3d810c2..524f71d2b 100644 --- a/app/controllers/dashboard/quick_start_controller.rb +++ b/app/controllers/dashboard/quick_start_controller.rb @@ -12,10 +12,10 @@ module Dashboard my_module = CreateMyModuleService.new(current_user, current_team, project: @project || create_project_params, experiment: @experiment || create_experiment_params).call - if my_module + if my_module.errors.empty? render json: { my_module_path: protocols_my_module_path(my_module) } else - render json: {}, status: :unprocessable_entity + render json: { errors: my_module.errors, error_object: my_module.class.name }, status: :unprocessable_entity end end diff --git a/app/services/create_experiment_service.rb b/app/services/create_experiment_service.rb index f8a08cbf4..c2d1e4d29 100644 --- a/app/services/create_experiment_service.rb +++ b/app/services/create_experiment_service.rb @@ -8,19 +8,26 @@ class CreateExperimentService end def call + new_experiment = nil ActiveRecord::Base.transaction do unless @params[:project].class == Project @params[:project] = CreateProjectService.new(@user, @team, @params[:project]).call end - raise ActiveRecord::Rollback unless @params[:project] + unless @params[:project]&.errors&.empty? + new_experiment = @params[:project] + raise ActiveRecord::Rollback + end @params[:created_by] = @user @params[:last_modified_by] = @user - @experiment = @params[:project].experiments.create!(@params) - create_experiment_activity - @experiment + @experiment = @params[:project].experiments.new(@params) + + create_experiment_activity if @experiment.save + + new_experiment = @experiment end + new_experiment end private diff --git a/app/services/create_my_module_service.rb b/app/services/create_my_module_service.rb index 7c1997891..144f53521 100644 --- a/app/services/create_my_module_service.rb +++ b/app/services/create_my_module_service.rb @@ -9,12 +9,16 @@ class CreateMyModuleService end def call + new_my_module = nil ActiveRecord::Base.transaction do unless @params[:experiment].class == Experiment @params[:experiment][:project] = @params[:project] @params[:experiment] = CreateExperimentService.new(@user, @team, @params[:experiment]).call end - raise ActiveRecord::Rollback unless @params[:experiment] + unless @params[:experiment]&.errors&.empty? + new_my_module = @params[:experiment] + raise ActiveRecord::Rollback + end @my_module_params[:x] ||= 0 @my_module_params[:y] ||= 0 @@ -29,8 +33,9 @@ class CreateMyModuleService @my_module.save! create_my_module_activity @params[:experiment].generate_workflow_img - @my_module + new_my_module = @my_module end + new_my_module end private diff --git a/app/services/create_project_service.rb b/app/services/create_project_service.rb index c23c55e87..75c4fd762 100644 --- a/app/services/create_project_service.rb +++ b/app/services/create_project_service.rb @@ -8,15 +8,24 @@ class CreateProjectService end def call + new_project = nil ActiveRecord::Base.transaction do @params[:created_by] = @user @params[:last_modified_by] = @user - @project = @team.projects.create!(@params) - @project.user_projects.create!(role: :owner, user: @user) - create_project_activity - @project + @project = @team.projects.new(@params) + + if @project.save + @project.user_projects.create!(role: :owner, user: @user) + create_project_activity + new_project = @project + else + new_project = @project + raise ActiveRecord::Rollback + + end end + new_project end private diff --git a/app/views/dashboards/_create_task_modal.html.erb b/app/views/dashboards/_create_task_modal.html.erb index cf8380a6b..9ec39df3e 100644 --- a/app/views/dashboards/_create_task_modal.html.erb +++ b/app/views/dashboards/_create_task_modal.html.erb @@ -11,7 +11,7 @@
<%= t("dashboard.create_task_modal.description") %>
-
+