Skip to content

Commit

Permalink
Merge pull request #405 from tungleduyxyz/issue_329
Browse files Browse the repository at this point in the history
Issue 329 Add Bundles and Invoices pagination
  • Loading branch information
pierre authored Jun 11, 2024
2 parents 79f9619 + a5c6585 commit 7f1ec7f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
6 changes: 5 additions & 1 deletion app/controllers/kaui/bundles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class BundlesController < Kaui::EngineController
# rubocop:disable Lint/HashCompareByIdentity
def index
cached_options_for_klient = options_for_klient
@per_page = (params[:per_page] || 10).to_i
@page = (params[:page] || 1).to_i

fetch_bundles = promise { @account.bundles(cached_options_for_klient) }
fetch_bundles = promise { Kaui::Account.paginated_bundles(@account.account_id, (@page - 1) * @per_page, @per_page, 'NONE', cached_options_for_klient) }
fetch_bundle_tags = promise do
all_bundle_tags = @account.all_tags(:BUNDLE, false, 'NONE', cached_options_for_klient)
all_bundle_tags.each_with_object({}) do |entry, hsh|
Expand Down Expand Up @@ -35,6 +37,8 @@ def index
fetch_available_subscription_tags = promise { Kaui::TagDefinition.all_for_subscription(cached_options_for_klient) }

@bundles = wait(fetch_bundles)
@total_pages = (@bundles.pagination_max_nb_records.to_f / @per_page).ceil

@tags_per_bundle = wait(fetch_bundle_tags)
@tags_per_subscription = wait(fetch_subscription_tags)
@custom_fields_per_bundle = wait(fetch_bundle_fields)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kaui/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def pagination
if account.nil?
Kaui::Invoice.list_or_search(search_key, offset, limit, cached_options_for_klient)
else
account.invoices(cached_options_for_klient.merge({ params: { includeVoidedInvoices: true } })).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) }
Kaui::Account.paginated_invoices(search_key, offset, limit, 'NONE', cached_options_for_klient.merge({ params: { includeVoidedInvoices: true } })).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) }
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/helpers/kaui/subscription_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,9 @@ def subscription_future_cancelled?(sub, account)
def subscription_cancelled?(sub)
sub.present? and sub.billing_end_date.present?
end

def paging_button_class(num, current_page)
num == current_page ? 'btn btn-primary' : 'btn btn-custom'
end
end
end
34 changes: 34 additions & 0 deletions app/views/kaui/bundles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,38 @@
<% end %>
<% end %>

<div class="text-right" style="margin-top: 20px;">
<%= link_to 'First', account_bundles_path(page: 1), class: "btn btn-custom #{'disabled' if @page == 1}" %>
<%= link_to 'Previous', account_bundles_path(page: @page - 1), class: "btn btn-custom #{'disabled' if @page == 1}" %>
<% if @total_pages <= 10 %>
<% (1..@total_pages).each do |num| %>
<%= link_to num, account_bundles_path(page: num), class: paging_button_class(num, @page) %>
<% end %>
<% else %>
<%= link_to 1, account_bundles_path(page: 1), class: paging_button_class(1, @page) %>
<% if @page < 5 %>
<% (2..5).each do |num| %>
<%= link_to num, account_bundles_path(page: num), class: paging_button_class(num, @page) %>
<% end %>
<%= '...' %>
<% elsif @page > @total_pages - 4 %>
<%= '...' %>
<% (@total_pages-4..@total_pages-1).each do |num| %>
<%= link_to num, account_bundles_path(page: num), class: paging_button_class(num, @page) %>
<% end %>
<% else %>
<%= '...' %>
<% (@page-1..@page+1).each do |num| %>
<%= link_to num, account_bundles_path(page: num), class: paging_button_class(num, @page) %>
<% end %>
<%= '...' %>
<% end %>
<%= link_to @total_pages, account_bundles_path(page: @total_pages), class: paging_button_class(@total_pages, @page) %>
<% end %>
<%= link_to 'Next', account_bundles_path(page: @page + 1), class: "btn btn-custom #{'disabled' if @page == @total_pages}" %>
<%= link_to 'Last', account_bundles_path(page: @total_pages), class: "btn btn-custom #{'disabled' if @page == @total_pages}" %>
</div>

</div>
24 changes: 8 additions & 16 deletions app/views/kaui/invoices/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@
<%= javascript_tag do %>
$(document).ready(function() {
var table = $('#invoices-table').DataTable({
<% if @account.account_id.blank? %>
"dom": "<'row'r>t<'row'<'col-md-6'i><'col-md-6'p>>",
"pagingType": <% if @max_nb_records.nil? -%>"simple"<% else -%>"full_numbers"<% end -%>,
"language": {
<!-- See DefaultPaginationSqlDaoHelper.java -->
"info": <% if @max_nb_records.nil? -%>"Showing _START_ to _END_ of <%= number_with_delimiter(Kaui::EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD) -%>+ entries"<% else -%>"Showing _START_ to _END_ of _TOTAL_ entries"<% end -%>
},
"pageLength": <%= @limit %>,
"displayStart": <%= @offset %>,
"ajax": "<%= invoices_pagination_path(:ordering => @ordering, :format => :json) %>",
<% else %>
// No paging for per-account listings
"dom": "t",
"paging": false,
"ajax": "<%= invoices_pagination_path :format => :json %>",
<% end %>
"dom": "<'row'r>t<'row'<'col-md-6'i><'col-md-6'p>>",
"pagingType": <% if @max_nb_records.nil? -%>"simple"<% else -%>"full_numbers"<% end -%>,
"language": {
"info": <% if @max_nb_records.nil? -%>"Showing _START_ to _END_ of <%= number_with_delimiter(Kaui::EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD) -%>+ entries"<% else -%>"Showing _START_ to _END_ of _TOTAL_ entries"<% end -%>
},
"pageLength": <%= @limit %>,
"displayStart": <%= @offset %>,
"ajax": "<%= invoices_pagination_path(:ordering => @ordering, :format => :json) %>",
<% if @search_query.blank? %>
"ordering": false,
<% elsif !@ordering.blank? %>
Expand Down

0 comments on commit 7f1ec7f

Please sign in to comment.