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

[frontend] Fix default subscription switch #4384

Merged
merged 1 commit into from
Jan 24, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/api/app/assets/javascripts/webui/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ $(document).on('click','.close-dialog', function() {
}
});

$(document).on('click', 'a[data-clear-checkboxes]', function(event) {
event.preventDefault();
$('input:checkbox').prop('checked', false);
});

// show/hide functionality for text
$(function() {
$('.show-hide').on('click', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/controllers/webui/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def update
private

def subscriptions_form
EventSubscription::Form.new(nil)
EventSubscription::Form.new
end
end
15 changes: 12 additions & 3 deletions src/api/app/controllers/webui/users/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ def index
@user = User.current
@groups_users = User.current.groups_users

@subscriptions_form = subscriptions_form
@subscriptions_form = subscriptions_form(default_form: params[:default_form])

respond_to do |format|
format.html
format.js
end
end

def update
Expand All @@ -24,7 +29,11 @@ def update

private

def subscriptions_form
EventSubscription::Form.new(User.current)
def subscriptions_form(options = {})
if options[:default_form]
EventSubscription::Form.new
else
EventSubscription::Form.new(User.current)
end
end
end
2 changes: 1 addition & 1 deletion src/api/app/models/event_subscription/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class EventSubscription
class Form
attr_reader :subscriber

def initialize(subscriber)
def initialize(subscriber = nil)
@subscriber = subscriber
end

Expand Down
5 changes: 3 additions & 2 deletions src/api/app/views/webui/users/subscriptions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

%h3 Events
%p.description Choose from which events you want to get an email
= render partial: 'webui/subscriptions/subscriptions_form'
#subscriptions-form
= render partial: 'webui/subscriptions/subscriptions_form'
%p
= submit_tag 'Update'
= link_to('Reset to default', '#', data: { 'clear-checkboxes': true })
= link_to('Reset to default', user_notifications_path(default_form: true), remote: true)
.grid_16.alpha.omega.box.box-shadow
%h2 RSS Feed
= form_tag(user_rss_token_path, id: 'generate_rss_token_form', method: :post) do
Expand Down
2 changes: 2 additions & 0 deletions src/api/app/views/webui/users/subscriptions/index.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('#subscriptions-form').replaceWith('<%= escape_javascript(render(partial: 'webui/subscriptions/subscriptions_form')) %>');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like quite a round-about way of doing implementing this feature, I mean, we have many steps to implement this feature:

  1. Click Reset link
  2. Send ajax requests to SubscriptionsController#index
  3. Return javascript to replace #subscriptions-form with the default subscriptions form html rendered in the javascript.

Why not just make the Reset link be a normal request to the controller? Also FYI reseting a user's subscriptions to the default can also be achieved by deleting all of the user's subscriptions. So it could be done with a destroy method on the controller i.e.

  def destroy
    subscriptions_form.reset_to_default
  end

And then in event_subscription/form.rb:

    def reset_to_default
      subscriber.event_subscriptions.delete_all
    end

Copy link
Member Author

@bgeuken bgeuken Jan 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanrolfe With the current approach a user can see the change and then decide if he wants it or not. Your proposal would directly apply the changes.

And regarding the roundtrip... we have that one way or the other. And without ajax we even have a full page reload.