roseweixel/lacquer-lover

View on GitHub
app/views/users/live_notifications.js.erb

Summary

Maintainability
Test Coverage
// SEE IF ANY DUE DATES HAVE CHANGED:

var currentDueDates = JSON.parse("<%= @user.due_date_list %>".replace(/&quot;/g, '"'));
var previousDueDates = $.map($(".due-date"), function(val){
  return $(val).text()
});

var dueDatesChanged = currentDueDates.toString() !== previousDueDates.toString();


// SEE IF ANYTHING ELSE HAS CHANGED:

var previousAllCategoriesArray = $('#all-categories-tracker').text();
var currentAllCategoriesArray = "<%= @user.transactions_and_friendships_data_array %>".replace(/&quot;/g, '"');
    
var countOrStatusChanged = previousAllCategoriesArray !== currentAllCategoriesArray;


// UPDATE THE DOM ONLY IF A CHANGE HAS OCCURRED:

if (countOrStatusChanged || dueDatesChanged) {

    // update the all-categories-tracker
    $('#all-categories-tracker').html(currentAllCategoriesArray);

    // update accepted friendships
    var previousFriendCount = $('#hidden_friend_count .tracker').attr('value');
    var currentFriendCount = "<%= @user.accepted_friends.count %>";

    // re-render friends only if the friend count has changed
    if (previousFriendCount !== currentFriendCount) {
        $('#hidden_friend_count').html("<%= j render 'count', count: @user.accepted_friends.count %>");
        $('#accepted-friends').html("<%= j render partial: 'friends', locals: {user: @user} %>");
    }

    // update all transactions and friendships notifications
    <% (notification_categories).each do |category| %>
        var currentCategory = "<%= category %>";

        // DEAL WITH ANY CHANGES IN COUNT OR STATE:

        // get and format the current and previous category arrays (e.g. ['active', 'pending'])
        var currentCategoryArray = JSON.parse("<%= current_category_array = @user.send(category).pluck(:state) %>".replace(/&quot;/g, '"'));

        var previousCategoryArray = JSON.parse($(".hidden-tracker#"+ currentCategory +"_state_array").html().trim());

        // if there's been a change for this category
        if (currentCategoryArray.toString() !== previousCategoryArray.toString()) {

            // see if something new was added to this category
            var addedToCategory = currentCategoryArray.length > previousCategoryArray.length;

            var previousCount = previousCategoryArray.length;
            var currentCount = currentCategoryArray.length;

            // update the hidden tracker
            $(".hidden-tracker#"+ currentCategory +"_state_array").html("<%= j render partial: 'hidden_tracker', locals: {user: @user, category: category} %>");

            // render the category again
            $('ul#' + currentCategory).html("<%=  j render category, user: @user %>");

            // update the counter badge
            $('#'+ currentCategory + '_wrapper').find('.badge-tracker-wrapper').html("<%= j render 'count', count: current_category_array.count %>");

            // if there are any notifications for this category
            if (currentCount > 0) {
                // show the category heading and counter badge
                $('#'+ currentCategory + '_wrapper').slideDown();
                $('#'+ currentCategory + '_wrapper').find('.badge-tracker-wrapper').slideDown();

                // make the badge red if any new notifications were added
                if (addedToCategory) {
                    $('#'+ currentCategory + '_wrapper').find('.tracker').addClass('red-background');
                }
            } else {
                // hide the category heading and counter badge if the count for this category is 0
                $('#'+ currentCategory + '_wrapper').find('.badge-tracker-wrapper').slideUp();
                $('#'+ currentCategory + '_wrapper').slideUp();
            }
        }

        // DEAL WITH CHANGED DUE DATES FOR TRANSACTIONS:

        // if it's possible for the category to have an associated due date and due dates have changed
        if (currentCategory === 'active_requested_transactions' && dueDatesChanged) {

            // render the category again
            $('ul#' + currentCategory).html("<%=  j render category, user: @user %>");

            // show the 'due date updated' alert badge
            $('#'+ currentCategory + '_wrapper').find('.due-date-updated').removeClass('hidden');
        }
        
    <% end %>
}