Refactored csv import flash errors

This commit is contained in:
Tom Bloor 2017-11-14 12:57:28 +00:00
parent b873e63d55
commit f0b1540f3e

View file

@ -69,7 +69,7 @@ sub post_add {
}; };
if ( defined $error ) { if ( defined $error ) {
$c->flash( error => $error, csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( $error );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -78,7 +78,7 @@ sub post_add {
my @required = grep {/^user$|^value$|^date$|^organisation$/} @csv_headers; my @required = grep {/^user$|^value$|^date$|^organisation$/} @csv_headers;
unless ( scalar( @required ) == 4 ) { unless ( scalar( @required ) == 4 ) {
$c->flash( error => 'Required columns not available', csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( 'Required columns not available' );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -86,7 +86,7 @@ sub post_add {
my $csv_output = $csv->getline_hr_all( $fh ); my $csv_output = $csv->getline_hr_all( $fh );
unless ( scalar( @$csv_output ) ) { unless ( scalar( @$csv_output ) ) {
$c->flash( error => "No data found", csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( "No data found" );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -94,7 +94,7 @@ sub post_add {
for my $data ( @$csv_output ) { for my $data ( @$csv_output ) {
for my $key ( qw/ user value organisation / ) { for my $key ( qw/ user value organisation / ) {
unless ( defined $data->{$key} ) { unless ( defined $data->{$key} ) {
$c->flash( error => "Undefined [$key] data found", csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( "Undefined [$key] data found" );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -103,7 +103,7 @@ sub post_add {
my $dtp = DateTime::Format::Strptime->new( pattern => $date_format ); my $dtp = DateTime::Format::Strptime->new( pattern => $date_format );
my $dt_obj = $dtp->parse_datetime($data->{date}); my $dt_obj = $dtp->parse_datetime($data->{date});
unless ( defined $dt_obj ) { unless ( defined $dt_obj ) {
$c->flash( error => "Undefined or incorrect format for [date] data found", csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( "Undefined or incorrect format for [date] data found" );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -126,7 +126,7 @@ sub post_add {
); );
unless ( defined $value_set ) { unless ( defined $value_set ) {
$c->flash( error => 'Error creating new Value Set', csv_data => $csv_data, date_format => $date_format ); $c->_csv_flash_error( 'Error creating new Value Set' );
$c->redirect_to( '/admin/import/add' ); $c->redirect_to( '/admin/import/add' );
return; return;
} }
@ -135,6 +135,17 @@ sub post_add {
$c->redirect_to( '/admin/import/' . $value_set->id ); $c->redirect_to( '/admin/import/' . $value_set->id );
} }
sub _csv_flash_error {
my ( $c, $error ) = @_;
$error //= "An error occurred";
$c->flash(
error => $error,
csv_data => $c->param('csv'),
date_format => $c->param('date_format'),
);
}
sub get_user { sub get_user {
my $c = shift; my $c = shift;
my $set_id = $c->param('set_id'); my $set_id = $c->param('set_id');
@ -182,6 +193,8 @@ sub get_user {
); );
} }
sub get_org { sub get_org {
my $c = shift; my $c = shift;