summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Controller
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-07-23 20:18:10 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2023-07-23 20:18:10 +0200
commit38ad42b42cbf6da6254a2498a2fc553507cb3f18 (patch)
treeaafcec15240e9804555e66cfb4af1ebccff823c9 /lib/Travelynx/Controller
parentc0754f9e871e68019cd6109e8fc2649b515b9fc1 (diff)
convert checkout to promises (checkout_p)
Diffstat (limited to 'lib/Travelynx/Controller')
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm153
1 files changed, 103 insertions, 50 deletions
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm
index 48fa944..3e051a1 100755
--- a/lib/Travelynx/Controller/Traveling.pm
+++ b/lib/Travelynx/Controller/Traveling.pm
@@ -622,17 +622,26 @@ sub travel_action {
if ( $params->{action} eq 'checkin' ) {
my $status = $self->get_user_status;
+ my $promise;
+
if ( $status->{checked_in}
and $status->{arr_eva}
and $status->{arrival_countdown} <= 0 )
{
- $self->checkout( station => $status->{arr_eva} );
+ $promise = $self->checkout_p( station => $status->{arr_eva} );
+ }
+ else {
+ $promise = Mojo::Promise->resolve;
}
$self->render_later;
- $self->checkin_p(
- station => $params->{station},
- train_id => $params->{train}
+ $promise->then(
+ sub {
+ return $self->checkin_p(
+ station => $params->{station},
+ train_id => $params->{train}
+ );
+ }
)->then(
sub {
my $destination = $params->{dest};
@@ -648,17 +657,26 @@ sub travel_action {
# Silently ignore errors -- if they are permanent, the user will see
# them when selecting the destination manually.
- my ( $still_checked_in, undef ) = $self->checkout(
+ return $self->checkout_p(
station => $destination,
force => 0
);
- my $station_link = '/s/' . $destination;
- $self->render(
- json => {
- success => 1,
- redirect_to => $still_checked_in ? '/' : $station_link,
- },
- );
+ }
+ )->then(
+ sub {
+ my ( $still_checked_in, undef ) = @_;
+ if ( my $destination = $params->{dest} ) {
+ my $station_link = '/s/' . $destination;
+ $self->render(
+ json => {
+ success => 1,
+ redirect_to => $still_checked_in
+ ? '/'
+ : $station_link,
+ },
+ );
+ }
+ return;
}
)->catch(
sub {
@@ -673,28 +691,47 @@ sub travel_action {
)->wait;
}
elsif ( $params->{action} eq 'checkout' ) {
- my ( $still_checked_in, $error ) = $self->checkout(
+ $self->render_later;
+ $self->checkout_p(
station => $params->{station},
force => $params->{force}
- );
- my $station_link = '/s/' . $params->{station};
+ )->then(
+ sub {
+ my ( $still_checked_in, $error ) = @_;
+ my $station_link = '/s/' . $params->{station};
- if ($error) {
- $self->render(
- json => {
- success => 0,
- error => $error,
- },
- );
- }
- else {
- $self->render(
- json => {
- success => 1,
- redirect_to => $still_checked_in ? '/' : $station_link,
- },
- );
- }
+ if ($error) {
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ }
+ else {
+ $self->render(
+ json => {
+ success => 1,
+ redirect_to => $still_checked_in
+ ? '/'
+ : $station_link,
+ },
+ );
+ }
+ return;
+ }
+ )->catch(
+ sub {
+ my ($error) = @_;
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ return;
+ }
+ )->wait;
}
elsif ( $params->{action} eq 'undo' ) {
my $status = $self->get_user_status;
@@ -747,27 +784,43 @@ sub travel_action {
)->wait;
}
elsif ( $params->{action} eq 'cancelled_to' ) {
- my ( undef, $error ) = $self->checkout(
+ $self->render_later;
+ $self->checkout_p(
station => $params->{station},
force => 1
- );
-
- if ($error) {
- $self->render(
- json => {
- success => 0,
- error => $error,
- },
- );
- }
- else {
- $self->render(
- json => {
- success => 1,
- redirect_to => '/',
- },
- );
- }
+ )->then(
+ sub {
+ my ( undef, $error ) = @_;
+ if ($error) {
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ }
+ else {
+ $self->render(
+ json => {
+ success => 1,
+ redirect_to => '/',
+ },
+ );
+ }
+ return;
+ }
+ )->catch(
+ sub {
+ my ($error) = @_;
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ return;
+ }
+ )->wait;
}
elsif ( $params->{action} eq 'delete' ) {
my $error = $self->journeys->delete(