FIX: simplify and improve choosing favorite badges (PR #13743)

This is a subsequent PR to the bug fix FIX: problems with choosing favorite badges by AndrewPrigorshnev · Pull Request #13731 · discourse/discourse · GitHub

In fact, this would fix that problem too, I just had already finished PR and decided to merge it, so users have a bug fixed, and we can discuss this PR in case we need to. Improvements are pretty straightforward, though. It fixes two things:

  1. There is no need to return anything from the server method PUT user_badges/{id}/toggle_favorite. This PR makes this method return a response with an empty body and status code 204 (which is the default status code for a PUT method without body in Rails)
  2. It changes the flow on the client side. Now when the user clicks on a star to favorite a badge, the UI on the client updates immediately, then it sends a request to the server to save that information. If there is an error, we roll back the UI. This will be more robust. For example, users have no chances to fast mark as favorite more badges than they’re allowed. Thanks to @ZogStriP for advice.

GitHub