Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-lookup some data for /report/new/ajax call. #3335

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Add options for user to set global notification preferences.
- Pop over mobile navigation menu. #3270
- Add support for the OS Maps API. #3328
- Speed up /report/new/ajax call. #3335
- Bugfixes:
- Fix non-JS form when all extra questions answered. #3248
- Improve display of disabled fields in iOS.
Expand Down
97 changes: 53 additions & 44 deletions perllib/FixMyStreet/App/Controller/Report/New.pm
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,28 @@ sub report_form_ajax : Path('ajax') : Args(0) {
$contribute_as->{body} = $ca_body if $ca_body;
}

my $lookups;
my $cobrand_body = $c->cobrand->can('body') && $c->cobrand->body;
foreach (@{$c->stash->{contacts}}) {
my $category = $_->category;
my $unresponsive = $c->stash->{unresponsive}{$category} || $c->stash->{unresponsive}{ALL};
next if $unresponsive->{$_->body_id};
my $body = $_->body;
push @{$lookups->{bodies}{$category}}, {
name => $body->name,
cobrand_name => $body->cobrand_name,
};
$lookups->{hints}{$category}->{title} ||= $_->get_extra_metadata('title_hint');
$lookups->{hints}{$category}->{detail} ||= $_->get_extra_metadata('detail_hint');
# Copy of Default's lookup using cobrand's body check (to save DB lookups)
$lookups->{anonymous_allowed}{$category} = $cobrand_body && $cobrand_body->id == $_->body_id && $_->get_extra_metadata('anonymous_allowed') ? 'button': '';
}

my %by_category;
foreach my $contact (@{$c->stash->{category_options}}) {
next unless $contact->category; # Ignore the 'Pick a category' line
my $cat = $c->stash->{category} = $contact->category;
my $body = $c->forward('by_category_ajax_data', [ 'all', $cat ]);
my $body = $c->forward('by_category_ajax_data', [ $cat, $lookups ]);
$by_category{$cat} = $body;
}

Expand All @@ -250,48 +267,47 @@ sub report_form_ajax : Path('ajax') : Args(0) {
unresponsive => $c->stash->{unresponsive}->{ALL} || '',
by_category => \%by_category,
};
$c->detach('send_json_response');
$c->forward('send_json_response');
}

