From 30954e7907ca35b71f9dfd7b1eb5d580bda82d14 Mon Sep 17 00:00:00 2001 From: Godwin Date: Sat, 11 Jun 2016 16:45:22 -0700 Subject: [PATCH] Updated interest buttons to use ajax instead of a round-trip post --- app/assets/javascripts/main.js | 24 +++++++++++++++++++++++ app/assets/stylesheets/_application.scss | 7 +++++++ app/controllers/conferences_controller.rb | 6 ++++++ app/models/workshop.rb | 1 + app/views/workshops/_show.html.haml | 2 +- 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index c1071a6..d9cd677 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -91,4 +91,28 @@ 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); + } + }); })(); diff --git a/app/assets/stylesheets/_application.scss b/app/assets/stylesheets/_application.scss index 42fcf87..0df4d40 100644 --- a/app/assets/stylesheets/_application.scss +++ b/app/assets/stylesheets/_application.scss @@ -591,6 +591,13 @@ input { } 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 { textarea { height: 16em; diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 6f787b5..c773732 100644 --- a/app/controllers/conferences_controller.rb +++ b/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 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 # go back to the workshop redirect_to view_workshop_url(@this_conference.slug, workshop.id) diff --git a/app/models/workshop.rb b/app/models/workshop.rb index bb54cb9..96d8c86 100644 --- a/app/models/workshop.rb +++ b/app/models/workshop.rb @@ -94,6 +94,7 @@ class Workshop < ActiveRecord::Base end def can_translate?(user, lang) + return false unless user.present? user.can_translate?(lang, locale) || (can_edit?(user) && lang.to_s != locale.to_s) end diff --git a/app/views/workshops/_show.html.haml b/app/views/workshops/_show.html.haml index 5c833f0..26203da 100644 --- a/app/views/workshops/_show.html.haml +++ b/app/views/workshops/_show.html.haml @@ -8,7 +8,7 @@ - else =_'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) - = 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) = richtext workshop.info - if preview.blank? && translations_available_for_editing