From e06b398bb491207079edfadde958812c836c986d Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 3 May 2022 15:15:49 +0200 Subject: [PATCH] Add delete action for step components [SCI-6780] --- .../step_components/checklists_controller.rb | 6 ++++ .../step_components/tables_controller.rb | 6 ++++ .../step_components/texts_controller.rb | 6 ++++ .../step_orderable_elements_controller.rb | 11 +++++++- app/javascript/vue/protocol/container.vue | 4 ++- .../vue/protocol/mixins/components/delete.js | 23 +++++++++++++++ app/javascript/vue/protocol/modals.vue | 13 +++++++++ .../vue/protocol/modals/delete_component.vue | 28 +++++++++++++++++++ app/javascript/vue/protocol/step.vue | 16 +++++++++++ .../protocol/step_components/checklist.vue | 13 ++++++++- .../vue/protocol/step_components/table.vue | 13 ++++++++- .../vue/protocol/step_components/text.vue | 13 ++++++++- app/serializers/checklist_serializer.rb | 12 +++++++- app/serializers/step_table_serializer.rb | 12 +++++++- app/serializers/step_text_serializer.rb | 12 +++++++- config/locales/en.yml | 6 +++- config/routes.rb | 6 ++-- 17 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 app/javascript/vue/protocol/mixins/components/delete.js create mode 100644 app/javascript/vue/protocol/modals.vue create mode 100644 app/javascript/vue/protocol/modals/delete_component.vue diff --git a/app/controllers/step_components/checklists_controller.rb b/app/controllers/step_components/checklists_controller.rb index af5b1e7d3..41b23816a 100644 --- a/app/controllers/step_components/checklists_controller.rb +++ b/app/controllers/step_components/checklists_controller.rb @@ -13,5 +13,11 @@ module StepComponents def element_params params.require(:checklist).permit(:name) end + + def load_vars + @element = @step.checklists.find_by(id: params[:id]) + @orderable_element = @element.step_orderable_elements.find_by(step: @step) + return render_404 unless @element && @orderable_element + end end end diff --git a/app/controllers/step_components/tables_controller.rb b/app/controllers/step_components/tables_controller.rb index 630f3f9e4..bafdc6eb7 100644 --- a/app/controllers/step_components/tables_controller.rb +++ b/app/controllers/step_components/tables_controller.rb @@ -16,5 +16,11 @@ module StepComponents def element_params params.require(:table).permit(:name, :contents) end + + def load_vars + @element = @step.tables.find_by(id: params[:id]) + @orderable_element = @element.step_table.step_orderable_elements.find_by(step: @step) + return render_404 unless @element && @orderable_element + end end end diff --git a/app/controllers/step_components/texts_controller.rb b/app/controllers/step_components/texts_controller.rb index 7e04b94d9..8e5e82b24 100644 --- a/app/controllers/step_components/texts_controller.rb +++ b/app/controllers/step_components/texts_controller.rb @@ -11,5 +11,11 @@ module StepComponents def element_params params.require(:step_text).permit(:text) end + + def load_vars + @element = @step.step_texts.find_by(id: params[:id]) + @orderable_element = @element.step_orderable_elements.find_by(step: @step) + return render_404 unless @element && @orderable_element + end end end diff --git a/app/controllers/step_orderable_elements_controller.rb b/app/controllers/step_orderable_elements_controller.rb index 2ee9a4a5a..eae9ee97e 100644 --- a/app/controllers/step_orderable_elements_controller.rb +++ b/app/controllers/step_orderable_elements_controller.rb @@ -2,7 +2,8 @@ class StepOrderableElementsController < ApplicationController before_action :load_vars_nested - before_action :check_manage_permissions, only: :create + before_action :load_vars + before_action :check_manage_permissions, only: %i(create destroy) def create ActiveRecord::Base.transaction do @@ -16,6 +17,14 @@ class StepOrderableElementsController < ApplicationController end end + def destroy + if @element.destroy + render json: @orderable_element, serializer: StepOrderableElementSerializer + else + render json: {}, status: :unprocessable_entity + end + end + private def load_vars_nested diff --git a/app/javascript/vue/protocol/container.vue b/app/javascript/vue/protocol/container.vue index a85a69560..3d247844e 100644 --- a/app/javascript/vue/protocol/container.vue +++ b/app/javascript/vue/protocol/container.vue @@ -57,6 +57,7 @@ {{ i18n.t("protocols.steps.new_step") }} + @@ -64,6 +65,7 @@ import InlineEdit from 'vue/shared/inline_edit.vue' import Step from 'vue/protocol/step' import ProtocolOptions from 'vue/protocol/protocolOptions' + import ProtocolModals from 'vue/protocol/modals' export default { name: 'ProtocolContainer', @@ -85,7 +87,7 @@ required: true } }, - components: { Step, InlineEdit, ProtocolOptions }, + components: { Step, InlineEdit, ProtocolModals, ProtocolOptions }, data() { return { protocol: { diff --git a/app/javascript/vue/protocol/mixins/components/delete.js b/app/javascript/vue/protocol/mixins/components/delete.js new file mode 100644 index 000000000..554277ffd --- /dev/null +++ b/app/javascript/vue/protocol/mixins/components/delete.js @@ -0,0 +1,23 @@ +export default { + methods: { + showDeleteModal() { + $('#modalDestroyProtocolContent').modal('show'); + $('#modalDestroyProtocolContent .delete-step').off().one('click', () => { + this.deleteComponent(); + $('#modalDestroyProtocolContent').modal('hide'); + }); + }, + deleteComponent() { + $.ajax({ + url: this.element.attributes.element.urls.delete_url, + type: 'DELETE', + success: (result) => { + this.$emit( + 'component:delete', + result.data + ); + } + }); + } + } +}; diff --git a/app/javascript/vue/protocol/modals.vue b/app/javascript/vue/protocol/modals.vue new file mode 100644 index 000000000..14b237a53 --- /dev/null +++ b/app/javascript/vue/protocol/modals.vue @@ -0,0 +1,13 @@ + + diff --git a/app/javascript/vue/protocol/modals/delete_component.vue b/app/javascript/vue/protocol/modals/delete_component.vue new file mode 100644 index 000000000..7de8c59ef --- /dev/null +++ b/app/javascript/vue/protocol/modals/delete_component.vue @@ -0,0 +1,28 @@ + + diff --git a/app/javascript/vue/protocol/step.vue b/app/javascript/vue/protocol/step.vue index 82af563b8..b211489b5 100644 --- a/app/javascript/vue/protocol/step.vue +++ b/app/javascript/vue/protocol/step.vue @@ -56,6 +56,7 @@ @@ -115,6 +116,21 @@ HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger'); }) }, + deleteComponent(element) { + let position = element.attributes.position; + this.elements.splice(position, 1) + let unordered_elements = this.elements.map( e => { + if (e.attributes.position >= position) { + e.attributes.position -= 1; + } + return e; + }) + this.reorderComponents(unordered_elements) + + }, + reorderComponents(elements) { + this.elements = elements.sort((a, b) => a.attributes.position - b.attributes.position); + }, updateName(newName) { $.ajax({ url: this.step.attributes.urls.update_url, diff --git a/app/javascript/vue/protocol/step_components/checklist.vue b/app/javascript/vue/protocol/step_components/checklist.vue index 6e8d3ee68..4f5591d4e 100644 --- a/app/javascript/vue/protocol/step_components/checklist.vue +++ b/app/javascript/vue/protocol/step_components/checklist.vue @@ -1,11 +1,22 @@ diff --git a/app/javascript/vue/protocol/step_components/table.vue b/app/javascript/vue/protocol/step_components/table.vue index b56f53111..1c3e2cf17 100644 --- a/app/javascript/vue/protocol/step_components/table.vue +++ b/app/javascript/vue/protocol/step_components/table.vue @@ -1,11 +1,22 @@ diff --git a/app/javascript/vue/protocol/step_components/text.vue b/app/javascript/vue/protocol/step_components/text.vue index d2f643044..adbfee6b2 100644 --- a/app/javascript/vue/protocol/step_components/text.vue +++ b/app/javascript/vue/protocol/step_components/text.vue @@ -1,11 +1,22 @@ diff --git a/app/serializers/checklist_serializer.rb b/app/serializers/checklist_serializer.rb index e087236fd..895c4841e 100644 --- a/app/serializers/checklist_serializer.rb +++ b/app/serializers/checklist_serializer.rb @@ -1,5 +1,15 @@ # frozen_string_literal: true class ChecklistSerializer < ActiveModel::Serializer - attributes :name + include Rails.application.routes.url_helpers + + attributes :name, :urls + + def urls + return unless object.step + + { + delete_url: step_checklist_path(object.step, object) + } + end end diff --git a/app/serializers/step_table_serializer.rb b/app/serializers/step_table_serializer.rb index 5aa4d8d70..482e6ba80 100644 --- a/app/serializers/step_table_serializer.rb +++ b/app/serializers/step_table_serializer.rb @@ -1,5 +1,15 @@ # frozen_string_literal: true class StepTableSerializer < ActiveModel::Serializer - attributes :name + include Rails.application.routes.url_helpers + + attributes :name, :urls + + def urls + return unless object.step + + { + delete_url: step_table_path(object.step, object) + } + end end diff --git a/app/serializers/step_text_serializer.rb b/app/serializers/step_text_serializer.rb index d916477eb..5ed5d9f4e 100644 --- a/app/serializers/step_text_serializer.rb +++ b/app/serializers/step_text_serializer.rb @@ -1,5 +1,15 @@ # frozen_string_literal: true class StepTextSerializer < ActiveModel::Serializer - attributes :text + include Rails.application.routes.url_helpers + + attributes :text, :urls + + def urls + return unless object.step + + { + delete_url: step_text_path(object.step, object) + } + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index b876cb9a9..2974b0df4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2523,7 +2523,11 @@ en: default_name: 'Table %{position}' checklist: default_name: 'Checklist %{position}' - + modals: + delete_component: + title: 'Delete content' + description_1: 'You’re about to delete a content block from your protocol. It might contain data you don’t want to lose. You won’t be able to get it back.' + description_2: 'Are you sure you want to delete it?' options: up_arrow_title: "Move step up" down_arrow_title: "Move step down" diff --git a/config/routes.rb b/config/routes.rb index 0a0e2e642..8ba7797e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -449,9 +449,9 @@ Rails.application.routes.draw do path: '/comments', only: %i(create index update destroy) - resources :tables, controller: 'step_components/tables', only: :create - resources :texts, controller: 'step_components/texts', only: :create - resources :checklists, controller: 'step_components/checklists', only: :create + resources :tables, controller: 'step_components/tables', only: %i(create destroy) + resources :texts, controller: 'step_components/texts', only: %i(create destroy) + resources :checklists, controller: 'step_components/checklists', only: %i(create destroy) member do get 'elements' post 'checklistitem_state'