sub category_extras_ajax : Path('category_extras') : Args(0) {
my ( $self, $c ) = @_;

$c->forward('initialize_report');
if ( ! $c->forward('determine_location') ) {
$c->stash->{json_response} = { error => _("Sorry, we could not find that location.") };
$c->detach('send_json_response');
$c->forward('report_form_ajax');
$c->forward('check_for_category', []);
my $category = $c->stash->{category};

my $json;
if ($category) {
$json = $c->stash->{json_response}->{by_category}->{$category};
} else {
$json = {
bodies => [ map { $_->name } values %{$c->stash->{bodies_to_list}} ],
};
}
$c->forward('setup_categories_and_bodies');
$c->forward('setup_report_extra_fields');

$c->forward('check_for_category', []);
$c->stash->{json_response} = $c->forward('by_category_ajax_data', [ 'one', $c->stash->{category} ]);
# unresponsive must return empty string if okay, as that's what mobile app checks
my $unresponsive = $c->stash->{unresponsive}->{$category} || $c->stash->{unresponsive}->{ALL} || '';
$json->{unresponsive} = $unresponsive;

$c->stash->{json_response} = $json;
$c->forward('send_json_response');
}

sub by_category_ajax_data : Private {
my ($self, $c, $type, $category) = @_;
my ($self, $c, $category, $lookups) = @_;

my @bodies;
my $bodies = [];
my $vars = {};
if ($category) {
$bodies = $c->forward('contacts_to_bodies', [ $category ]);
@bodies = @$bodies;
$vars->{list_of_names} = [ map { $_->cobrand_name } @bodies ];
} else {
@bodies = values %{$c->stash->{bodies_to_list}};
}
my $bodies = $lookups->{bodies}{$category};
$c->stash->{list_of_names} = [ map { $_->{cobrand_name} } @$bodies ];

my $non_public = $c->stash->{non_public_categories}->{$category};
my $anon_button = ($c->cobrand->allow_anonymous_reports($category) eq 'button');
my $anon_button = ($c->cobrand->allow_anonymous_reports($category, $lookups->{anonymous_allowed}) eq 'button');
my $body = {
bodies => [ map { $_->name } @bodies ],
$non_public ? ( non_public => JSON->true ) : (),
bodies => [ map { $_->{name} } @$bodies ],
$anon_button ? ( allow_anonymous => JSON->true ) : (),
};

if ( $c->stash->{category_extras}->{$category} && @{ $c->stash->{category_extras}->{$category} } >= 1 ) {
my $extras = $c->stash->{category_extras}->{$category} && @{ $c->stash->{category_extras}->{$category} } >= 1;
if ($extras) {
my $disable_form = $c->forward('disable_form_message');
$body->{disable_form} = $disable_form if %$disable_form;

Expand All @@ -301,38 +317,31 @@ sub by_category_ajax_data : Private {
} @{$c->stash->{category_extras}->{$c->stash->{category}}};
}

if (($c->stash->{category_extras}->{$category} && @{ $c->stash->{category_extras}->{$category} } >= 1) or
$c->stash->{unresponsive}->{$category} or $c->stash->{report_extra_fields}) {
$body->{category_extra} = $c->render_fragment('report/new/category_extras.html', $vars);
if ($extras or $c->stash->{unresponsive}->{$category} or $c->stash->{report_extra_fields}) {
$body->{category_extra} = $c->render_fragment('report/new/category_extras.html');
$body->{category_extra_json} = $c->forward('generate_category_extra_json');
$body->{extra_hidden} = 1 if $c->stash->{category_extras_hidden}->{$category};
}

my $unresponsive = $c->stash->{unresponsive}->{$category};
$unresponsive ||= $c->stash->{unresponsive}->{ALL} || '' if $type eq 'one';

# unresponsive must return empty string if okay, as that's what mobile app checks
# councils_text.html must be rendered if it differs from the default output,
# which currently means for unresponsive and non_public categories.
if ($type eq 'one' || ($type eq 'all' && $unresponsive)) {
if (my $unresponsive = $c->stash->{unresponsive}->{$category}) {
$body->{unresponsive} = $unresponsive;
# Check for no bodies here, because if there are any (say one
# unresponsive, one not), can use default display code for that.
if ($type eq 'all' && !@$bodies) {
$body->{councils_text} = $c->render_fragment( 'report/new/councils_text.html', $vars);
if (!@$bodies) {
$body->{councils_text} = $c->render_fragment( 'report/new/councils_text.html');
$body->{councils_text_private} = $c->render_fragment( 'report/new/councils_text_private.html');
}
}
if ($non_public) {
$body->{councils_text} = $c->render_fragment( 'report/new/councils_text.html', $vars);
if (my $non_public = $c->stash->{non_public_categories}->{$category}) {
$body->{non_public} = JSON->true;
$body->{councils_text} = $c->render_fragment( 'report/new/councils_text.html');
}

if ($category) {
my @contacts = grep { $_->category eq $category } @{$c->stash->{contacts}};
my $hints = form_field_hints(@contacts);
$body->{title_hint} = $hints->{title} if $hints->{title};
$body->{detail_hint} = $hints->{detail} if $hints->{detail};
}
my $hints = $lookups->{hints}{$category};
$body->{title_hint} = $hints->{title} if $hints->{title};
$body->{detail_hint} = $hints->{detail} if $hints->{detail};

return $body;
}
Expand Down
7 changes: 5 additions & 2 deletions perllib/FixMyStreet/Cobrand/Default.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,13 @@ pressed in the front end, rather than whenever a username is not provided.
=cut

sub allow_anonymous_reports {
my ($self, $category_name) = @_;
my ($self, $category_name, $lookup) = @_;

$category_name ||= $self->{c}->stash->{category};
if ( $category_name && $self->can('body') and $self->body ) {
return 0 unless $category_name;

return $lookup->{$category_name} if defined $lookup->{$category_name};
if ( $self->can('body') and $self->body ) {
my $category_rs = FixMyStreet::DB->resultset("Contact")->search({
body_id => $self->body->id,
category => $category_name
Expand Down
6 changes: 4 additions & 2 deletions templates/web/base/report/new/category_extras.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[% SET default_list = [] %][% FOR b IN bodies_to_list.values %][% default_list.push(b.cobrand_name) %][% END %]
[% DEFAULT list_of_names = default_list %]
[% IF NOT list_of_names %]
[% SET default_list = [] %][% FOR b IN bodies_to_list.values %][% default_list.push(b.cobrand_name) %][% END %]
[% SET list_of_names = default_list %]
[% END %]
[% category = mark_safe(category) %]

<div id="category_meta">
Expand Down
6 changes: 4 additions & 2 deletions templates/web/base/report/new/councils_text_all.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[% SET default_list = [] %][% FOR b IN bodies_to_list.values %][% default_list.push(b.cobrand_name) %][% END %]
[% DEFAULT list_of_names = default_list %]
[% IF NOT list_of_names %]
[% SET default_list = [] %][% FOR b IN bodies_to_list.values %][% default_list.push(b.cobrand_name) %][% END %]
[% SET list_of_names = default_list %]
[% END %]
[% category = mark_safe(category) %]

<p>
Expand Down