Add a display of the latest orders
authorMarius Gavrilescu <marius@ieval.ro>
Sat, 28 May 2016 20:32:12 +0000 (21:32 +0100)
committerMarius Gavrilescu <marius@ieval.ro>
Sat, 28 May 2016 20:32:12 +0000 (21:32 +0100)
MANIFEST
lib/App/Web/Oof.pm
tmpl/display.html [new file with mode: 0644]

index 7151d876659f9df38fa703dad939a8aa29d698fb..182d9baf92676599d5b87889a3666452949f7684 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -18,3 +18,4 @@ tmpl/footer.html
 tmpl/form.html
 tmpl/order.html
 tmpl/pay.html
+tmpl/display.html
index 69fcac39eba64accbb7c87408512f258377df7a3..1294c168a9687da7c5155054d7b3a992eb165881 100644 (file)
@@ -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 (file)
index 0000000..12323a2
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<link rel="stylesheet" href="/static/style.css">
+<title>Recent orders | ledparts4you</title>
+
+<h1 id="title">ledparts4you</h1>
+<div id="subtitle">Issues/Questions? Contact us at <a href="mailto:orders@ledparts4you.uk.to">orders@ledparts4you.uk.to</a></div>
+
+<div class="order">
+From <span class="name"></span>
+<table>
+<thead><tr><th>Item<th>Quantity<th>Price</thead>
+<tbody><tr><td class="item"><a href="#" class="title"></a><br><span class="subtitle"></span><td class="quantity"><td><span class="price"></span><span class="freepost"><br>FREE P&amp;P</span></tbody>
+</table>
+<p>Stripe token: <span class="stripe_token">[None]</span></p>
+<hr>
+</div>
This page took 0.013866 seconds and 4 git commands to generate.