From 9d541e28b2dc2ae356acb5b9e6df7bece0b968f4 Mon Sep 17 00:00:00 2001 From: Andrej Date: Tue, 23 Jul 2024 14:50:39 +0200 Subject: [PATCH] Add delete location action [SCI-10864] --- .../storage_locations_controller.rb | 2 +- .../vue/storage_locations/table.vue | 39 ++++++++++++++++++- .../toolbars/storage_locations_service.rb | 10 ++++- config/locales/en.yml | 7 ++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index 4336f8008..7b0fa55df 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -49,7 +49,7 @@ class StorageLocationsController < ApplicationController if @storage_location.discard render json: {} else - render json: { errors: @storage_location.errors.full_messages }, status: :unprocessable_entity + render json: { error: @storage_location.errors.full_messages }, status: :unprocessable_entity end end diff --git a/app/javascript/vue/storage_locations/table.vue b/app/javascript/vue/storage_locations/table.vue index 9f9096a58..945046335 100644 --- a/app/javascript/vue/storage_locations/table.vue +++ b/app/javascript/vue/storage_locations/table.vue @@ -12,6 +12,7 @@ @duplicate="duplicate" @tableReloaded="reloadingTable = false" @move="move" + @delete="deleteStorageLocation" /> + @@ -36,13 +44,15 @@ import axios from '../../packs/custom_axios.js'; import DataTable from '../shared/datatable/table.vue'; import EditModal from './modals/new_edit.vue'; import MoveModal from './modals/move.vue'; +import ConfirmationModal from '../shared/confirmation_modal.vue'; export default { name: 'RepositoriesTable', components: { DataTable, EditModal, - MoveModal + MoveModal, + ConfirmationModal }, props: { dataSource: { @@ -70,7 +80,9 @@ export default { editModalMode: null, editStorageLocation: null, objectToMove: null, - moveToUrl: null + moveToUrl: null, + storageLocationDeleteTitle: '', + storageLocationDeleteDescription: '' }; }, computed: { @@ -200,6 +212,29 @@ export default { move(event, rows) { [this.objectToMove] = rows; this.moveToUrl = event.path; + }, + async deleteStorageLocation(event, rows) { + const storageLocationType = rows[0].container ? this.i18n.t('storage_locations.box') : this.i18n.t('storage_locations.location'); + const description = ` +

${this.i18n.t('storage_locations.index.delete_modal.description_1_html', + { name: rows[0].name, type: storageLocationType, num_of_items: event.number_of_items })}

+

${this.i18n.t('storage_locations.index.delete_modal.description_2_html')}

`; + + this.storageLocationDeleteDescription = description; + this.storageLocationDeleteTitle = this.i18n.t('storage_locations.index.delete_modal.title', { type: storageLocationType }); + const ok = await this.$refs.deleteStorageLocationModal.show(); + if (ok) { + axios.delete(event.path).then((_) => { + this.reloadingTable = true; + HelperModule.flashAlertMsg(this.i18n.t('storage_locations.index.delete_modal.success_message', + { + type: storageLocationType[0].toUpperCase() + storageLocationType.slice(1), + name: rows[0].name + }), 'success'); + }).catch((error) => { + HelperModule.flashAlertMsg(error.response.data.error, 'danger'); + }); + } } } }; diff --git a/app/services/toolbars/storage_locations_service.rb b/app/services/toolbars/storage_locations_service.rb index a6b275b01..900b3004b 100644 --- a/app/services/toolbars/storage_locations_service.rb +++ b/app/services/toolbars/storage_locations_service.rb @@ -74,11 +74,19 @@ module Toolbars return unless can_manage_storage_locations?(current_user.current_team) + storage_location = @storage_locations.first + + number_of_items = storage_location.storage_location_repository_rows.count + + StorageLocation.inner_storage_locations(current_user.current_team, storage_location) + .where(container: true) + .joins(:storage_location_repository_rows) + .count { name: 'delete', label: I18n.t('storage_locations.index.toolbar.delete'), icon: 'sn-icon sn-icon-delete', - path: storage_location_path(@storage_locations.first), + number_of_items: number_of_items, + path: storage_location_path(storage_location), type: :emit } end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1e576d17f..a81190bb9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2671,6 +2671,8 @@ en: errors: my_module_references_missing: 'Task references are not set' storage_locations: + box: 'box' + location: 'location' show: table: position: "Position" @@ -2733,6 +2735,11 @@ en: error_flash: "An error occurred. The selected location/box has not been moved." placeholder: find_storage_locations: 'Find location' + delete_modal: + title: 'Delete a %{type}' + description_1_html: "You're about to delete %{name}. This action will delete the %{type}. %{num_of_items} items inside will lose their assigned positions." + description_2_html: 'Are you sure you want to delete it?' + success_message: "%{type} %{name} successfully deleted." libraries: manange_modal_column_index: