Allow comments to be entered while still in transit

This commit is contained in:
Daniel Friesel 2019-12-06 21:40:51 +01:00
parent 119f0a6b7d
commit 094c536eb4
4 changed files with 146 additions and 4 deletions

View file

@ -777,6 +777,25 @@ sub startup {
} }
); );
$self->helper(
'update_in_transit_comment' => sub {
my ( $self, $comment ) = @_;
my $uid = $self->current_user->{id};
my $status = $self->pg->db->select( 'in_transit', ['user_data'],
{ user_id => $uid } )->expand->hash;
if ( not $status ) {
return;
}
$status->{user_data}{comment} = $comment;
$self->pg->db->update(
'in_transit',
{ user_data => JSON->new->encode( $status->{user_data} ) },
{ user_id => $uid }
);
}
);
$self->helper( $self->helper(
'update_journey_part' => sub { 'update_journey_part' => sub {
my ( $self, $db, $journey_id, $key, $value ) = @_; my ( $self, $db, $journey_id, $key, $value ) = @_;
@ -2652,6 +2671,7 @@ sub startup {
route_after => \@route_after, route_after => \@route_after,
messages => $in_transit->{messages}, messages => $in_transit->{messages},
extra_data => $in_transit->{data}, extra_data => $in_transit->{data},
comment => $in_transit->{user_data}{comment},
}; };
my @parsed_messages; my @parsed_messages;
@ -2818,7 +2838,7 @@ sub startup {
order_by => { -desc => 'journey_id' }, order_by => { -desc => 'journey_id' },
limit => 1 limit => 1
} }
)->hash; )->expand->hash;
if ($latest) { if ($latest) {
my $ts = $latest->{checkout_ts}; my $ts = $latest->{checkout_ts};
@ -2843,6 +2863,7 @@ sub startup {
arr_ds100 => $latest->{arr_ds100}, arr_ds100 => $latest->{arr_ds100},
arr_name => $latest->{arr_name}, arr_name => $latest->{arr_name},
arr_platform => $latest->{arr_platform}, arr_platform => $latest->{arr_platform},
comment => $latest->{user_data}{comment},
}; };
} }
@ -3129,6 +3150,7 @@ sub startup {
$authed_r->get('/history/:year')->to('traveling#yearly_history'); $authed_r->get('/history/:year')->to('traveling#yearly_history');
$authed_r->get('/history/:year/:month')->to('traveling#monthly_history'); $authed_r->get('/history/:year/:month')->to('traveling#monthly_history');
$authed_r->get('/journey/add')->to('traveling#add_journey_form'); $authed_r->get('/journey/add')->to('traveling#add_journey_form');
$authed_r->get('/journey/comment')->to('traveling#comment_form');
$authed_r->get('/journey/:id')->to('traveling#journey_details'); $authed_r->get('/journey/:id')->to('traveling#journey_details');
$authed_r->get('/s/*station')->to('traveling#station'); $authed_r->get('/s/*station')->to('traveling#station');
$authed_r->get('/confirm_mail/:token')->to('account#confirm_mail'); $authed_r->get('/confirm_mail/:token')->to('account#confirm_mail');
@ -3137,6 +3159,7 @@ sub startup {
$authed_r->post('/account/insight')->to('account#insight'); $authed_r->post('/account/insight')->to('account#insight');
$authed_r->post('/history/map')->to('traveling#map_history'); $authed_r->post('/history/map')->to('traveling#map_history');
$authed_r->post('/journey/add')->to('traveling#add_journey_form'); $authed_r->post('/journey/add')->to('traveling#add_journey_form');
$authed_r->post('/journey/comment')->to('traveling#comment_form');
$authed_r->post('/journey/edit')->to('traveling#edit_journey'); $authed_r->post('/journey/edit')->to('traveling#edit_journey');
$authed_r->post('/journey/passenger_rights/*filename') $authed_r->post('/journey/passenger_rights/*filename')
->to('passengerrights#generate'); ->to('passengerrights#generate');

View file

@ -673,6 +673,51 @@ sub journey_details {
} }
sub comment_form {
my ($self) = @_;
my $dep_ts = $self->param('dep_ts');
my $status = $self->get_user_status;
if ( not $status->{checked_in} ) {
$self->render(
'edit_comment',
error => 'notfound',
journey => {}
);
}
elsif ( not $dep_ts ) {
$self->param( dep_ts => $status->{sched_departure}->epoch );
$self->param( comment => $status->{comment} );
$self->render(
'edit_comment',
error => undef,
journey => $status
);
}
elsif ( $self->validation->csrf_protect->has_error('csrf_token') ) {
$self->render(
'edit_comment',
error => undef,
journey => $status
);
}
elsif ( $dep_ts != $status->{sched_departure}->epoch ) {
# TODO find and update appropriate past journey (if it exists)
$self->param( comment => $status->{comment} );
$self->render(
'edit_comment',
error => undef,
journey => $status
);
}
else {
$self->app->log->debug("set comment");
$self->update_in_transit_comment( $self->param('comment') );
$self->redirect_to('/');
}
}
sub edit_journey { sub edit_journey {
my ($self) = @_; my ($self) = @_;
my $journey_id = $self->param('journey_id'); my $journey_id = $self->param('journey_id');

View file

@ -3,6 +3,9 @@
<div class="card-content"> <div class="card-content">
<i class="material-icons small right sync-failed-marker grey-text" style="display: none;">sync_problem</i> <i class="material-icons small right sync-failed-marker grey-text" style="display: none;">sync_problem</i>
<span class="card-title">Eingecheckt in <%= $journey->{train_type} %> <%= $journey->{train_no} %></span> <span class="card-title">Eingecheckt in <%= $journey->{train_type} %> <%= $journey->{train_no} %></span>
% if ($journey->{comment}) {
<p><%= $journey->{comment} %>.</p>
% }
<p> <p>
<div class="center-align countdown" <div class="center-align countdown"
data-journey="<%= $journey->{real_departure}->epoch %>;<%= <%= $journey->{real_arrival}->epoch %>" data-journey="<%= $journey->{real_departure}->epoch %>;<%= <%= $journey->{real_arrival}->epoch %>"
@ -216,9 +219,16 @@
% } % }
</div> </div>
<div class="card-action"> <div class="card-action">
<a class="action-undo blue-text" data-id="in_transit" style="margin-right: 0;"> % if ($journey->{arr_name}) {
<i class="material-icons left">undo</i> Rückgängig <a style="margin-right: 0;" href="/journey/comment">
</a> <i class="material-icons left">comment</i> Kommentar
</a>
% }
% else {
<a class="action-undo blue-text" data-id="in_transit" style="margin-right: 0;">
<i class="material-icons left">undo</i> Checkin Rückgängig
</a>
% }
% if (defined $journey->{arrival_countdown} and $journey->{arrival_countdown} <= 0) { % if (defined $journey->{arrival_countdown} and $journey->{arrival_countdown} <= 0) {
<a <a
class="action-checkout right" class="action-checkout right"
@ -276,6 +286,11 @@
auschecken</a>. auschecken</a>.
</p> </p>
</div> </div>
<div class="card-action">
<a class="action-undo blue-text" data-id="in_transit" style="margin-right: 0;">
<i class="material-icons left">undo</i> Checkin Rückgängig
</a>
</div>
</div> </div>
% } % }
</div> </div>

View file

@ -0,0 +1,59 @@
<h1>Zugfahrt kommentieren</h1>
% if ($error or not $journey->{checked_in}) {
<div class="row">
<div class="col s12">
<div class="card caution-color">
<div class="card-content white-text">
<span class="card-title">Fehler</span>
<p>Du bist gerade nicht eingecheckt. Vergangene Zugfahrten
kannst du über die Editierfunktion in der History
kommentieren.</p>
</div>
</div>
</div>
</div>
% }
% else {
%= form_for '/journey/comment' => (method => 'POST') => begin
%= csrf_field
%= hidden_field 'dep_ts' => param('dep_ts')
<div class="row">
<div class="col s12">
<p>
Eintrag zu
<b><%= $journey->{train_type} %> <%= $journey->{train_no} %></b>
von
<b><%= $journey->{dep_name} %></b>
nach
<b><%= $journey->{arr_name} // 'irgendwo' %></b>
am
<b><%= $journey->{sched_departure}->strftime('%d.%m.%Y') %></b>
</p>
% if (current_user()->{is_public} & 0x04) {
<p>
Der hier eingetragene Text ist als Teil deines Nutzerstatus
öffentlich sichtbar.
</p>
% }
</div>
</div>
<div class="row">
<div class="col s12">
%= text_field 'comment'
</div>
</div>
<div class="row">
<div class="col s6 m6 l6 center-align">
<a href="/" class="waves-effect waves-light btn">
Abbrechen
</a>
</div>
<div class="col s6 m6 l6 center-align">
<button class="btn waves-effect waves-light" type="submit" name="action" value="save">
Speichern
<i class="material-icons right">send</i>
</button>
</div>
</div>
%= end
% }