Skip to content

Commit

Permalink
Merge pull request #3831 from 3scale/THREESCALE-9797_applications_empty
Browse files Browse the repository at this point in the history
🦋 Implement empty state in Applications index
  • Loading branch information
josemigallas authored Jun 25, 2024
2 parents 532acac + 4585b7c commit cfc866f
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 304 deletions.
7 changes: 1 addition & 6 deletions app/controllers/api/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class Api::ApplicationsController < FrontendController
before_action :authorize_partners
before_action :find_plans
before_action :find_service
before_action :find_states, only: :index
before_action :find_applications, only: :index
before_action :find_states, only: :index # rubocop:disable Rails/LexicallyScopedActionFilter
before_action :find_buyer, only: :create
before_action :authorize_multiple_applications, only: :create
before_action :find_application_plan, only: :create
Expand Down Expand Up @@ -47,10 +46,6 @@ def accessible_plans
super.where(issuer: @service)
end

def accessible_not_bought_cinstances
super.where(service: @service)
end

def initialize_new_presenter
@presenter = Api::ApplicationsNewPresenter.new(provider: current_account,
service: @service,
Expand Down
8 changes: 1 addition & 7 deletions app/controllers/buyers/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class Buyers::ApplicationsController < FrontendController
before_action :authorize_partners
before_action :find_plans
before_action :find_buyer
before_action :find_states, only: :index
before_action :find_applications, only: :index
before_action :find_states, only: :index # rubocop:disable Rails/LexicallyScopedActionFilter
before_action :authorize_multiple_applications, only: :create
before_action :find_application_plan, only: :create
before_action :find_service, only: :create
Expand Down Expand Up @@ -36,15 +35,10 @@ def create

protected

def define_search_scope(opts = {})
super opts.reverse_merge(account: @account.id)
end

def initialize_new_presenter
@presenter = Buyers::ApplicationsNewPresenter.new(provider: current_account,
buyer: @account,
user: current_user,
cinstance: @cinstance)
end

end
1 change: 0 additions & 1 deletion app/controllers/provider/admin/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Provider::Admin::ApplicationsController < FrontendController
before_action :authorize_partners
before_action :find_plans
before_action :find_states, only: :index
before_action :find_applications, only: :index
before_action :find_buyer, only: :create
before_action :authorize_multiple_applications, only: :create
before_action :find_application_plan, only: :create
Expand Down
13 changes: 0 additions & 13 deletions app/helpers/applications_helper.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
# frozen_string_literal: true

module ApplicationsHelper
# TODO: need to refactor this method, there is no default return value
def create_application_link_href(account)
if account.bought_cinstances.size.zero?
new_admin_buyers_account_application_path(account)
elsif can?(:admin, :multiple_applications)
if can?(:see, :multiple_applications)
new_admin_buyers_account_application_path(account)
else
admin_upgrade_notice_path(:multiple_applications)
end
end
end

def last_traffic(cinstance)
return unless cinstance.first_daily_traffic_at?

Expand Down
25 changes: 4 additions & 21 deletions app/lib/applications_controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ def self.included(base)
def index
@presenter = ApplicationsIndexPresenter.new(application_plans: @application_plans,
accessible_services: accessible_services,
cinstances: @cinstances,
search: @search,
service: @service,
current_account: current_account,
provider: current_account,
accessible_plans: accessible_plans,
account: @account,
user: current_user)
buyer: @account,
user: current_user,
params: params)
end

protected
Expand All @@ -28,13 +27,6 @@ def initialize_cinstance
@cinstance.validate_human_edition!
end

def define_search_scope(opts = {})
@search = ThreeScale::Search.new(params[:search] || params)
@search.account = params['account_id'] if params.key?('account_id')
@search.plan_id = params['application_plan_id'] if params.key?('application_plan_id')
@search.merge!(opts)
end

def change_state(action, message, *rest)
@cinstance.public_send("#{action}!", *rest)

Expand All @@ -55,15 +47,6 @@ def find_states
@states = Cinstance.allowed_states.collect(&:to_s).sort
end

def find_applications
define_search_scope
@cinstances = accessible_not_bought_cinstances.scope_search(@search)
.order_by(params[:sort], params[:direction])
.preload(:service, user_account: %i[admin_user], plan: %i[pricing_rules])
.paginate(pagination_params)
.decorate
end

