openSUSE/open-build-service

View on GitHub
src/api/app/components/notification_action_bar_component.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class NotificationActionBarComponent < ApplicationComponent
  attr_accessor :state, :update_path, :counted_notifications

  def initialize(state:, update_path:, counted_notifications:)
    super

    @state = state
    @update_path = add_params(update_path)
    @counted_notifications = counted_notifications
  end

  def button_text(all: false)
    text = %w[all unread].include?(state) ? 'Read' : 'Unread'
    if all
      "Mark all as '#{text}'"
    else
      "Mark selected as '#{text}'"
    end
  end

  def disable_with_content(all: false)
    spinner = tag.i(class: 'fas fa-spinner fa-spin ms-2')
    button_text(all: all) + spinner
  end

  private

  def add_params(path)
    button = state == 'unread' ? 'read' : 'unread'
    return "#{path}&update_all=true&button=#{button}" if path.include?('?')

    "#{path}?update_all=true&button=#{button}"
  end
end