FleetCart/Themes/Storefront/resources/assets/public/js/mixins/CartHelpersMixin.js

112 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-12-03 14:07:47 +00:00
import store from "../store";
2023-06-11 12:14:03 +00:00
export default {
data() {
return {
loadingOrderSummary: false,
shippingMethodName: null,
applyingCoupon: false,
couponCode: null,
couponError: null,
};
},
computed: {
cart() {
return store.state.cart;
},
cartIsEmpty() {
return store.cartIsEmpty();
},
cartIsNotEmpty() {
2023-12-03 14:07:47 +00:00
return !store.cartIsEmpty();
2023-06-11 12:14:03 +00:00
},
hasShippingMethod() {
return store.hasShippingMethod();
},
firstShippingMethod() {
return Object.keys(store.state.cart.availableShippingMethods)[0];
},
hasCoupon() {
return store.state.cart.coupon.code !== undefined;
},
},
methods: {
applyCoupon() {
2023-12-03 14:07:47 +00:00
if (!this.couponCode) {
2023-06-11 12:14:03 +00:00
return;
}
this.loadingOrderSummary = true;
this.applyingCoupon = true;
2023-12-03 14:07:47 +00:00
axios
.post(route("cart.coupon.store"), { coupon: this.couponCode })
.then((response) => {
this.couponCode = null;
this.couponError = null;
store.updateCart(response.data);
})
.catch((error) => {
this.couponError = error.response.data.message;
})
.finally(() => {
this.loadingOrderSummary = false;
this.applyingCoupon = false;
});
2023-06-11 12:14:03 +00:00
},
removeCoupon() {
this.loadingOrderSummary = true;
2023-12-03 14:07:47 +00:00
axios
.delete(route("cart.coupon.destroy"))
.then((response) => {
store.updateCart(response.data);
})
.catch((error) => {
this.$notify(error.response.data.message);
})
.finally(() => {
this.loadingOrderSummary = false;
});
2023-06-11 12:14:03 +00:00
},
2023-12-03 14:07:47 +00:00
async updateShippingMethod(shippingMethodName) {
if (!shippingMethodName) {
2023-06-11 12:14:03 +00:00
return;
}
this.loadingOrderSummary = true;
this.changeShippingMethod(shippingMethodName);
2023-12-03 14:07:47 +00:00
try {
const response = await axios.post(
route("cart.shipping_method.store", {
...(store.hasCoupon() && {
coupon_code: store.getCoupon(),
}),
}),
{
shipping_method: shippingMethodName,
}
);
store.updateCart(response.data);
} catch (error) {
this.$notify(error.response.data.message);
} finally {
2023-06-11 12:14:03 +00:00
this.loadingOrderSummary = false;
2023-12-03 14:07:47 +00:00
}
2023-06-11 12:14:03 +00:00
},
},
};