Skip to content

Commit

Permalink
Fix: the new submission edit form bugs (#373)
Browse files Browse the repository at this point in the history
* add placeholder argument to select component

* add option to the nested form component to have an empty row at start

* fix select input id vs name mixing, making the submission_metadata_selector to not work as expected

* fix select input component where we can add inputs not valid by default

* fix new ontology form error state can't change acronym

* fix edit ontology metadata form error state not rendering

* reset agents input values to fetch there values if edit metadata on error state

* fix ontology viewOf detaching from  parent

* re-use attr_metadata helper where possible to not call multiple time to submission_metadata endpoint  taking to much time

* fix submission metadata wrong categories got from the API

* not show errors message inside nested inputs

* extract ontology form inputs into helpers to be re-used in submission form

* fix edit ontology metadata form error state not rendering

* hide ontology format input advanced options

* remove the display of an empty row by default for list_field_inputs

* fix usage of reset_agent_attributes concern
  • Loading branch information
syphax-bouazzouni authored Oct 31, 2023
1 parent 42c9c4d commit da306c3
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 134 deletions.
4 changes: 3 additions & 1 deletion app/components/input/select_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def initialize(id: nil, label: '', name:, value: [], selected: '', placeholder:

def call
render Input::InputFieldComponent.new(name: @name, error_message: @error_message, helper_text: @helper_text, label: @label) do
render SelectInputComponent.new(id: @id, name: @name, values: @values, selected: @selected, multiple: @multiple, open_to_add_values: @open_to_add_values, data: @data)
render SelectInputComponent.new(id: @id, name: @name, values: @values, selected: @selected,
placeholder: @placeholder,
multiple: @multiple, open_to_add_values: @open_to_add_values, data: @data)
end
end
end
3 changes: 2 additions & 1 deletion app/components/nested_form_inputs_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class NestedFormInputsComponent < ViewComponent::Base
renders_many :rows
renders_one :empty_state

def initialize(object_name: '')
def initialize(object_name: '', default_empty_row: false)
super
@object_name = object_name
@default_row = default_empty_row
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
%div.delete{data: {action:"click->nested-form#remove"}}
= inline_svg 'icons/delete.svg'

- if rows.empty?
- if @default_row && rows.empty?
%div.d-flex.align-items-center.nested-form-wrapper.my-1{'data-new-record': 'true'}
%div{style: 'width: 90%'}
= template
Expand Down
7 changes: 4 additions & 3 deletions app/components/select_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def initialize(id:, name:, values:, selected: nil, multiple: false, open_to_add_
end

def call
select_input_tag(@id, @values, @selected, multiple: @multiple, open_to_add_values: @open_to_add_values, placeholder: @placeholder)
select_input_tag(@id, @name, @values, @selected, multiple: @multiple, open_to_add_values: @open_to_add_values, placeholder: @placeholder)
end

private

def select_input_tag(id, values, selected, options = {})
def select_input_tag(id, name, values, selected, options = {})
multiple = options[:multiple] || false
open_to_add_values = options[:open_to_add_values] || false
placeholder = options[:placeholder] || ''
Expand All @@ -37,6 +37,7 @@ def select_input_tag(id, values, selected, options = {})
multiple: multiple,
data: data
}
select_tag(id, options_for_select(values, selected), select_html_options)
select_tag(name, options_for_select(values, selected), select_html_options)

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default class extends Controller {
let myOptions = {}

myOptions = {
create: true,
render: {
option: (data) => {
return `<div> ${data.text} </div>`
Expand Down
27 changes: 23 additions & 4 deletions app/controllers/concerns/ontology_updater.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module OntologyUpdater
extend ActiveSupport::Concern
include SubmissionUpdater
include TurboHelper

def update_existent_ontology(acronym)
@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(acronym).first
return nil if @ontology.nil?

@ontology.update(values: ontology_params)
[@ontology, @ontology.update(values: ontology_params)]
end

def ontology_from_params
Expand All @@ -25,6 +26,7 @@ def ontology_params
# p[:acl].reject!(&:blank?)
p[:hasDomain].reject!(&:blank?) if p[:hasDomain]
p[:group].reject!(&:blank?) if p[:group]
p[:viewOf] = '' if p.key?(:viewOf) && !p.key?(:isView)
p.to_h
end

Expand All @@ -36,20 +38,25 @@ def show_new_errors(object, redirection = 'ontologies/new')
@user_select_list = LinkedData::Client::Models::User.all.map { |u| [u.username, u.id] }
@user_select_list.sort! { |a, b| a[1].downcase <=> b[1].downcase }
@errors = response_errors(object)
@is_update_ontology = true
@selected_attributes = (Array(errors_attributes) + Array(params[:submission]&.keys)).uniq
@ontology = ontology_from_params

@submission = submission_from_params(params[:submission]) if params[:submission]
render redirection
if redirection.is_a?(Hash) && redirection[:id]
render_turbo_stream replace(redirection[:id], partial: redirection[:partial])
else
render redirection, status: 422
end
end

def errors_attributes
@errors = @errors[:error] if @errors && @errors[:error]
@errors.keys.map(&:to_s) if @errors.is_a?(Hash)
end

def new_submission_hash
@submission = @ontology.explore.latest_submission({ display: 'all' })
def new_submission_hash(ontology)
@submission = ontology.explore.latest_submission({ display: 'all' })

submission_params = submission_params(params[:submission])

Expand All @@ -69,4 +76,16 @@ def update_submission_hash(acronym)
submission_params[:ontology] = acronym
submission_params
end

private
def reset_agent_attributes
helpers.agent_attributes.each do |attr|
current_val = @submission[attr]
new_values = Array(current_val).map { |x| LinkedData::Client::Models::Agent.find(x) }

new_values = new_values.first unless current_val.is_a?(Array)

@submission[attr] = new_values
end
end
end
5 changes: 1 addition & 4 deletions app/controllers/concerns/submission_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ def submission_from_params(new_submission_hash)
LinkedData::Client::Models::OntologySubmission.new(values: submission_params(new_submission_hash))
end
def save_submission(new_submission_hash)


@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(new_submission_hash[:ontology]).first
@submission = submission_from_params(new_submission_hash)

update_ontology_summary_only
Expand All @@ -31,7 +28,7 @@ def update_submission(new_submission_hash, submission_id)
end

update_ontology_summary_only
@submission.update(values: new_values, cache_refresh_all: false)
[@submission, @submission.update(values: new_values, cache_refresh_all: false)]
end

def add_ontologies_to_object(ontologies,object)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/ontologies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,15 @@ def properties
end

def create
@is_update_ontology = false
@ontology = ontology_from_params.save

if response_error?(@ontology)
show_new_errors(@ontology)
return
end

@submission = save_submission(new_submission_hash)
@submission = save_submission(new_submission_hash(@ontology))

if response_error?(@submission)
@ontology.delete
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ontologies_metadata_curator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def update
new_data = active_submission_data
new_data[:ontology] = onto
new_data[:id] = sub_i
error_responses << update_submission(new_data, sub_i) if new_data
error_responses << update_submission(new_data, sub_i).last if new_data
@submissions << @submission
end

Expand Down
48 changes: 19 additions & 29 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ def create
@is_update_ontology = true

if params[:ontology]
@ontology = update_existent_ontology(params[:ontology_id])
@ontology, response = update_existent_ontology(params[:ontology_id])

if @ontology.nil? || response_error?(@ontology)
show_new_errors(@ontology)
if response.nil? || response_error?(response)
show_new_errors(response)
return
end
end

@submission = save_submission(new_submission_hash)
@submission = save_submission(new_submission_hash(@ontology))

if response_error?(@submission)
show_new_errors(@submission)
Expand All @@ -69,21 +68,24 @@ def edit_properties
def edit
@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology_id]).first
ontology_not_found(params[:ontology_id]) unless @ontology
@categories = LinkedData::Client::Models::Category.all
@groups = LinkedData::Client::Models::Group.all
@user_select_list = LinkedData::Client::Models::User.all.map {|u| [u.username, u.id]}
@user_select_list.sort! {|a,b| a[1].downcase <=> b[1].downcase}
@is_update_ontology = true
category_attributes = submission_metadata.group_by{|x| x['category']}.transform_values{|x| x.map{|attr| attr['attribute']} }
category_attributes = category_attributes.reject{|key| ['no'].include?(key.to_s)}
category_attributes['general'] << %w[acronym name groups administeredBy categories]
category_attributes['license'] << 'viewingRestriction'
category_attributes['relations'] << 'viewOf'
@categories_order = ['general', 'description', 'dates', 'license', 'people', 'links', 'images', 'community', 'usage' ,'relations', 'content','methodology', 'object description properties']
@category_attributes = category_attributes
end

# When editing a submission (called when submit "Edit submission information" form)
def update
@is_update_ontology = true
acronym = params[:ontology_id]
submission_id = params[:id]
if params[:ontology]
@ontology = update_existent_ontology(acronym)
if @ontology.nil? || response_error?(@ontology)
show_new_errors(@ontology, partial: 'submissions/form_content', locals: { id: 'test' })
show_new_errors(@ontology, partial: 'submissions/form_content', id: 'test')
return
end
end
Expand All @@ -93,35 +95,23 @@ def update
notice: 'Submission updated successfully'
end

@submission = update_submission(update_submission_hash(acronym), submission_id)
#reset_agent_attributes
@submission, response = update_submission(update_submission_hash(acronym), submission_id)
if params[:attribute].nil?
if response_error?(@submission)
show_new_errors(@submission, partial: 'submissions/form_content', locals: { id: 'test' })
if response_error?(response)
show_new_errors(response, partial: 'submissions/form_content', id: 'test')
else
redirect_to "/ontologies/#{acronym}",
notice: 'Submission updated successfully'
notice: 'Submission updated successfully', status: :see_other
end
else
@errors = response_errors(@submission) if response_error?(@submission)
@errors = response_errors(response) if response_error?(response)
@submission = submission_from_params(params[:submission])
@submission.submissionId = submission_id
reset_agent_attributes
render_submission_attribute(params[:attribute])
end

end

private

def reset_agent_attributes
helpers.agent_attributes.each do |attr|
current_val = @submission.send(attr)
new_values = Array(current_val).map { |x| LinkedData::Client::Models::Agent.find(x) }

new_values = new_values.first unless current_val.is_a?(Array)

@submission.send("#{attr}=", new_values)
end
end

end
4 changes: 2 additions & 2 deletions app/helpers/components_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def properties_list_component(c, properties, &block)
else
v
end
render FieldContainerComponent.new(label: attr_label(k, show_tooltip: false)) do
render FieldContainerComponent.new(label: attr_label(k, attr_metadata: attr_metadata(k), show_tooltip: false)) do
content
end
end
Expand Down Expand Up @@ -64,7 +64,7 @@ def properties_card(title, tooltip, properties, &block)
def properties_dropdown(id, title, tooltip, properties, &block)
render DropdownContainerComponent.new(title: title, id: id, tooltip: tooltip) do |d|
d.empty_state do
properties_string = properties.keys[0..4].map{|key| "<b>#{attr_label(key, show_tooltip: false)}</b>" }.join(', ')+'... ' if properties
properties_string = properties.keys[0..4].map{|key| "<b>#{attr_label(key, attr_metadata: attr_metadata(key), show_tooltip: false)}</b>" }.join(', ')+'... ' if properties
empty_state_message "The fields #{properties_string} are empty"
end

Expand Down
4 changes: 2 additions & 2 deletions app/helpers/inputs_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module InputsHelper

def text_input(name:, value:, label: nil, disabled: false, help: nil)
def text_input(name:, value:, label: nil, disabled: false, help: nil, error_message: nil)
render Input::TextInputComponent.new(label: input_label(label, name), name: name, value: value,
error_message: input_error_message(name),
error_message: error_message || input_error_message(name),
disabled: disabled,
helper_text: help)
end
Expand Down
Loading

0 comments on commit da306c3

Please sign in to comment.