Last active
April 19, 2020 11:14
-
-
Save zivce/4f0bf8046797d4c6651b3ef0ac9c9d08 to your computer and use it in GitHub Desktop.
Added strategy factory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Strategy { | |
constructor(name, handler) { | |
this._name = name; | |
this._handler = handler; | |
} | |
execute() { | |
this._handler(); | |
} | |
} | |
export class StrategyManager { | |
constructor() { | |
this._strategies = []; | |
} | |
addStrategy(strategy) { | |
this._strategies = [...this._strategies, strategy]; | |
} | |
executeAll() { | |
this._strategies.forEach(strategy => strategy.execute()); | |
this._strategies = []; | |
} | |
} | |
export class DefWishlist { | |
$wishlistList = $(".wishlist-list__body"); | |
$myFavouritesRollover = $(".mini-favourites"); | |
$myFavouritesSendList = $(".wishlist-send-list"); | |
$emptyWishlistHTML = $("#emptyWishlistPopupTemplate").html(); | |
$wishlistCount = $("[data-wishlist-item-count]"); | |
$myFavouritesRolloverContainer = $( | |
".my-favourites-rollover__content", | |
$myFavouritesRollover | |
); | |
$myFavouritesRolloverControls = $( | |
".my-favourites-rollover__controls", | |
$myFavouritesRollover | |
); | |
constructor() { | |
this._strategyManager = new StrategyManager(); | |
} | |
removeProductFromWishListHandlerRefactored(e) { | |
e.stopPropagation(); | |
const target = $(e.currentTarget); | |
const productCode = target.data("product-code"); | |
const wishlistData = await requestPost("/favourites/remove", { productCode }, { format: "text" }); | |
const count = this.getCount(wishlistData); | |
if (count === 0) { | |
this._strategyManager.addStrategy("zeroCount", this.emptyWishListStrategy); | |
} | |
if (count <= 12) { | |
this._strategyManager.addStrategy("lessThanTwelve", this.lessThanTwelveItemsStrategy); | |
} | |
if (this.$wishlistList.length) { | |
this._strategyManager.addStrategy("wishListExists",() => this.wishListExistsStrategy(wishlistData)); | |
} | |
if (!$(wishlistData).find(".wishlist-list__body").length) { | |
this._strategyManager.addStrategy("noWishlistBody", () => this.noWishlistBodyStrategy(wishlistData)); | |
} | |
this._strategyManager.executeAll(); | |
this.defaultWishListBodyStrategy(count); | |
return false; | |
} | |
getCount(wishlistData) { | |
return parseInt( | |
$(wishlistData) | |
.find("[data-wishlist-item-count]") | |
.attr("data-wishlist-item-count"), | |
10 | |
); | |
} | |
defaultWishListBodyStrategy(count) { | |
$wishlistCount.html(count); | |
$wishlistCount.attr("data-wishlist-item-count", count); | |
this.refreshWishListRollover(); | |
} | |
noWishlistBodyStrategy(wishlistData) { | |
const $listerContainer = $(".wishlist"); | |
$listerContainer.html($(wishlistData).find(".wishlist").html()); | |
} | |
emptyWishListStrategy() { | |
$myFavouritesSendList.remove(); | |
if ($myFavouritesRolloverContainer.length) { | |
$myFavouritesRolloverContainer.remove(); | |
$myFavouritesRolloverControls.remove(); | |
$myFavouritesRollover.prepend(emptyWishlistHTML); | |
} | |
} | |
wishListExistsStrategy(wishlistData) { | |
startRefreshEffects($wishlistList); | |
if ($(".wishlist--limit-achieved").length) | |
$(".wishlist--limit-achieved").remove(); | |
$wishlistList.html($(wishlistData).find(".wishlist-list__body").html()); | |
window.BB.product.bindToAddToCartForm(); | |
if ($(".js-load-more-favourites").length) { | |
$(".js-load-more-favourites").find("span").html(`(${count})`); | |
} else { | |
$(".column-block").show(); | |
} | |
$(".remove-favourite").removeAttr("disabled").removeClass("disabled"); | |
initWishlistATB(); | |
setTimeout(() => endRefreshEffects($wishlistList), 500); | |
} | |
lessThanTwelveItemsStrategy() { | |
$(".js-load-more-favourites").hide(); | |
$(".column-block").show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment