From c3519fa4df63b513e55bb8a2f1c09a05cc6a2eee Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Thu, 28 Sep 2017 16:39:03 +0100 Subject: [PATCH] Improved Feedback list page --- lib/Pear/LocalLoop.pm | 1 + .../LocalLoop/Controller/Admin/Feedback.pm | 11 ++++++-- lib/Pear/LocalLoop/Plugin/Datetime.pm | 11 ++++++++ lib/Pear/LocalLoop/Plugin/TemplateHelpers.pm | 18 ++++++++++++ templates/admin/feedback/index.html.ep | 28 +++++++++++++++---- 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 lib/Pear/LocalLoop/Plugin/TemplateHelpers.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 8bdb811..a2f3ce1 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -36,6 +36,7 @@ sub startup { $self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } ); $self->plugin('Pear::LocalLoop::Plugin::Validators'); $self->plugin('Pear::LocalLoop::Plugin::Datetime'); + $self->plugin('Pear::LocalLoop::Plugin::TemplateHelpers'); $self->plugin('Authentication' => { 'load_user' => sub { diff --git a/lib/Pear/LocalLoop/Controller/Admin/Feedback.pm b/lib/Pear/LocalLoop/Controller/Admin/Feedback.pm index f9063a4..753539c 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Feedback.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Feedback.pm @@ -9,8 +9,15 @@ has result_set => sub { sub index { my $c = shift; - my $feedback_rs = $c->result_set; - $c->stash( feedbacks => [ $feedback_rs->all ] ); + my $feedback_rs = $c->result_set->search( + undef, + { + page => $c->param('page') || 1, + rows => 12, + order_by => { -desc => 'submitted_at' }, + }, + ); + $c->stash( feedback_rs => $feedback_rs ); } sub read { diff --git a/lib/Pear/LocalLoop/Plugin/Datetime.pm b/lib/Pear/LocalLoop/Plugin/Datetime.pm index c598709..1f79e45 100644 --- a/lib/Pear/LocalLoop/Plugin/Datetime.pm +++ b/lib/Pear/LocalLoop/Plugin/Datetime.pm @@ -6,6 +6,17 @@ use DateTime::Format::Strptime; sub register { my ( $plugin, $app, $conf ) = @_; + $app->helper( human_datetime_parser => sub { + return DateTime::Format::Strptime->new( pattern => '%x %X' ); + }); + + $app->helper( format_human_datetime => sub { + my ( $c, $datetime_obj ) = @_; + return $c->human_datetime_parser->format_datetime( + $datetime_obj, + ); + }); + $app->helper( iso_datetime_parser => sub { return DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%S.%3N%z' ); }); diff --git a/lib/Pear/LocalLoop/Plugin/TemplateHelpers.pm b/lib/Pear/LocalLoop/Plugin/TemplateHelpers.pm new file mode 100644 index 0000000..87c04d4 --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/TemplateHelpers.pm @@ -0,0 +1,18 @@ +package Pear::LocalLoop::Plugin::TemplateHelpers; +use Mojo::Base 'Mojolicious::Plugin'; + +sub register { + my ( $plugin, $app, $conf ) = @_; + + $app->helper( truncate_text => sub { + my ( $c, $string, $length ) = @_; + if ( length $string < $length ) { + return $string; + } else { + return substr( $string, 0, $length - 3 ) . '...'; + } + }); + +} + +1; diff --git a/templates/admin/feedback/index.html.ep b/templates/admin/feedback/index.html.ep index c25a176..81cf9f9 100644 --- a/templates/admin/feedback/index.html.ep +++ b/templates/admin/feedback/index.html.ep @@ -11,12 +11,28 @@ Success! <%= $success %> % } -
- % for my $feedback (@$feedbacks) { - -
- <%= $feedback->user->email %> <%= $feedback->submitted_at %> + +
+
+ %= bootstrap_pagination( $c->param('page') || 1, $feedback_rs->pager->last_page, { class => 'justify-content-center' } ); +
+