Browse Source

Updated interest buttons to use ajax instead of a round-trip post

development
Godwin 9 years ago
parent
commit
30954e7907
  1. 24
      app/assets/javascripts/main.js
  2. 7
      app/assets/stylesheets/_application.scss
  3. 6
      app/controllers/conferences_controller.rb
  4. 1
      app/models/workshop.rb
  5. 2
      app/views/workshops/_show.html.haml

24
app/assets/javascripts/main.js

@ -91,4 +91,28 @@
htmlNode.setAttribute('data-input', 'mouse'); htmlNode.setAttribute('data-input', 'mouse');
} }
}); });
Array.prototype.forEach.call(document.querySelectorAll('form.js-xhr'), function(form) {
if (form.addEventListener) {
form.addEventListener('submit', function(event) {
event.preventDefault();
form.classList.add('requesting');
var data = new FormData(form);
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
form.classList.remove('requesting');
if (request.status == 200) {
var response = JSON.parse(request.responseText);
for (var i = 0; i < response.length; i++) {
form.querySelector(response[i].selector).outerHTML = response[i].html;
}
}
}
}
request.open('POST', form.getAttribute('action'), true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.send(data);
}, false);
}
});
})(); })();

7
app/assets/stylesheets/_application.scss

@ -591,6 +591,13 @@ input {
} }
form { form {
@include _(transition, 'opacity 250ms ease-in-out, filter 250ms ease-in-out, -webkit-filter 250ms ease-in-out');
&.requesting {
opacity: 0.5;
-webkit-filter: blur(5px);
}
&.composition { &.composition {
textarea { textarea {
height: 16em; height: 16em;

6
app/controllers/conferences_controller.rb

@ -1084,6 +1084,12 @@ class ConferencesController < ApplicationController
WorkshopInterest.create(:workshop_id => workshop.id, :user_id => current_user.id) unless interested WorkshopInterest.create(:workshop_id => workshop.id, :user_id => current_user.id) unless interested
if request.xhr? if request.xhr?
interest = workshop.interested?(current_user) ? :remove_interest : :show_interest
css_class = workshop.interested?(current_user) ? :delete : :add
render json: [ {
selector: 'button[value="toggle_interest"]',
html: view_context.button_tag(interest, :value => :toggle_interest, :class => css_class)
} ]
else else
# go back to the workshop # go back to the workshop
redirect_to view_workshop_url(@this_conference.slug, workshop.id) redirect_to view_workshop_url(@this_conference.slug, workshop.id)

1
app/models/workshop.rb

@ -94,6 +94,7 @@ class Workshop < ActiveRecord::Base
end end
def can_translate?(user, lang) def can_translate?(user, lang)
return false unless user.present?
user.can_translate?(lang, locale) || (can_edit?(user) && lang.to_s != locale.to_s) user.can_translate?(lang, locale) || (can_edit?(user) && lang.to_s != locale.to_s)
end end

2
app/views/workshops/_show.html.haml

@ -8,7 +8,7 @@
- else - else
=_'articles.workshops.info.interested_count', "#{workshop.interested_count} people are interested in this workshop", :vars => {:count => workshop.interested_count} =_'articles.workshops.info.interested_count', "#{workshop.interested_count} people are interested in this workshop", :vars => {:count => workshop.interested_count}
- if preview.blank? && workshop.can_show_interest?(current_user) - if preview.blank? && workshop.can_show_interest?(current_user)
= form_tag toggle_workshop_interest_path(workshop.conference.slug, workshop.id) do = form_tag toggle_workshop_interest_path(workshop.conference.slug, workshop.id), class: 'js-xhr' do
= button_tag (workshop.interested?(current_user) ? :remove_interest : :show_interest), :value => :toggle_interest, :class => (workshop.interested?(current_user) ? :delete : :add) = button_tag (workshop.interested?(current_user) ? :remove_interest : :show_interest), :value => :toggle_interest, :class => (workshop.interested?(current_user) ? :delete : :add)
= richtext workshop.info = richtext workshop.info
- if preview.blank? && translations_available_for_editing - if preview.blank? && translations_available_for_editing

Loading…
Cancel
Save