Use POST requests to modify data

This commit is contained in:
Daniel Friesel 2018-10-05 16:33:59 +02:00
parent 3198689286
commit d75351b30c
7 changed files with 88 additions and 121 deletions

View file

@ -181,6 +181,10 @@ app->attr(
sub epoch_to_dt {
my ($epoch) = @_;
# Bugs (and user errors) may lead to undefined timestamps. Set them to
# 1970-01-01 to avoid crashing and show obviously wrong data instead.
$epoch //= 0;
return DateTime->from_epoch(
epoch => $epoch,
time_zone => 'Europe/Berlin'
@ -479,10 +483,7 @@ helper 'get_user_status' => sub {
if ( @{$rows} ) {
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $ts = DateTime->from_epoch(
epoch => $rows->[0][1],
time_zone => 'Europe/Berlin'
);
my $ts = epoch_to_dt( $rows->[0][1] );
my $checkin_station_name = $rows->[0][3];
my @route = split( qr{[|]}, $rows->[0][8] // q{} );
my @route_after;
@ -529,41 +530,63 @@ get '/' => sub {
$self->render('landingpage');
};
get '/a/checkin' => sub {
my ($self) = @_;
my $station = $self->param('station');
my $train_id = $self->param('train');
post '/action' => sub {
my ($self) = @_;
my $params = $self->req->json;
my ( $train, $error ) = $self->checkin( $station, $train_id );
if ( not exists $params->{action} ) {
$params = $self->req->params->to_hash;
}
if ($error) {
if ( not $params->{action} ) {
$self->render(
'checkin',
error => $error,
train => undef
json => {},
status => 400,
);
return;
}
else {
$self->render(
'checkin',
error => undef,
train => $train
);
my $station = $params->{station};
if ( $params->{action} eq 'checkin' ) {
my ( $train, $error )
= $self->checkin( $params->{station}, $params->{train}, );
if ($error) {
$self->render(
json => {
success => 0,
error => $error,
},
);
}
else {
$self->render(
json => {
success => 1,
},
);
}
}
};
elsif ( $params->{action} eq 'checkout' ) {
my $error = $self->checkout( $params->{station}, $params->{force}, );
get '/a/checkout' => sub {
my ($self) = @_;
my $station = $self->param('station');
my $force = $self->param('force');
my $error = $self->checkout( $station, $force );
if ($error) {
$self->render( 'checkout', error => $error );
}
else {
$self->redirect_to("/${station}");
if ($error) {
$self->render(
json => {
success => 0,
error => $error,
},
);
}
else {
$self->render(
json => {
success => 1,
},
);
}
}
};

View file

@ -0,0 +1,26 @@
$(document).ready(function() {
$('.action-checkin').click(function() {
var link = $(this);
req = {
action: 'checkin',
station: link.data('station'),
train: link.data('train'),
};
link.replaceWith('<div class="progress"><div class="indeterminate"></div></div>');
$.post('/action', req, function(data) {
$(location).attr('href', '/');
});
});
$('.action-checkout').click(function() {
var link = $(this);
req = {
action: 'checkout',
station: link.data('station'),
force: link.data('force'),
};
link.replaceWith('<div class="progress"><div class="indeterminate"></div></div>');
$.post('/action', req, function(data) {
$(location).attr('href', '/' + req.station);
});
});
});

View file

@ -1,54 +0,0 @@
% if ($error) {
<div class="row">
<div class="col s12 m6">
<div class="card red darken-4">
<div class="card-content white-text">
<span class="card-title">I am Error</span>
<p><%= $error %></p>
</div>
<div class="card-action">
% if (param('station')) {
<a href="/<%= param('station') %>">Zurück zu den Abfahrten</a>
% }
% else {
<a href="/">Zur Hauptseite</a>
% }
</div>
</div>
</div>
% }
% else {
<div class="row">
<div class="col s12">
<div class="card green darken-4">
<div class="card-content white-text">
<span class="card-title">Eingecheckt in <%= $train->line %></span>
<p>Abfahrt um <%= $train->sched_departure->strftime('%H:%M') %>
% if ($train->departure_delay) {
+<%= $train->departure_delay %>
% }
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col s12">
Weitere Route:
</div>
</div>
<div class="row">
<div class="col s12">
<table>
<tbody>
% for my $station ($train->route_post) {
<tr>
<td><%= $station %></td>
<td><a href="/a/checkout?station=<%= $station %>">hier auschecken</a></td>
</tr>
% }
</tbody>
</table>
</div>
</div>
% }

View file

@ -1,31 +0,0 @@
% if ($error) {
<div class="row">
<div class="col s12 m6">
<div class="card red darken-4">
<div class="card-content white-text">
<span class="card-title">I am Error</span>
<p><%= $error %></p>
</div>
<div class="card-action">
<a href="/a/checkout?station=<%= param('station') %>&amp;force=1">Ohne Echtzeitdaten auschecken</a>
% if (param('station')) {
<a href="/<%= param('station') %>">Zurück zu den Abfahrten</a>
% }
% else {
<a href="/">Zur Hauptseite</a>
% }
</div>
</div>
</div>
% }
% else {
<div class="row">
<div class="col s12">
<div class="card green darken-4">
<div class="card-content white-text">
<span class="card-title">Erfolgreich ausgecheckt</span>
</div>
</div>
</div>
</div>
% }

View file

@ -9,7 +9,9 @@
ab <%= $status->{station_name} %></p>
</div>
<div class="card-action">
<a href="/a/checkout?station=<%= $ds100 %>">Hier auschecken</a>
<a class="action-checkout" data-station="<%= $ds100 %>">
Hier auschecken
</a>
</div>
</div>
% }
@ -28,12 +30,12 @@
% for my $result (@{$results}) {
<tr>
<td>
<a href="/a/checkin?station=<%= $ds100 %>&amp;train=<%= $result->train_id %>" title="Check In">
<a class="action-checkin" data-station="<%= $ds100 %>" data-train="<%= $result->train_id %>">
<%= $result->line %>
</a>
</td>
<td>
<a href="/a/checkin?station=<%= $ds100 %>&amp;train=<%= $result->train_id %>" title="Check In">
<a class="action-checkin" data-station="<%= $ds100 %>" data-train="<%= $result->train_id %>">
<%= $result->destination %>
</a>
</td>

View file

@ -13,7 +13,7 @@
<tbody>
% my $is_after = 0;
% for my $station (@{$status->{route_after}}) {
<tr><td><a href="/a/checkout?station=<%= $station %>"><%= $station %></a></td></tr>
<tr><td><a class="action-checkout" data-station="<%= $station %>"><%= $station %></a></td></tr>
% }
</tbody>
</table>

View file

@ -9,6 +9,7 @@
%= stylesheet '/static/css/local.css'
%= javascript '/static/js/jquery-2.2.4.min.js'
%= javascript '/static/js/materialize.min.js'
%= javascript '/static/js/travelynx-actions.js'
</head>
<body>