Skip to content

Commit

Permalink
Merge pull request #15044 from opf/bug/53394-oauth-flow-causes-loss-o…
Browse files Browse the repository at this point in the history
…f-already-selected-option-while-adding-a-storage-to-a-project

[#53394] OAuth flow causes loss of already selected option while adding a storage to a project.
  • Loading branch information
ba1ash authored Mar 20, 2024
2 parents 911099a + 25d01e4 commit ba1b99b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default class ProjectStorageFormController extends Controller {
}

this.toggleFolderDisplay(this.folderModeValue);
this.setProjectFolderModeQueryParam(this.folderModeValue);
});
}

Expand Down Expand Up @@ -158,8 +159,8 @@ export default class ProjectStorageFormController extends Controller {
}

this.folderModeValue = mode;

this.toggleFolderDisplay(mode);
this.setProjectFolderModeQueryParam(mode);
}

private get modalService():Observable<OpModalService> {
Expand Down Expand Up @@ -208,6 +209,12 @@ export default class ProjectStorageFormController extends Controller {
);
}

private setProjectFolderModeQueryParam(mode:string) {
const url = new URL(window.location.href);
url.searchParams.set('storages_project_storage[project_folder_mode]', mode);
window.history.replaceState(window.history.state, '', url);
}

private toggleFolderDisplay(value:string):void {
// If the manual radio button is selected, show the manual folder selection section
if (this.hasProjectFolderSectionTarget && value === 'manual') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ def index
@project_storages = Storages::ProjectStorage.where(project: @project).includes(:storage)
# Render the list storages using ViewComponents in the /app/components folder which defines
# the ways rows are rendered in a table layout.
render '/storages/project_settings/index'
render "/storages/project_settings/index"
end

# Show a HTML page with a form in order to create a new ProjectStorage
# Called by: When a user clicks on the "+New" button in Project -> Settings -> File Storages
def new
@available_storages = available_storages
@project_storage = ::Storages::ProjectStorages::SetAttributesService
.new(user: current_user,
model: Storages::ProjectStorage.new,
contract_class: EmptyContract)
.call(project: @project,
storage: @available_storages.find do |storage|
storage.id.to_s == params.dig(:storages_project_storage, :storage_id)
end)
.result
project_folder_mode = Storages::ProjectStorage.project_folder_modes.values.find do |mode|
mode == params.dig(:storages_project_storage, :project_folder_mode)
end
storage = @available_storages.find { |s| s.id.to_s == params.dig(:storages_project_storage, :storage_id) }
@project_storage =
::Storages::ProjectStorages::SetAttributesService
.new(user: current_user, model: Storages::ProjectStorage.new, contract_class: EmptyContract)
.call(project: @project, storage:, project_folder_mode:)
.result
@last_project_folders = {}

render template: '/storages/project_settings/new'
render template: "/storages/project_settings/new"
end

# Create a new ProjectStorage object.
Expand All @@ -89,7 +89,7 @@ def create
redirect_to_project_storages_path_with_oauth_access_grant_confirmation
else
@available_storages = available_storages
render '/storages/project_settings/new'
render "/storages/project_settings/new"
end
end

Expand Down Expand Up @@ -128,7 +128,7 @@ def edit
.pluck(:mode, :origin_folder_id)
.to_h

render '/storages/project_settings/edit'
render "/storages/project_settings/edit"
end

# Update is similar to create above
Expand All @@ -145,7 +145,7 @@ def update
redirect_to project_settings_project_storages_path
else
@project_storage = @object
render '/storages/project_settings/edit'
render "/storages/project_settings/edit"
end
end

Expand All @@ -167,7 +167,7 @@ def destroy
def destroy_info
@project_storage_to_destroy = @object

render '/storages/project_settings/destroy_info'
render "/storages/project_settings/destroy_info"
end

private
Expand All @@ -178,7 +178,7 @@ def permitted_storage_settings_params
# "params" is an instance of ActionController::Parameters
params
.require(:storages_project_storage)
.permit('storage_id', 'project_folder_mode', 'project_folder_id')
.permit("storage_id", "project_folder_mode", "project_folder_id")
.to_h
.reverse_merge(project_id: @project.id)
end
Expand Down Expand Up @@ -212,7 +212,7 @@ def redirect_to_project_storages_path_with_nudge_modal

def oauth_access_grant_nudge_modal(authorized: false)
{
type: 'Storages::Admin::OAuthAccessGrantNudgeModalComponent',
type: "Storages::Admin::OAuthAccessGrantNudgeModalComponent",
parameters: {
project_storage: @project_storage.id,
authorized:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
# See COPYRIGHT and LICENSE files for more details.
#++

# Used by: CreateService when setting attributes
module Storages::ProjectStorages
class SetAttributesService < ::BaseServices::SetAttributes
def set_default_attributes(_params)
def set_default_attributes(params)
project_storage = model
storage = project_storage.storage

project_storage.creator ||= user

project_storage.project_folder_mode ||=
if storage.present? && storage.automatic_management_enabled?
if params[:project_folder_mode].present?
params[:project_folder_mode]
elsif storage.present? && storage.automatic_management_enabled?
"automatic"
else
"inactive"
Expand Down
6 changes: 4 additions & 2 deletions modules/storages/spec/features/manage_project_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@
# Press Edit icon to change the project folder mode to inactive
page.find('.icon.icon-edit').click
expect(page).to have_current_path edit_project_settings_project_storage_path(project_id: project,
id: Storages::ProjectStorage.last)
id: Storages::ProjectStorage.last,
storages_project_storage: {project_folder_mode: 'manual'})
expect(page).to have_text('Edit the file storage to this project')
expect(page).to have_no_select('storages_project_storage_storage_id')
expect(page).to have_text(storage.name)
Expand All @@ -170,7 +171,8 @@
# Click Edit icon again but cancel the edit
page.find('.icon.icon-edit').click
expect(page).to have_current_path edit_project_settings_project_storage_path(project_id: project,
id: Storages::ProjectStorage.last)
id: Storages::ProjectStorage.last,
storages_project_storage: {project_folder_mode: 'inactive'})
expect(page).to have_text('Edit the file storage to this project')
page.click_link('Cancel')
expect(page).to have_current_path project_settings_project_storages_path(project)
Expand Down

0 comments on commit ba1b99b

Please sign in to comment.