-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Admin] Flashes helper and reorganization #6280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c6fcd6
Add FlashHelper to differentiate between toasts and alerts
chaimann 0292a6c
Update controllers to use flash[:notice]
chaimann b191dc3
Update application view to render `toasts` from helper
chaimann c02a105
Add layout container for alerts
chaimann de12315
Move flashes to dedicated layout components
chaimann 0bd1b35
Support default alert titles
chaimann 2526156
Support flash[:alert] = <String>
chaimann 27adf3d
Update alert attribute "description" to "message"
chaimann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
admin/app/components/solidus_admin/layout/flashes/alerts/component.html.erb
This file contains hidden or 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,7 @@ | ||
<div id="flash_alerts" class="flex items-center justify-center"> | ||
<div class="fixed w-2/3 top-3 flex flex-col gap-3 pointer-events-none" role="alert"> | ||
<% alerts.each do |key, text| %> | ||
<%= render component("ui/alert").new(title: text[:title], message: text[:message], scheme: key.to_sym) %> | ||
<% end %> | ||
</div> | ||
</div> |
19 changes: 19 additions & 0 deletions
19
admin/app/components/solidus_admin/layout/flashes/alerts/component.rb
This file contains hidden or 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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Layout::Flashes::Alerts::Component < SolidusAdmin::BaseComponent | ||
attr_reader :alerts | ||
|
||
# Construct alert flashes like: | ||
# flash[:alert] = { <alert_type>: { title: "", message: "" } } | ||
# See +SolidusAdmin::UI::Alert::Component::SCHEMES+ for available alert types. | ||
# | ||
# If a string is passed to flash[:alert], we treat it is a body of the alert message and fall back to +danger+ type | ||
# and default title (see +SolidusAdmin::UI::Alert::Component+). | ||
def initialize(alerts:) | ||
if alerts.is_a?(String) | ||
alerts = { danger: { message: alerts } } | ||
end | ||
|
||
@alerts = alerts.slice(*SolidusAdmin::UI::Alert::Component::SCHEMES.keys) | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
admin/app/components/solidus_admin/layout/flashes/toasts/component.html.erb
This file contains hidden or 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,5 @@ | ||
<div id="flash_toasts" class="fixed inset-x-0 bottom-3 flex items-center justify-center flex-col gap-3 pointer-events-none" role="alert"> | ||
<% toasts.each do |key, message| %> | ||
<%= render component("ui/toast").new(text: message, scheme: key.to_sym == :error ? :error : :default) %> | ||
<% end %> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
admin/app/components/solidus_admin/layout/flashes/toasts/component.rb
This file contains hidden or 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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Layout::Flashes::Toasts::Component < SolidusAdmin::BaseComponent | ||
attr_reader :toasts | ||
|
||
def initialize(toasts:) | ||
@toasts = toasts | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
en: | ||
close: Close | ||
close: "Close" | ||
defaults: | ||
titles: | ||
danger: "Caution" | ||
info: "Info" | ||
success: "Success" | ||
warning: "Warning" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# Reserve :alert for messages that should go in UI alert component. Everything else will be shown in UI toast. | ||
module SolidusAdmin | ||
module FlashHelper | ||
def toasts | ||
flash.to_hash.with_indifferent_access.except(:alert) | ||
end | ||
|
||
def alerts | ||
flash.to_hash.with_indifferent_access.fetch(:alert, {}) | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
admin/spec/components/solidus_admin/layout/flashes/alerts/component_spec.rb
This file contains hidden or 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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::Layout::Flashes::Alerts::Component, type: :component do | ||
let(:component) { described_class.new(alerts:) } | ||
|
||
context "when alerts passed as Hash" do | ||
let(:alerts) do | ||
{ warning: { title: "Be careful", message: "Something fishy going on" } } | ||
end | ||
|
||
it "renders correctly" do | ||
render_inline(component) | ||
|
||
aggregate_failures do | ||
expect(page).to have_content("Be careful") | ||
expect(page).to have_content("Something fishy going on") | ||
end | ||
end | ||
end | ||
|
||
context "when alerts passed as String" do | ||
let(:alerts) { "Something fishy going on" } | ||
|
||
it "renders correctly" do | ||
render_inline(component) | ||
|
||
aggregate_failures do | ||
expect(page).to have_content("Caution") | ||
expect(page).to have_content("Something fishy going on") | ||
end | ||
end | ||
end | ||
|
||
describe "multiple alerts" do | ||
let(:alerts) do | ||
{ | ||
warning: { title: "Be careful", message: "Something fishy going on" }, | ||
success: { title: "It worked", message: "Nothing to worry about!" } | ||
} | ||
end | ||
|
||
it "renders correctly" do | ||
render_inline(component) | ||
|
||
aggregate_failures do | ||
expect(page).to have_content("Be careful") | ||
expect(page).to have_content("Something fishy going on") | ||
expect(page).to have_content("It worked") | ||
expect(page).to have_content("Nothing to worry about!") | ||
end | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
admin/spec/components/solidus_admin/layout/flashes/toasts/component_spec.rb
This file contains hidden or 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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::Layout::Flashes::Toasts::Component, type: :component do | ||
let(:component) { described_class.new(toasts:) } | ||
|
||
describe "error toast" do | ||
let(:toasts) { { error: "Some error" } } | ||
|
||
it "renders correctly" do | ||
render_inline(component) | ||
|
||
aggregate_failures do | ||
expect(page).to have_content("Some error") | ||
expect(page).to have_css(".bg-red-500") | ||
end | ||
end | ||
end | ||
|
||
describe "default toast" do | ||
let(:toasts) { { notice: "All good" } } | ||
|
||
it "renders correctly" do | ||
render_inline(component) | ||
|
||
aggregate_failures do | ||
expect(page).to have_content("All good") | ||
expect(page).to have_css(".bg-full-black") | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.