def find_cinstance
@cinstance = accessible_not_bought_cinstances.includes(plan: %i[service original plan_metrics pricing_rules])
.find(params[:id])
Expand Down
73 changes: 59 additions & 14 deletions app/presenters/applications_index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,41 @@ class ApplicationsIndexPresenter
include ApplicationsHelper

delegate :can?, to: :ability
delegate :total_entries, to: :applications

def initialize(application_plans:, accessible_services:, cinstances:, search:, service:, current_account:, accessible_plans:, account:, user:)
def initialize(application_plans:, accessible_services:, service:, provider:, accessible_plans:, buyer:, user:, params:)
@accessible_services = accessible_services
@application_plans = application_plans
@cinstances = cinstances
@search = search
@service = service
@current_account = current_account
@provider = provider
@accessible_plans = accessible_plans
@account = account
@buyer = buyer
@user = user
@ability = Ability.new(user)
@sorting_params = [params[:sort], params[:direction]]
@pagination_params = { page: params[:page] || 1, per_page: params[:per_page] || 20 }

@search = ThreeScale::Search.new(params[:search] || params)
@search.account = params['account_id'] if params.key?('account_id')
@search.plan_id = params['application_plan_id'] if params.key?('application_plan_id')
end

attr_reader :application_plans, :accessible_services, :cinstances, :search, :service, :current_account, :accessible_plans, :account, :ability
attr_reader :ability, :accessible_plans, :accessible_services, :application_plans, :buyer,
:pagination_params, :provider, :search, :service, :sorting_params, :user

def toolbar_props
show_application_plans = !application_plans.empty? && !current_account.master_on_premises?
service_column_visible = service.nil? && current_account.multiservice?
def toolbar_props # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
show_application_plans = !application_plans.empty? && !provider.master_on_premises?
service_column_visible = service.nil? && provider.multiservice?
new_application_path = if service.present?
new_admin_service_application_path(service)
elsif account.present?
create_application_link_href(account)
elsif buyer.present?
create_application_link_href
else
new_provider_admin_application_path
end

props = {
totalEntries: cinstances.total_entries,
totalEntries: total_entries,
actions: [{
label: 'Create an application',
href: new_application_path,
Expand All @@ -52,7 +59,7 @@ def toolbar_props
}]
}

