From 69d4d80d43da02572ae56fe398bcb86ad6849dbf Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 28 May 2016 21:32:12 +0100 Subject: [PATCH] Add a display of the latest orders --- MANIFEST | 1 + lib/App/Web/Oof.pm | 31 ++++++++++++++++++++++++++++++- tmpl/display.html | 17 +++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tmpl/display.html diff --git a/MANIFEST b/MANIFEST index 7151d87..182d9ba 100644 --- a/MANIFEST +++ b/MANIFEST @@ -18,3 +18,4 @@ tmpl/footer.html tmpl/form.html tmpl/order.html tmpl/pay.html +tmpl/display.html diff --git a/lib/App/Web/Oof.pm b/lib/App/Web/Oof.pm index 69fcac3..1294c16 100644 --- a/lib/App/Web/Oof.pm +++ b/lib/App/Web/Oof.pm @@ -43,7 +43,7 @@ my $postage_per_item = $ENV{OOF_POSTAGE_PER_ITEM} // 50; ################################################## my %db; -my ($form, $continue, $order, $details, $pay); +my ($form, $continue, $order, $details, $pay, $display); { sub parse_html { @@ -58,6 +58,7 @@ my ($form, $continue, $order, $details, $pay); $order = parse_html 'order'; $details = parse_html 'details'; $pay = parse_html 'pay'; + $display = parse_html 'display'; } sub stringify_money { sprintf "£%.2f", $_[0] / 100 } @@ -293,6 +294,33 @@ sub pay_app { [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]]; } +sub display_table_row { + my ($data, $tr) = @_; + $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/; + $tr->fclass('freepost')->detach unless $data->{freepost}; + $tr->fclass('price')->replace_content(stringify_money $data->{subtotal}); + $tr->fclass('title')->attr('data-product', $data->{product}); +} + +sub display_order { + my ($data, $div) = @_; + my @products = @{decode_json $data->{products}}; + $div->find('table')->iter3(\@products, \&display_table_row); + $div->fclass('name')->replace_content($data->{first_name} . ' ' . $data->{last_name}); + $div->fclass('stripe_token')->replace_content($data->{stripe_token}) if $data->{stripe_token}; +} + +sub display_app { + my ($env) = @_; + $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); + my $req = Plack::Request->new($env); + my $n = int ($req->param('n') // 10); + my @orders = $db{$$}->query("SELECT * FROM orders ORDER BY date DESC LIMIT $n")->hashes; + my $tree = $display->clone; + $tree->fclass('order')->iter3(\@orders, \&display_order); + [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]; +} + sub app { my $footer = read_file 'tmpl/footer.html'; builder { @@ -310,6 +338,7 @@ sub app { mount '/order' => \&order_app; mount '/details' => \&details_app; mount '/pay' => \&pay_app; + mount '/display' => \&display_app; } } diff --git a/tmpl/display.html b/tmpl/display.html new file mode 100644 index 0000000..12323a2 --- /dev/null +++ b/tmpl/display.html @@ -0,0 +1,17 @@ + + + +Recent orders | ledparts4you + +

ledparts4you

+
Issues/Questions? Contact us at orders@ledparts4you.uk.to
+ +
+From + + + +
ItemQuantityPrice


FREE P&P
+

Stripe token: [None]

+
+
-- 2.30.2