Skip to content

Commit

Permalink
commit changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Sep 14, 2024
1 parent 0d7fbaa commit ddc8942
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 106 deletions.
100 changes: 56 additions & 44 deletions lib/outboxer/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ class Web < Sinatra::Base
time_zone: params[:time_zone])

@normalised_query_params = normalise_query_params(
status: params[:status],
sort: params[:sort],
order: params[:order],
page: params[:page],
per_page: params[:per_page],
time_zone: params[:time_zone])
status: @denormalised_query_params[:status],
sort: @denormalised_query_params[:sort],
order: @denormalised_query_params[:order],
page: @denormalised_query_params[:page],
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone])

@normalised_query_string = normalise_query_string(
status: params[:status],
sort: params[:sort],
order: params[:order],
page: params[:page],
per_page: params[:per_page],
time_zone: params[:time_zone])
status: @denormalised_query_params[:status],
sort: @denormalised_query_params[:sort],
order: @denormalised_query_params[:order],
page: @denormalised_query_params[:page],
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone])
end

helpers do
Expand Down Expand Up @@ -95,12 +95,12 @@ def outboxer_path(path)
end

HEADERS = {
'id' => 'Id',
'status' => 'Status',
'messageable' => 'Messageable',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'updated_by' => 'Updated By',
id: 'Id',
status: 'Status',
messageable: 'Messageable',
created_at: 'Created At',
updated_at: 'Updated At',
updated_by: 'Updated By',
}

def generate_pagination(current_page:, total_pages:, params:)
Expand All @@ -112,17 +112,25 @@ def generate_pagination(current_page:, total_pages:, params:)
previous_page = {
text: 'Previous',
href: outboxer_path("/messages" + normalise_query_string(
status: params[:status], sort: params[:sort], order: params[:order],
page: current_page - 1, per_page: params[:per_page], time_zone: params[:time_zone]))
status: @denormalised_query_params[:status],
sort: @denormalised_query_params[:sort],
order: @denormalised_query_params[:order],
page: current_page - 1,
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone]))
}
end

pages = (([current_page - 4, 1].max)..([current_page + 4, total_pages].min)).map do |page|
{
text: page,
href: outboxer_path("/messages" + normalise_query_string(
status: params[:status], sort: params[:sort], order: params[:order], page: page,
per_page: params[:per_page], time_zone: params[:time_zone])),
status: @denormalised_query_params[:status],
sort: @denormalised_query_params[:sort],
order: @denormalised_query_params[:order],
page: page,
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone])),
is_active: current_page == page
}
end
Expand All @@ -131,8 +139,12 @@ def generate_pagination(current_page:, total_pages:, params:)
next_page = {
text: 'Next',
href: outboxer_path("/messages" + normalise_query_string(
status: params[:status], sort: params[:sort], order: params[:order],
page: current_page + 1, per_page: params[:per_page], time_zone: params[:time_zone]))
status: @denormalised_query_params[:status],
sort: @denormalised_query_params[:sort],
order: @denormalised_query_params[:order],
page: current_page + 1,
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone]))
}
end

Expand All @@ -141,31 +153,31 @@ def generate_pagination(current_page:, total_pages:, params:)

