Skip to content

Commit

Permalink
Remove op_primer_flash as it is no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Sep 26, 2024
1 parent 1d073a7 commit 3363485
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/controllers/my_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ def revoke_api_key

# rubocop:disable Rails/ActionControllerFlashBeforeRender
result.on_success do
flash[:op_primer_flash] = { message: t("my.access_token.notice_api_token_revoked") }
flash[:notice] = t("my.access_token.notice_api_token_revoked")
end

result.on_failure do |r|
error = r.errors.map(&:message).join("; ")
Rails.logger.error("Failed to revoke api token ##{current_user.id}: #{error}")
flash[:op_primer_flash] = { message: t("my.access_token.failed_to_revoke_token", error:), scheme: :danger }
flash[:error] = t("my.access_token.failed_to_revoke_token", error:)
end
# rubocop:enable Rails/ActionControllerFlashBeforeRender

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/errors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def render_error(arg) # rubocop:disable Metrics/AbcSize
respond_to do |format|
format.html do
error_message = "[#{I18n.t(:error_code, code: status)}] #{message}\n#{message_details}"
flash.now[:op_primer_flash] = { scheme: :danger, message: error_message, dismiss_scheme: :none }
flash.now[:error] = { message: error_message, dismiss_scheme: :none }
render template: "common/error",
layout: use_layout,
status:,
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/flash_messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def render_flash_messages
end

def render_flash_content(key, content)
case key
when :op_primer_flash
render_flash_message(content[:scheme] || :default, content[:message], **content.except(:message))
case content
when Hash
render_flash_message(key, **content)
else
render_flash_message(key, content)
render_flash_message(key, message: content)
end
end

Expand All @@ -74,8 +74,8 @@ def mapped_flash_type(type)
end
end

def render_flash_message(type, message, **args)
render(OpPrimer::FlashComponent.new(scheme: mapped_flash_type(type), **args.except(:scheme))) do
def render_flash_message(type, message:, **args)
render(OpPrimer::FlashComponent.new(scheme: mapped_flash_type(type), **args)) do
join_flash_messages(message)
end
end
Expand Down
20 changes: 15 additions & 5 deletions lookbook/docs/patterns/08-flash-banner.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ See below for available arguments.
## Render primer banner / flash from controllers

In many views in OpenProject, you will find `flash[:notice], flash[:alert], flash[:error]` messages.
These are rendered using the previous `op-flash` styles, and are not yet primerized.
All of these are now rendered with primer banners by default.

So if you find yourself in need to render a primerized flash. You can use this pattern instead:
If you find the need to customize the rendering of the flash, you can also pass a hash to the flash:

```ruby
flash[:op_primer_flash] = { message: "Successful update", scheme: :success, icon: :check }
flash[:notice] = { message: "Successful update", icon: :check }
```

Or for an error:

```ruby
flash[:op_primer_flash] = { message: "Oh no! Something went wrong", scheme: :danger, icon: :alert }
flash[:error] = { message: "Oh no! Something went wrong", icon: :alert }
```

These are rendered in the layout through the `render_banner_messages` helper using the `OpPrimer::FlashComponent`.
If you want to render multiple lines, `message` can also be an array that will be joined by breaklines for rendering:

```ruby
flash[:error] = { message: ["Oh no! Something went wrong", "Some more details here"], icon: :alert }
```

If you want to make the flash non-dismissable, you can pass `dismiss_scheme: :none`:

```ruby
flash[:error] = { message: "Oh noes!"], icon: :alert, dismiss_scheme: :none }
```

## Usage in turbo streams

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def create
service_result = call_update_service

if service_result.success?
flash[:op_primer_flash] = {
message: I18n.t(:"storages.notice_successful_storage_connection"),
scheme: :success
}
flash[:notice] = I18n.t(:"storages.notice_successful_storage_connection")
redirect_to edit_admin_settings_storage_path(@storage)
else
respond_with_ampf_form_turbo_stream_or_edit_html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def show_redirect_uri
end

def finish_setup
flash[:op_primer_flash] = { message: I18n.t(:"storages.notice_successful_storage_connection"), scheme: :success }
flash[:notice] = I18n.t(:"storages.notice_successful_storage_connection")

redirect_to edit_admin_settings_storage_path(@storage)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def change_health_notifications_enabled
update_via_turbo_stream(component: Storages::Admin::SidePanel::HealthNotificationsComponent.new(storage: @storage))
respond_with_turbo_streams
else
flash.now[:op_primer_flash] = {
message: I18n.t("storages.health_email_notifications.error_could_not_be_saved"), scheme: :danger
}
flash.now[:error] = I18n.t("storages.health_email_notifications.error_could_not_be_saved")
render :edit
end
end
Expand All @@ -209,11 +207,11 @@ def destroy

# rubocop:disable Rails/ActionControllerFlashBeforeRender
service_result.on_failure do
flash[:op_primer_flash] = { message: join_flash_messages(service_result.errors.full_messages), scheme: :danger }
flash[:error] = service_result.errors.full_messages
end

service_result.on_success do
flash[:op_primer_flash] = { message: I18n.t(:notice_successful_delete), scheme: :success }
flash[:notice] = I18n.t(:notice_successful_delete)
end
# rubocop:enable Rails/ActionControllerFlashBeforeRender

Expand Down Expand Up @@ -265,7 +263,7 @@ def prepare_storage_for_access_management_form
def ensure_valid_provider_type_selected
short_provider_type = params[:provider]
if short_provider_type.blank? || (@provider_type = ::Storages::Storage::PROVIDER_TYPE_SHORT_NAMES[short_provider_type]).blank?
flash[:op_primer_flash] = { message: I18n.t("storages.error_invalid_provider_type"), scheme: :danger }
flash[:error] = I18n.t("storages.error_invalid_provider_type")
redirect_to admin_settings_storages_path
end
end
Expand Down

0 comments on commit 3363485

Please sign in to comment.