From d75351b30c38dc22e0071e18dc68be551eb628a7 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 5 Oct 2018 16:33:59 +0200 Subject: Use POST requests to modify data --- index.pl | 83 ++++++++++++++++++++++------------- public/static/js/travelynx-actions.js | 26 +++++++++++ templates/checkin.html.ep | 54 ----------------------- templates/checkout.html.ep | 31 ------------- templates/departures.html.ep | 8 ++-- templates/landingpage.html.ep | 2 +- templates/layouts/default.html.ep | 1 + 7 files changed, 86 insertions(+), 119 deletions(-) create mode 100644 public/static/js/travelynx-actions.js delete mode 100644 templates/checkin.html.ep delete mode 100644 templates/checkout.html.ep diff --git a/index.pl b/index.pl index dcc941c..6d00a1d 100644 --- a/index.pl +++ b/index.pl @@ -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'); - - my ( $train, $error ) = $self->checkin( $station, $train_id ); +post '/action' => sub { + my ($self) = @_; + my $params = $self->req->json; - if ($error) { - $self->render( - 'checkin', - error => $error, - train => undef - ); + if ( not exists $params->{action} ) { + $params = $self->req->params->to_hash; } - else { + + if ( not $params->{action} ) { $self->render( - 'checkin', - error => undef, - train => $train + json => {}, + status => 400, ); + return; } -}; -get '/a/checkout' => sub { - my ($self) = @_; - my $station = $self->param('station'); - my $force = $self->param('force'); + my $station = $params->{station}; - my $error = $self->checkout( $station, $force ); + if ( $params->{action} eq 'checkin' ) { - if ($error) { - $self->render( 'checkout', error => $error ); + my ( $train, $error ) + = $self->checkin( $params->{station}, $params->{train}, ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } } - else { - $self->redirect_to("/${station}"); + elsif ( $params->{action} eq 'checkout' ) { + my $error = $self->checkout( $params->{station}, $params->{force}, ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } } }; diff --git a/public/static/js/travelynx-actions.js b/public/static/js/travelynx-actions.js new file mode 100644 index 0000000..3fbbdea --- /dev/null +++ b/public/static/js/travelynx-actions.js @@ -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('
'); + $.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('
'); + $.post('/action', req, function(data) { + $(location).attr('href', '/' + req.station); + }); + }); +}); diff --git a/templates/checkin.html.ep b/templates/checkin.html.ep deleted file mode 100644 index 7e68a0b..0000000 --- a/templates/checkin.html.ep +++ /dev/null @@ -1,54 +0,0 @@ -% if ($error) { -
-
-
-
- I am Error -

<%= $error %>

-
-
- % if (param('station')) { - Zurück zu den Abfahrten - % } - % else { - Zur Hauptseite - % } -
-
-
-% } -% else { -
-
-
-
- Eingecheckt in <%= $train->line %> -

Abfahrt um <%= $train->sched_departure->strftime('%H:%M') %> - % if ($train->departure_delay) { - +<%= $train->departure_delay %> - % } -

-
-
-
-
-
-
- Weitere Route: -
-
-
-
- - - % for my $station ($train->route_post) { - - - - - % } - -
<%= $station %>hier auschecken
-
-
-% } diff --git a/templates/checkout.html.ep b/templates/checkout.html.ep deleted file mode 100644 index 1b5a4f2..0000000 --- a/templates/checkout.html.ep +++ /dev/null @@ -1,31 +0,0 @@ -% if ($error) { -
-
-
-
- I am Error -

<%= $error %>

-
-
- Ohne Echtzeitdaten auschecken - % if (param('station')) { - Zurück zu den Abfahrten - % } - % else { - Zur Hauptseite - % } -
-
-
-% } -% else { -
-
-
-
- Erfolgreich ausgecheckt -
-
-
-
-% } diff --git a/templates/departures.html.ep b/templates/departures.html.ep index bbe0149..bd11d15 100644 --- a/templates/departures.html.ep +++ b/templates/departures.html.ep @@ -9,7 +9,9 @@ ab <%= $status->{station_name} %>

% } @@ -28,12 +30,12 @@ % for my $result (@{$results}) { - + <%= $result->line %> - + <%= $result->destination %> diff --git a/templates/landingpage.html.ep b/templates/landingpage.html.ep index 0207118..a33d83a 100644 --- a/templates/landingpage.html.ep +++ b/templates/landingpage.html.ep @@ -13,7 +13,7 @@ % my $is_after = 0; % for my $station (@{$status->{route_after}}) { - <%= $station %> + <%= $station %> % } diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 5e0091c..d11bd2a 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -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' -- cgit v1.2.3