-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16856 from opf/task/56496-document-primer-flash-m…
…odals-pattern Task/56496 document flash modals pattern
- Loading branch information
Showing
9 changed files
with
134 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2012-2024 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
|
||
module OpModalFlashable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
add_flash_types :op_modal | ||
end | ||
|
||
def flash_op_modal(component:, parameters: {}) | ||
flash[:op_modal] = { component: component.name, parameters: } | ||
end | ||
|
||
def store_callback_op_modal_flash(component:, parameters: {}) | ||
session[:callback_op_modal] = { component: component.name, parameters: } | ||
end | ||
|
||
def retrieve_callback_op_modal_flash | ||
session.delete(:callback_op_modal) if session[:callback_op_modal].present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,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_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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters