From 3b2748ae71911a04e5a17440ffc8f13464c81d7a Mon Sep 17 00:00:00 2001 From: Andrej Date: Tue, 30 Jul 2024 09:31:21 +0200 Subject: [PATCH] Add state saving for result [SCI-10794] --- .../settings/user_settings_controller.rb | 16 +++++++------- app/javascript/vue/results/result.vue | 21 +++++++++++++++++++ app/javascript/vue/results/results.vue | 8 +++++-- app/jobs/cleanup_user_settings_job.rb | 4 +++- app/models/result.rb | 3 +++ app/serializers/result_serializer.rb | 7 ++++++- config/initializers/extends.rb | 1 + 7 files changed, 48 insertions(+), 12 deletions(-) diff --git a/app/controllers/users/settings/user_settings_controller.rb b/app/controllers/users/settings/user_settings_controller.rb index 2b106bc17..04620a496 100644 --- a/app/controllers/users/settings/user_settings_controller.rb +++ b/app/controllers/users/settings/user_settings_controller.rb @@ -17,8 +17,8 @@ module Users next unless Extends::WHITELISTED_USER_SETTINGS.include?(key.to_s) case key.to_s - when 'task_step_states' - update_task_step_states(data) + when 'task_step_states', 'result_states' + update_object_states(data, key.to_s) else current_user.settings[key] = data end @@ -34,18 +34,18 @@ module Users private - def update_task_step_states(task_step_states_data) - current_states = current_user.settings.fetch('task_step_states', {}) + def update_object_states(object_states_data, object_state_key) + current_states = current_user.settings.fetch(object_state_key, {}) - task_step_states_data.each do |step_id, collapsed| + object_states_data.each do |object_id, collapsed| if collapsed - current_states[step_id] = true + current_states[object_id] = true else - current_states.delete(step_id) + current_states.delete(object_id) end end - current_user.settings['task_step_states'] = current_states + current_user.settings[object_state_key] = current_states end end end diff --git a/app/javascript/vue/results/result.vue b/app/javascript/vue/results/result.vue index bb2137b43..cd8816346 100644 --- a/app/javascript/vue/results/result.vue +++ b/app/javascript/vue/results/result.vue @@ -158,6 +158,9 @@ export default { resultToReload: { type: Number, required: false }, activeDragResult: { required: false + }, + userSettingsUrl: { + required: false } }, data() { @@ -215,6 +218,17 @@ export default { deep: true } }, + mounted() { + this.$nextTick(() => { + const resultId = `#resultBody${this.result.id}`; + this.isCollapsed = this.result.attributes.collapsed; + if (this.isCollapsed) { + $(resultId).collapse('hide'); + } else { + $(resultId).collapse('show'); + } + }); + }, computed: { reorderableElements() { return this.orderedElements.map((e) => ({ id: e.id, attributes: e.attributes.orderable })); @@ -321,6 +335,13 @@ export default { toggleCollapsed() { this.isCollapsed = !this.isCollapsed; this.result.attributes.collapsed = this.isCollapsed; + + const settings = { + key: 'result_states', + data: { [this.result.id]: this.isCollapsed } + }; + + axios.put(this.userSettingsUrl, { settings: [settings] }); }, dragEnter(e) { if (!this.urls.upload_attachment_url) return; diff --git a/app/javascript/vue/results/results.vue b/app/javascript/vue/results/results.vue index 23eefb5e8..3b4bc97dc 100644 --- a/app/javascript/vue/results/results.vue +++ b/app/javascript/vue/results/results.vue @@ -22,6 +22,7 @@ :result="result" :resultToReload="resultToReload" :activeDragResult="activeDragResult" + :userSettingsUrl="userSettingsUrl" @result:elements:loaded="resultToReload = null" @result:move_element="reloadResult" @result:attachments:loaded="resultToReload = null" @@ -64,7 +65,8 @@ export default { canCreate: { type: String, required: true }, archived: { type: String, required: true }, active_url: { type: String, required: true }, - archived_url: { type: String, required: true } + archived_url: { type: String, required: true }, + userSettingsUrl: { type: String, required: false } }, data() { return { @@ -74,10 +76,12 @@ export default { resultToReload: null, nextPageUrl: null, loadingPage: false, - activeDragResult: null + activeDragResult: null, + userSettingsUrl: null }; }, mounted() { + this.userSettingsUrl = document.querySelector('meta[name="user-settings-url"]').getAttribute('content'); window.addEventListener('scroll', this.loadResults, false); window.addEventListener('scroll', this.initStackableHeaders, false); this.nextPageUrl = this.url; diff --git a/app/jobs/cleanup_user_settings_job.rb b/app/jobs/cleanup_user_settings_job.rb index f8bf283fa..b1554c283 100644 --- a/app/jobs/cleanup_user_settings_job.rb +++ b/app/jobs/cleanup_user_settings_job.rb @@ -4,7 +4,9 @@ class CleanupUserSettingsJob < ApplicationJob queue_as :default def perform(record_type, record_id) - raise ArgumentError, 'Invalid record_type' unless %w(task_step_states results_order).include?(record_type) + unless %w(task_step_states results_order result_states).include?(record_type) + raise ArgumentError, 'Invalid record_type' + end sanitized_record_id = record_id.to_i.to_s raise ArgumentError, 'Invalid record_id' unless sanitized_record_id == record_id.to_s diff --git a/app/models/result.rb b/app/models/result.rb index 767937497..37ea43114 100644 --- a/app/models/result.rb +++ b/app/models/result.rb @@ -37,6 +37,9 @@ class Result < ApplicationRecord accepts_nested_attributes_for :tables before_save :ensure_default_name + after_discard do + CleanupUserSettingsJob.perform_later('result_states', id) + end def self.search(user, include_archived, diff --git a/app/serializers/result_serializer.rb b/app/serializers/result_serializer.rb index 156cc8b7f..9988a28a6 100644 --- a/app/serializers/result_serializer.rb +++ b/app/serializers/result_serializer.rb @@ -10,7 +10,12 @@ class ResultSerializer < ActiveModel::Serializer attributes :name, :id, :urls, :updated_at, :created_at_formatted, :updated_at_formatted, :user, :my_module_id, :attachments_manageble, :marvinjs_enabled, :marvinjs_context, :type, :wopi_enabled, :wopi_context, :created_at, :created_by, :archived, :assets_order, - :open_vector_editor_context, :comments_count, :assets_view_mode, :storage_limit + :open_vector_editor_context, :comments_count, :assets_view_mode, :storage_limit, :collapsed + + def collapsed + result_states = current_user.settings.fetch('result_states', {}) + result_states[object.id.to_s] == true + end def marvinjs_enabled MarvinJsService.enabled? diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index a580cc3cf..684cc56ca 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -675,6 +675,7 @@ class Extends repository_export_file_type navigator_collapsed navigator_width + result_states ).freeze end