OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Merge branch 'dev' into task/56496-document-primer-flash-modals-pattern
Kabiru Mwenja095f64136100
diff --cc app/helpers/flash_messages_helper.rbindex 4f39df25fe9,fa02f49912c..40300ae197c--- a/app/helpers/flash_messages_helper.rb+++ b/app/helpers/flash_messages_helper.rb@@@ -37,49 -37,46 +37,47 @@@ module FlashMessagesHelpe# Renders flash messagesdef render_flash_messagesmessages = flash- .reject { |k, _| k.to_s == "op_primer_flash" }.reject { |k, _| k.start_with? "_" }- .map do |k, v|- if k.to_sym == :op_modal- render_op_modal_flash_component(component: v[:component], parameters: v.fetch(:parameters, {}))- else- render_flash_message(k, v)- end- end- .reject { |k, _| k.to_s == "modal" }++ .reject { |k, _| k.to_s == "op_modal" }+ .map { |k, v| render_flash_content(k.to_sym, v) }safe_join messages, "\n"end- def render_op_modal_flash_component(component:, parameters: {})- component = component.constantize if component.is_a?(String)-- component.new(**parameters).render_in(self)+ def render_flash_content(key, content)+ case content+ when Hash+ render_flash_message(key, **content)+ else+ render_flash_message(key, message: content)+ endend- def render_flash_message(type, message, html_options = {}) # rubocop:disable Metrics/AbcSize- if type.to_s == "notice"- type = "success"- end+ def render_flash_modal- content = flash[:modal]- return if content.blank?++ return if (content = flash[:op_modal]).blank?+- toast_css_classes = ["op-toast -#{type}", html_options.delete(:class)]++ component = content[:component]++ component = component.constantize if component.is_a?(String)+- component = content[:type].constantize+ component.new(**content.fetch(:parameters, {})).render_in(self)+ end- # Add autohide class to notice flashes if configured- if type.to_s == "success" && User.current.pref.auto_hide_popups?- toast_css_classes << "autohide-toaster"+ def mapped_flash_type(type)+ case type+ when :error, :danger+ :danger+ when :warning+ :warning+ when :success, :notice+ :success+ else+ :defaultend+ end- html_options = { class: toast_css_classes.join(" "), role: "alert" }.merge(html_options)- close_button = content_tag :a, "", class: "op-toast--close icon-context icon-close",- title: I18n.t("js.close_popup_title"),- tabindex: "0"- toast = content_tag(:div, join_flash_messages(message), class: "op-toast--content")- content_tag :div, "", class: "op-toast--wrapper" do- content_tag :div, "", class: "op-toast--casing" do- content_tag :div, html_options do- concat(close_button)- concat(toast)- end- end+ def render_flash_message(type, message:, **args)+ render(OpPrimer::FlashComponent.new(scheme: mapped_flash_type(type), **args)) do+ join_flash_messages(message)endendenddiff --cc lookbook/docs/patterns/09-flash-modal.md.erbindex 5cd1aa59180,00000000000..8bdb82a1808mode 100644,000000..100644--- a/lookbook/docs/patterns/09-flash-modal.md.erb+++ b/lookbook/docs/patterns/09-flash-modal.md.erb@@@ -1,60 -1,0 +1,60 @@@+This pattern defines use cases where a modal view component is rendered on the next request via a [flash](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html+). This is useful when you want to display a modal after a user action, such as a form submission or on a callback request from an external service.++> *_[Flash](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html) provides a way to pass temporary primitive types between controller actions. Anything you place in the flash is stored in the session and will be exposed to the very next action and then cleared out._++## Overview++To flash a modal, set a `flash[:op_modal]` hash in your controller action. The hash should contain the following keys:+ * `component:` - the modal component to render+ * `parameters:` - the parameters to pass to the modal component++You can use the helper method `flash_op_modal` to set the flash modal properties.++Ex.++```ruby+flash_op_modal component: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent,+ parameters: { storage: storage.id }+```+- Internally, the `FlashMessagesHelper#render_flash_messages` will select flash messages with the key `:op_modal` and render the modal component with the given parameters.++Internally, the `FlashMessagesHelper#render_flash_modal` will select flash messages with the key `:op_modal` and render the modal component with the given parameters.++P.S. When the `component` is a `Primer::Alpha::Dialog` the `auto-show-dialog` stimulus controller should be declared in order to automatically show the dialog when the page is loaded.++Ex.++```ruby+render(+ Primer::Alpha::Dialog.new(+ id: dialog_id,+ title:,+ data: {+ controller: "auto-show-dialog",+ }+) do+ # ....+end+```++### Flash a modal on callback from external service++When you want to display a modal after a callback request from an external service, store the op modal component directly in the `session` in the controller action that performs by calling `OpModalFlashable#store_callback_op_modal_flash` bofore the open redirect and then extract them in the controller action that handles the callback via `OpTurbo::Flashable#retrieve_callback_op_modal_flash`.++Ex.++```ruby+# Controller action that performs the open redirect+def redirect_to_external_service+ store_callback_op_modal_flash component: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent,+ parameters: { storage: storage.id }+ redirect_to external_service_url+end++# Controller action that handles the callback+def callback_from_external_service+ # Handle the callback+ redirect_to some_path, op_modal: retrieve_callback_op_modal_flash+end+```+