def generate_headers(params:)
HEADERS.map do |header_key, header_text|
if params[:sort] == header_key
if params[:order] == 'asc'
if @denormalised_query_params[:sort] == header_key
if @denormalised_query_params[:order] == 'asc'
{
text: header_text,
icon_class: 'bi bi-arrow-up',
href: outboxer_path('/messages' + normalise_query_string(
status: params[:status],
order: 'desc',
status: @denormalised_query_params[:status],
order: :desc,
sort: header_key,
page: 1,
per_page: params[:per_page],
time_zone: params[:time_zone]
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone]
))
}
else
{
text: header_text,
icon_class: 'bi bi-arrow-down',
href: outboxer_path('/messages' + normalise_query_string(
status: params[:status],
order: 'asc',
status: @denormalised_query_params[:status],
order: :asc,
sort: header_key,
page: 1,
per_page: params[:per_page],
time_zone: params[:time_zone]
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone]
))
}
end
Expand All @@ -174,12 +186,12 @@ def generate_headers(params:)
text: header_text,
icon_class: '',
href: outboxer_path('/messages' + normalise_query_string(
status: params[:status],
order: 'asc',
status: @denormalised_query_params[:status],
order: :asc,
sort: header_key,
page: 1,
per_page: params[:per_page],
time_zone: params[:time_zone]
per_page: @denormalised_query_params[:per_page],
time_zone: @denormalised_query_params[:time_zone]
))
}
end
Expand Down Expand Up @@ -209,12 +221,12 @@ def normalise_query_params(status: Messages::LIST_STATUS_DEFAULT,
per_page: Messages::LIST_PER_PAGE_DEFAULT,
time_zone: Messages::LIST_TIME_ZONE_DEFAULT)
{
status: status&.to_sym == Messages::LIST_STATUS_DEFAULT ? nil : status,
sort: sort&.to_sym == Messages::LIST_SORT_DEFAULT ? nil : sort,
order: order&.to_sym == Messages::LIST_ORDER_DEFAULT ? nil : order,
page: page&.to_i == Messages::LIST_PAGE_DEFAULT ? nil : page,
per_page: per_page&.to_i == Messages::LIST_PER_PAGE_DEFAULT ? nil : per_page,
time_zone: time_zone&.to_s == Messages::LIST_TIME_ZONE_DEFAULT ? nil : time_zone
status: status == Messages::LIST_STATUS_DEFAULT ? nil : status,
sort: sort == Messages::LIST_SORT_DEFAULT ? nil : sort,
order: order == Messages::LIST_ORDER_DEFAULT ? nil : order,
page: page.to_i == Messages::LIST_PAGE_DEFAULT ? nil : page,
per_page: per_page.to_i == Messages::LIST_PER_PAGE_DEFAULT ? nil : per_page,
time_zone: time_zone.to_s == Messages::LIST_TIME_ZONE_DEFAULT ? nil : time_zone
}.compact
end

Expand Down
62 changes: 0 additions & 62 deletions lib/outboxer/web/views/messages/index.erb
Original file line number Diff line number Diff line change
@@ -1,62 +0,0 @@
<!-- Table -->
<div class="container table-container">
<table class="table table-hover">
<thead class="table-light">
<tr>
<%
headers = {
'Id' => 'id',
'Messageable Type' => 'messageable_type',
'Messageable Id' => 'messageable_id',
'Status' => 'status',
'Created At' => 'created_at',
'Updated At' => 'updated_at',
'Updated By' => 'updated_by',
}
headers.each do |display, col|
%>
<th>
<a href="?page=<%= page %>&limit=<%= limit %>&sort=<%= col %>&order=<%= sort.to_s == col && order == :asc ? 'desc' : 'asc' %>">
<%= display %>
<% if sort.to_s == col %>
<%= order == :asc ? '▲' : '▼' %>
<% end %>
</a>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% messages.each do |message| %>
<tr class="clickable-row" data-href="/messages/<%= message.id %>">
<td><%= message.id %></td>
<td><%= message.messageable_type %></td>
<td><%= message.messageable_id %></td>
<td><%= message.status %></td>
<td><%= message.created_at %></td>
<td><%= message.updated_at %></td>
<td><%= message.updated_by %></td>
</tr>
<% end %>
</tbody>
</table>
</div>

<!-- Pagination Controls -->
<div class="container">
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<li class="page-item <%= 'disabled' unless messages.current_page > 1 %>">
<a class="page-link" href="?page=<%= messages.prev_page %>&limit=<%= limit %>&order=<%= order %>&sort=<%= sort %>" tabindex="-1" aria-disabled="true">&lt;&lt;</a>
</li>
<% messages.total_pages.times do |i| %>
<li class="page-item <%= 'active' if messages.current_page == i + 1 %>">
<a class="page-link" href="?page=<%= i + 1 %>&limit=<%= limit %>&order=<%= order %>&sort=<%= sort %>"><%= i + 1 %></a>
</li>
<% end %>
<li class="page-item <%= 'disabled' unless messages.current_page < messages.total_pages %>">
<a class="page-link" href="?page=<%= messages.next_page %>&limit=<%= limit %>&order=<%= order %>&sort=<%= sort %>" >&gt;&gt;</a>
</li>
</ul>
</nav>
</div>

0 comments on commit ddc8942

Please sign in to comment.