if account.nil? # Probably use other variable like current_account
if buyer.nil?
props[:attributeFilters].append({ title: 'Account',
name: 'search[account_query]',
placeholder: 'Search by account',
Expand Down Expand Up @@ -97,7 +104,7 @@ def toolbar_props
end
end

if current_account.settings.finance.allowed?
if provider.settings.finance.allowed?
props[:attributeFilters].append({ name: 'search[plan_type]',
title: 'Plan type',
collection: [{ id: :free, title: 'Free' },
Expand All @@ -109,6 +116,44 @@ def toolbar_props
props
end

def raw_applications
return @raw_applications if @raw_applications.present?

raw = user.accessible_cinstances.not_bought_by(provider)
@raw_applications = if service.present?
raw.where(service: service)
elsif buyer.present?
raw.bought_by(buyer)
else
raw
end
end

def applications
@applications ||= raw_applications.scope_search(search)
.order_by(*sorting_params)
.preload(:service, user_account: %i[admin_user], plan: %i[pricing_rules])
.paginate(pagination_params)
.decorate
end

def empty_state?
raw_applications.empty?
end

# TODO: need to refactor this method, there is no default return value
def create_application_link_href
if buyer.bought_cinstances.size.zero?
new_admin_buyers_account_application_path(buyer)
elsif can?(:admin, :multiple_applications)
if can?(:see, :multiple_applications)
new_admin_buyers_account_application_path(buyer)
else
admin_upgrade_notice_path(:multiple_applications)
end
end
end

private

def states_for_filter
Expand Down
12 changes: 10 additions & 2 deletions app/views/api/applications/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
- content_for :page_header_title, "Applications on #{@service.name}"
- service_name = @service.name
- content_for :page_header_title, "Applications on #{service_name}"

- if current_user.accessible_services.empty?
= render 'shared/service_access'
- elsif presenter.empty_state?
= render partial: 'shared/empty_state', locals: { title: t('.empty_state.title'),
body: t('.empty_state.body', name: service_name),
icon: 'plus-circle',
primary: { title: t('.empty_state.primary'),
href: new_admin_service_application_path(@service) } }

- else
= render 'shared/applications/listing', { applications: @cinstances,
= render 'shared/applications/listing', { applications: presenter.applications,
application_plans: @application_plans,
plan: @plan,
account: @account,
Expand Down
11 changes: 9 additions & 2 deletions app/views/buyers/applications/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
- account_name = @account.name
- content_for :menu do
= render '/buyers/accounts/menu'

- content_for :page_header_title, "Applications for #{@account.name}"
- content_for :page_header_title, "Applications for #{account_name}"

- if current_user.accessible_services.empty?
= render 'shared/service_access'
- elsif presenter.empty_state?
= render partial: 'shared/empty_state', locals: { title: t('.empty_state.title'),
body: t('.empty_state.body'),
icon: 'plus-circle',
primary: { title: t('.empty_state.primary'),
href: presenter.create_application_link_href } }
- else
= render 'shared/applications/listing', { applications: @cinstances,
= render 'shared/applications/listing', { applications: presenter.applications,
application_plans: @application_plans,
plan: @plan,
account: @account,
Expand Down
8 changes: 7 additions & 1 deletion app/views/provider/admin/applications/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@

- if current_user.accessible_services.empty?
= render 'shared/service_access'
- elsif presenter.empty_state?
= render partial: 'shared/empty_state', locals: { title: t('.empty_state.title'),
body: t('.empty_state.body'),
icon: 'plus-circle',
primary: { title: t('.empty_state.primary'),
href: new_provider_admin_application_path } }
- else
= render 'shared/applications/listing', { applications: @cinstances,
= render 'shared/applications/listing', { applications: presenter.applications,
application_plans: @application_plans,
plan: @plan,
account: @account,
Expand Down
61 changes: 29 additions & 32 deletions app/views/shared/applications/_listing.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,38 @@ div class="pf-c-card"
| Paid?
= th_sortable('cinstances.created_at', 'Created on')
= th_sortable('cinstances.first_daily_traffic_at', 'Traffic on')
td

tbody role="rowgroup" class="cinstances"
- applications.each do |cinstance|
tr role="row" id=dom_id(cinstance)
td class="pf-c-table__check select" role="cell" id=cinstance.id
label
= bulk_select_one cinstance
td role="cell" data-label="Name"
= link_to cinstance.display_name, provider_admin_application_path(cinstance)
td role="cell" data-label="State" class="state"
=> cinstance.state
- if cinstance.trial?
= remaining_trial_days(cinstance)
- unless account
td role="cell" data-label="Account"
= link_to_buyer_or_deleted cinstance.account, :admin_buyers_account_path
- if service_column_visible
td role="cell" data-label="Service"
= link_to_service cinstance.service
- if show_application_plans
td role="cell" data-label="Plan" class="plan"
= link_to_plan_edit cinstance.plan
- if current_account.settings.finance.allowed?
td role="cell" data-label="Paid?" class="free_or_paid"
= plan_free_or_paid cinstance.plan
td role="cell" data-label="Created on"
= time_tag_with_title cinstance.created_at
td role="cell" data-label="Traffic on"
= last_traffic(cinstance)
td
- if applications.empty?
tr class="no_results"
td colspan=(service_column_visible ? 10 : 9)
| No applications
= render partial: 'shared/empty_search_state'
- else
- applications.each do |cinstance|
tr role="row" id=dom_id(cinstance)
td class="pf-c-table__check select" role="cell" id=cinstance.id
label
= bulk_select_one cinstance
td role="cell" data-label="Name"
= link_to cinstance.display_name, provider_admin_application_path(cinstance)
td role="cell" data-label="State" class="state"
=> cinstance.state
- if cinstance.trial?
= remaining_trial_days(cinstance)
- unless account
td role="cell" data-label="Account"
= link_to_buyer_or_deleted cinstance.account, :admin_buyers_account_path
- if service_column_visible
td role="cell" data-label="Service"
= link_to_service cinstance.service
- if show_application_plans
td role="cell" data-label="Plan" class="plan"
= link_to_plan_edit cinstance.plan
- if current_account.settings.finance.allowed?
td role="cell" data-label="Paid?" class="free_or_paid"
= plan_free_or_paid cinstance.plan
td role="cell" data-label="Created on"
= time_tag_with_title cinstance.created_at
td role="cell" data-label="Traffic on"
= last_traffic(cinstance)

= will_paginate applications

Expand Down
Loading

0 comments on commit cfc866f

Please sign in to comment.