2021-05-12 19:52:05 +00:00
|
|
|
frappe.provide("e_commerce.wishlist");
|
|
|
|
var wishlist = e_commerce.wishlist;
|
2021-03-14 11:58:49 +00:00
|
|
|
|
2021-05-12 19:52:05 +00:00
|
|
|
frappe.provide("e_commerce.shopping_cart");
|
|
|
|
var shopping_cart = e_commerce.shopping_cart;
|
2021-03-14 11:58:49 +00:00
|
|
|
|
|
|
|
$.extend(wishlist, {
|
|
|
|
set_wishlist_count: function() {
|
2021-03-15 18:35:53 +00:00
|
|
|
// set badge count for wishlist icon
|
2021-03-14 11:58:49 +00:00
|
|
|
var wish_count = frappe.get_cookie("wish_count");
|
2021-03-16 05:39:01 +00:00
|
|
|
if (frappe.session.user==="Guest") {
|
2021-03-14 11:58:49 +00:00
|
|
|
wish_count = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:39:01 +00:00
|
|
|
if (wish_count) {
|
2021-03-14 11:58:49 +00:00
|
|
|
$(".wishlist").toggleClass('hidden', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
var $wishlist = $('.wishlist-icon');
|
|
|
|
var $badge = $wishlist.find("#wish-count");
|
|
|
|
|
2021-03-16 05:39:01 +00:00
|
|
|
if (parseInt(wish_count) === 0 || wish_count === undefined) {
|
2021-03-14 11:58:49 +00:00
|
|
|
$wishlist.css("display", "none");
|
2021-03-16 05:39:01 +00:00
|
|
|
} else {
|
2021-03-14 11:58:49 +00:00
|
|
|
$wishlist.css("display", "inline");
|
|
|
|
}
|
2021-03-16 05:39:01 +00:00
|
|
|
if (wish_count) {
|
2021-03-14 11:58:49 +00:00
|
|
|
$badge.html(wish_count);
|
|
|
|
$wishlist.addClass('cart-animate');
|
|
|
|
setTimeout(() => {
|
|
|
|
$wishlist.removeClass('cart-animate');
|
|
|
|
}, 500);
|
|
|
|
} else {
|
|
|
|
$badge.remove();
|
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
bind_move_to_cart_action: function() {
|
|
|
|
// move item to cart from wishlist
|
|
|
|
$('.page_content').on("click", ".btn-add-to-cart", (e) => {
|
|
|
|
const $move_to_cart_btn = $(e.currentTarget);
|
|
|
|
let item_code = $move_to_cart_btn.data("item-code");
|
|
|
|
|
|
|
|
shopping_cart.shopping_cart_update({
|
|
|
|
item_code,
|
|
|
|
qty: 1,
|
|
|
|
cart_dropdown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
let success_action = function() {
|
2021-04-20 16:24:52 +00:00
|
|
|
const $card_wrapper = $move_to_cart_btn.closest(".wishlist-card");
|
2021-03-15 18:35:53 +00:00
|
|
|
$card_wrapper.addClass("wish-removed");
|
|
|
|
};
|
|
|
|
let args = { item_code: item_code };
|
|
|
|
this.add_remove_from_wishlist("remove", args, success_action, null, true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
bind_remove_action: function() {
|
|
|
|
// remove item from wishlist
|
2021-05-19 15:47:47 +00:00
|
|
|
let me = this;
|
|
|
|
|
2021-03-15 18:35:53 +00:00
|
|
|
$('.page_content').on("click", ".remove-wish", (e) => {
|
|
|
|
const $remove_wish_btn = $(e.currentTarget);
|
|
|
|
let item_code = $remove_wish_btn.data("item-code");
|
|
|
|
|
|
|
|
let success_action = function() {
|
2021-04-20 16:24:52 +00:00
|
|
|
const $card_wrapper = $remove_wish_btn.closest(".wishlist-card");
|
2021-03-15 18:35:53 +00:00
|
|
|
$card_wrapper.addClass("wish-removed");
|
2021-05-19 15:47:47 +00:00
|
|
|
if (frappe.get_cookie("wish_count") == 0) {
|
|
|
|
$(".page_content").empty();
|
|
|
|
me.render_empty_state();
|
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
};
|
|
|
|
let args = { item_code: item_code };
|
|
|
|
this.add_remove_from_wishlist("remove", args, success_action);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
bind_wishlist_action() {
|
|
|
|
// 'wish'('like') or 'unwish' item in product listing
|
2021-07-11 21:58:33 +00:00
|
|
|
$('.page_content').on('click', '.like-action, .like-action-list', (e) => {
|
2021-03-15 18:35:53 +00:00
|
|
|
const $btn = $(e.currentTarget);
|
2021-07-13 18:16:24 +00:00
|
|
|
this.wishlist_action($btn);
|
|
|
|
});
|
|
|
|
},
|
2021-07-08 05:27:01 +00:00
|
|
|
|
2021-07-13 18:16:24 +00:00
|
|
|
wishlist_action(btn) {
|
|
|
|
const $wish_icon = btn.find('.wish-icon');
|
|
|
|
let me = this;
|
2021-07-08 05:27:01 +00:00
|
|
|
|
2021-07-13 18:16:24 +00:00
|
|
|
if (frappe.session.user==="Guest") {
|
|
|
|
if (localStorage) {
|
|
|
|
localStorage.setItem("last_visited", window.location.pathname);
|
2021-05-19 15:47:47 +00:00
|
|
|
}
|
2021-07-13 18:16:24 +00:00
|
|
|
this.redirect_guest();
|
|
|
|
return;
|
|
|
|
}
|
2021-05-19 15:47:47 +00:00
|
|
|
|
2021-07-13 18:16:24 +00:00
|
|
|
let success_action = function() {
|
|
|
|
e_commerce.wishlist.set_wishlist_count();
|
|
|
|
};
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2021-07-13 18:16:24 +00:00
|
|
|
if ($wish_icon.hasClass('wished')) {
|
|
|
|
// un-wish item
|
|
|
|
btn.removeClass("like-animate");
|
|
|
|
btn.addClass("like-action-wished");
|
|
|
|
this.toggle_button_class($wish_icon, 'wished', 'not-wished');
|
|
|
|
|
|
|
|
let args = { item_code: btn.data('item-code') };
|
|
|
|
let failure_action = function() {
|
|
|
|
me.toggle_button_class($wish_icon, 'not-wished', 'wished');
|
|
|
|
};
|
|
|
|
this.add_remove_from_wishlist("remove", args, success_action, failure_action);
|
|
|
|
} else {
|
|
|
|
// wish item
|
|
|
|
btn.addClass("like-animate");
|
|
|
|
btn.addClass("like-action-wished");
|
|
|
|
this.toggle_button_class($wish_icon, 'not-wished', 'wished');
|
|
|
|
|
|
|
|
let args = {
|
|
|
|
item_code: btn.data('item-code'),
|
|
|
|
price: btn.data('price'),
|
|
|
|
formatted_price: btn.data('formatted-price')
|
|
|
|
};
|
|
|
|
let failure_action = function() {
|
|
|
|
me.toggle_button_class($wish_icon, 'wished', 'not-wished');
|
|
|
|
};
|
|
|
|
this.add_remove_from_wishlist("add", args, success_action, failure_action);
|
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toggle_button_class(button, remove, add) {
|
|
|
|
button.removeClass(remove);
|
|
|
|
button.addClass(add);
|
|
|
|
},
|
|
|
|
|
|
|
|
add_remove_from_wishlist(action, args, success_action, failure_action, async=false) {
|
|
|
|
/* AJAX call to add or remove Item from Wishlist
|
|
|
|
action: "add" or "remove"
|
|
|
|
args: args for method (item_code, price, formatted_price),
|
|
|
|
success_action: method to execute on successs,
|
|
|
|
failure_action: method to execute on failure,
|
|
|
|
async: make call asynchronously (true/false). */
|
2021-05-19 15:47:47 +00:00
|
|
|
if (frappe.session.user==="Guest") {
|
2021-05-24 20:05:22 +00:00
|
|
|
if (localStorage) {
|
2021-05-19 15:47:47 +00:00
|
|
|
localStorage.setItem("last_visited", window.location.pathname);
|
|
|
|
}
|
2021-07-08 05:27:01 +00:00
|
|
|
this.redirect_guest();
|
2021-05-19 15:47:47 +00:00
|
|
|
} else {
|
|
|
|
let method = "erpnext.e_commerce.doctype.wishlist.wishlist.add_to_wishlist";
|
|
|
|
if (action === "remove") {
|
|
|
|
method = "erpnext.e_commerce.doctype.wishlist.wishlist.remove_from_wishlist";
|
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2021-05-19 15:47:47 +00:00
|
|
|
frappe.call({
|
|
|
|
async: async,
|
|
|
|
type: "POST",
|
|
|
|
method: method,
|
|
|
|
args: args,
|
|
|
|
callback: function (r) {
|
|
|
|
if (r.exc) {
|
|
|
|
if (failure_action && (typeof failure_action === 'function')) {
|
|
|
|
failure_action();
|
|
|
|
}
|
|
|
|
frappe.msgprint({
|
|
|
|
message: __("Sorry, something went wrong. Please refresh."),
|
|
|
|
indicator: "red", title: __("Note")
|
|
|
|
});
|
|
|
|
} else if (success_action && (typeof success_action === 'function')) {
|
|
|
|
success_action();
|
2021-03-16 05:39:01 +00:00
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
}
|
2021-05-19 15:47:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-08 05:27:01 +00:00
|
|
|
redirect_guest() {
|
|
|
|
frappe.call('erpnext.e_commerce.api.get_guest_redirect_on_action').then((res) => {
|
|
|
|
window.location.href = res.message || "/login";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-05-19 15:47:47 +00:00
|
|
|
render_empty_state() {
|
|
|
|
$(".page_content").append(`
|
|
|
|
<div class="cart-empty frappe-card">
|
|
|
|
<div class="cart-empty-state">
|
|
|
|
<img src="/assets/erpnext/images/ui-states/cart-empty-state.png" alt="Empty Cart">
|
|
|
|
</div>
|
|
|
|
<div class="cart-empty-message mt-4">${ __('Wishlist is empty !') }</p>
|
|
|
|
</div>
|
|
|
|
`);
|
2021-03-14 11:58:49 +00:00
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
frappe.ready(function() {
|
|
|
|
if (window.location.pathname !== "/wishlist") {
|
|
|
|
$(".wishlist").toggleClass('hidden', true);
|
|
|
|
wishlist.set_wishlist_count();
|
|
|
|
} else {
|
|
|
|
wishlist.bind_move_to_cart_action();
|
|
|
|
wishlist.bind_remove_action();
|
|
|
|
}
|
|
|
|
|
2021-03-14 11:58:49 +00:00
|
|
|
});
|