##################################################
my %db;
-my ($form, $continue, $order, $details, $pay);
+my ($form, $continue, $order, $details, $pay, $display);
{
sub parse_html {
$order = parse_html 'order';
$details = parse_html 'details';
$pay = parse_html 'pay';
+ $display = parse_html 'display';
}
sub stringify_money { sprintf "£%.2f", $_[0] / 100 }
[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 {
mount '/order' => \&order_app;
mount '/details' => \&details_app;
mount '/pay' => \&pay_app;
+ mount '/display' => \&display_app;
}
}
--- /dev/null
+<!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&P</span></tbody>
+</table>
+<p>Stripe token: <span class="stripe_token">[None]</span></p>
+<hr>
+</div>