') $('.design-studio-favorite-modal').removeClass('hidden') } }); // Make a request to toggle the favorite status of the package function toggleFavorite(el) { hasToggleFavoritesRequest = true; var data = { user_favorite_package: { package_id: $(el).data('package-id'), package_type: 'ecard' } } var favoriteIcon = $(el); if (favoriteIcon.hasClass('icon-heart')) { $.ajax({ type: 'DELETE', url: '/user/favorites', headers: { 'X-CSRF-Token': csrfToken }, data: data, success: function() { PBS.user.removeFavoriteEcard(data['user_favorite_package']['package_id']) favoriteIcon.addClass('icon-heart-empty'); favoriteIcon.removeClass('icon-heart'); hasToggleFavoritesRequest = false; }, error: function() { alert("Sorry, your card couldn't be removed from your favorites. Please try again."); hasToggleFavoritesRequest = false; } }); } else { $.ajax({ type: 'POST', url: '/user/favorites', headers: { 'X-CSRF-Token': csrfToken }, data: data, success: function(_data) { window.dataLayer.push({ event: 'ecard_design_favorited', package_id: data.user_favorite_package.package_id }); PBS.user.addFavoriteEcard(data['user_favorite_package']['package_id']) favoriteIcon.addClass('icon-heart'); favoriteIcon.removeClass('icon-heart-empty'); hasToggleFavoritesRequest = false; }, error: function(_data) { alert("Sorry, your card couldn't be favorited. Please try again."); hasToggleFavoritesRequest = false; } }); } } $('.design-studio-favorite-modal .close-modal').click(function() { $('.design-studio-favorite-modal').addClass('hidden') $('.favorite-modal-overlay').remove() }) function setHeartFromLocalStorage() { setHeartFromIds(PBS.user.favoritesData().ecard_favorite_ids) } function setHeartFromIds(ecard_favorite_ids) { var isFavorite = _.include( ecard_favorite_ids, '4e579bcb3ea34b0c58000413' ) var favorite_icon_class = isFavorite ? 'icon-heart' : 'icon-heart-empty' var opposite_favorite_icon_class = isFavorite ? 'icon-heart-empty' : 'icon-heart' $('.card-preview__window .is-favorite').addClass(favorite_icon_class).removeClass(opposite_favorite_icon_class) } // Only set hearts if the user is logged in if (PBS.current_user.registered) { setHeartFromLocalStorage(); // set hearts from local storage prior to making ajax request to prevent late-loading of hearts that haven't changed $.ajax({ url: '/user/favorites', method: 'GET', dataType: 'json', success: function(data) { try { localStorage.setItem('userFavorites', JSON.stringify(data)); setHeartFromLocalStorage(); } catch (err) { // if local storage could not be set due to size constraints, don't let it break the rest of the js on the page console.log(err); setHeartFromIds(data.ecard_favorite_ids) } } }) } else { localStorage.removeItem('userFavorites') } })