SymfonyCasts/symfony5

up vote downvote jquery selectors

muffatruffa opened this issue · 1 comments

When the user up vote or down vote one answer all the answers are updated by the Ajax call to the server. This should be considered a bug.
It would be better if just the answer the user clicked on would be updated (up-voted or down-voted).
This is the way I achieved it:

  • In show.html.twig
    +<span class="js-vote-total">+ 6</span>
    instead of
    -<span><span class="js-vote-total">+ 6</span></span>

and this is my js code (just changed the jquery selector and commented the original line)

/**
 * Simple (ugly) code to handle the comment vote up/down
 */
 var $container = $('.js-vote-arrows');
 $container.find('a').on('click', function(e) {
     e.preventDefault();
     var $link = $(e.currentTarget);
     $.ajax({
         url: '/comments/10/vote/'+$link.data('direction'),
         method: 'POST'
     }).then(function(data) {
         // $container.find('.js-vote-total').text(data.votes);
         $link.siblings("span.js-vote-total").text(data.votes);
     });
 });

Hey @muffatruffa ,

Thank you for your interest in SymfonyCasts tutorials!

First of all, thank you for sharing your solution with us, that's a good point! though I have to say that was just a first dummy version of this feature that we're going to improve in the next tutorials :) In the first tutorial, it wasn't important too much, and we just didn't want to complicate things at that moment. Unfortunately, we can't change code in already released tutorials, but we can consider your feedback in future tutorials :)

Cheers!