Class: Practical::Views::FlashMessagesComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/practical/views/flash_messages_component.rb

Instance Method Summary collapse

Methods included from ElementHelper

#grab, #mix

Instance Method Details

#alert_toastObject



30
31
32
33
34
35
36
# File 'app/components/practical/views/flash_messages_component.rb', line 30

def alert_toast
  render_toast(
    color_variant: :warning,
    data: helpers.flash[:alert],
    default_icon: icon_set.alert_icon
  )
end

#callObject



4
5
6
7
8
9
10
11
12
# File 'app/components/practical/views/flash_messages_component.rb', line 4

def call
  tag.aside(class: 'notification-messages wa-stack') do
    safe_join([
      alert_toast,
      notice_toast,
      success_toast,
    ])
  end
end

#notice_toastObject



22
23
24
25
26
27
28
# File 'app/components/practical/views/flash_messages_component.rb', line 22

def notice_toast
  render_toast(
    color_variant: :neutral,
    data: helpers.flash[:notice],
    default_icon: icon_set.info_icon
  )
end

#render_toast(color_variant:, data:, default_icon:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/components/practical/views/flash_messages_component.rb', line 38

def render_toast(color_variant:, data:, default_icon:)
  return nil if data.nil?

  case data
  when String
    message = data
    icon = default_icon
  when Hash
    data = data.with_indifferent_access
    message = data[:message]
    icon = data[:icon]
  end


  component = Practical::Views::ToastComponent.new(color_variant: color_variant)

  render component do |component|
    if icon.present? && icon.is_a?(Hash)
      icon = Practical::Views::IconComponent.new(**icon.to_h.symbolize_keys)
      component.with_icon do
        render icon
      end
    end

    message
  end
end

#success_toastObject



14
15
16
17
18
19
20
# File 'app/components/practical/views/flash_messages_component.rb', line 14

def success_toast
  render_toast(
    color_variant: :success,
    data: helpers.flash[:success],
    default_icon: icon_set.success_icon
  )
end