Skip to content

Commit

Permalink
fix: not able to change address through portal
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Jan 24, 2024
1 parent 0374dbd commit 0dba507
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 74 deletions.
79 changes: 78 additions & 1 deletion webshop/templates/includes/cart/address_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,88 @@
<div class="card-text mb-2">
{{ address.display }}
</div>
<a href="/addresses?name={{address.name}}" class="card-link">
<a href="/address/{{address.name}}" class="card-link">
<svg class="icon icon-sm">
<use href="#icon-edit"></use>
</svg>
{{ _('Edit') }}
</a>
</div>
</div>


<script>
frappe.ready(() => {
function get_update_address_dialog() {
let d = new frappe.ui.Dialog({
title: "Select Address",
fields: [{
'fieldtype': 'HTML',
'fieldname': 'address_picker',
}],
primary_action_label: __('Set Address'),
primary_action: () => {
const $card = d.$wrapper.find('.address-card.active');
const address_type = $card.closest('[data-address-type]').attr('data-address-type');
const address_name = $card.closest('[data-address-name]').attr('data-address-name');
frappe.call({
type: "POST",
method: "webshop.webshop.shopping_cart.cart.update_cart_address",
freeze: true,
args: {
address_type,
address_name
},
callback: function(r) {
d.hide();
if (!r.exc) {
$(".cart-tax-items").html(r.message.total);
shopping_cart.parent.find(
`.address-container[data-address-type="${address_type}"]`
).html(r.message.address);
}
}
});
}
});

return d;
}

function get_address_template(type) {
return {
shipping: `<div class="mb-3" data-section="shipping-address">
<div class="row no-gutters" data-fieldname="shipping_address_name">
{% for address in shipping_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="shipping"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
billing: `<div class="mb-3" data-section="billing-address">
<div class="row no-gutters" data-fieldname="customer_address">
{% for address in billing_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="billing"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
}[type];
}

$(document).on('click', '.btn-change-address', (e) => {
const d = get_update_address_dialog();
const type = $(e.currentTarget).parents('.address-container').attr('data-address-type');

$(d.get_field('address_picker').wrapper).html(
get_address_template(type)
);

d.show();
});
});
</script>
73 changes: 0 additions & 73 deletions webshop/templates/pages/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ $.extend(shopping_cart, {
},

bind_events: function() {
shopping_cart.bind_address_picker_dialog();
shopping_cart.bind_place_order();
shopping_cart.bind_request_quotation();
shopping_cart.bind_change_qty();
Expand All @@ -21,78 +20,6 @@ $.extend(shopping_cart, {
shopping_cart.bind_coupon_code();
},

bind_address_picker_dialog: function() {
const d = this.get_update_address_dialog();
this.parent.find('.btn-change-address').on('click', (e) => {
const type = $(e.currentTarget).parents('.address-container').attr('data-address-type');
$(d.get_field('address_picker').wrapper).html(
this.get_address_template(type)
);
d.show();
});
},

get_update_address_dialog() {
let d = new frappe.ui.Dialog({
title: "Select Address",
fields: [{
'fieldtype': 'HTML',
'fieldname': 'address_picker',
}],
primary_action_label: __('Set Address'),
primary_action: () => {
const $card = d.$wrapper.find('.address-card.active');
const address_type = $card.closest('[data-address-type]').attr('data-address-type');
const address_name = $card.closest('[data-address-name]').attr('data-address-name');
frappe.call({
type: "POST",
method: "webshop.webshop.shopping_cart.cart.update_cart_address",
freeze: true,
args: {
address_type,
address_name
},
callback: function(r) {
d.hide();
if (!r.exc) {
$(".cart-tax-items").html(r.message.total);
shopping_cart.parent.find(
`.address-container[data-address-type="${address_type}"]`
).html(r.message.address);
}
}
});
}
});

return d;
},

get_address_template(type) {
return {
shipping: `<div class="mb-3" data-section="shipping-address">
<div class="row no-gutters" data-fieldname="shipping_address_name">
{% for address in shipping_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="shipping"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
billing: `<div class="mb-3" data-section="billing-address">
<div class="row no-gutters" data-fieldname="customer_address">
{% for address in billing_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="billing"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
}[type];
},

bind_place_order: function() {
$(".btn-place-order").on("click", function() {
shopping_cart.place_order(this);
Expand Down

0 comments on commit 0dba507

Please sign in to comment.