Files
myprojplanet_vite/src/components/CMyCart/CMyCart.ts

146 lines
3.7 KiB
TypeScript
Raw Normal View History

import { ref, onMounted, onBeforeMount, PropType, reactive, watch } from 'vue';
import { CCardState } from '../CCardState';
2021-09-04 15:05:34 +02:00
import { computed, defineComponent } from 'vue';
import { useGlobalStore } from '@store/globalStore';
import { useProducts } from '@store/Products';
import { CCopyBtn } from '@src/components/CCopyBtn';
import { CSingleCart } from '@src/components/CSingleCart';
import { CTitleBanner } from '@src/components/CTitleBanner';
import { tools } from '@store/Modules/tools';
2021-09-16 21:08:02 +02:00
import { useI18n } from 'vue-i18n';
2021-09-04 15:05:34 +02:00
import MixinUsers from '../../mixins/mixin-users';
import { useQuasar } from 'quasar';
import { ICart } from 'app/src/model';
2024-01-13 00:29:02 +01:00
2021-09-04 15:05:34 +02:00
export default defineComponent({
name: 'CMyCart',
props: {},
components: { CTitleBanner, CCardState, CCopyBtn, CSingleCart },
setup() {
const globalStore = useGlobalStore();
const productStore = useProducts();
const { t } = useI18n();
const $q = useQuasar();
2021-09-04 15:05:34 +02:00
const { getnumItemsCart } = MixinUsers();
2021-09-16 21:08:02 +02:00
const codice_sconto = ref('')
const descr_sconto = ref('')
const caricamentodati = ref(false)
const myCart = computed<ICart | null>(() => productStore.cart);
2021-09-04 15:05:34 +02:00
const myTotalPrice = computed(() => {
if (productStore.cart) {
return productStore.cart.totalPrice.toFixed(2);
} else {
return 0;
}
});
const totalPriceIntero = computed((): string => {
if (productStore.cart && productStore.cart.totalPriceIntero) {
return productStore.cart.totalPriceIntero.toFixed(2);
2021-09-04 15:05:34 +02:00
} else {
return '0';
2021-09-04 15:05:34 +02:00
}
});
2021-09-04 15:05:34 +02:00
const ordersCart = computed(() => {
if (!!productStore.cart) {
return productStore.cart.items;
2021-09-04 15:05:34 +02:00
} else {
return null;
2021-09-04 15:05:34 +02:00
}
});
2021-09-04 15:05:34 +02:00
const idOrdersCart = computed(() => {
if (!!productStore.cart) {
return productStore.cart._id;
} else {
return '';
}
});
2021-09-04 15:05:34 +02:00
const numOrders = computed(() => {
if (!!productStore.cart && productStore.cart.items) {
return productStore.cart.items.length;
2021-09-04 15:05:34 +02:00
} else {
return 0;
2021-09-04 15:05:34 +02:00
}
});
2021-09-04 15:05:34 +02:00
function closecart() {
globalStore.rightCartOpen = false;
2021-09-04 15:05:34 +02:00
}
2023-11-30 14:27:46 +01:00
function mounted() {
productStore.loadOrders();
2023-11-30 14:27:46 +01:00
}
function existsOrders() {
return productStore.getNumOrders() > 0;
}
async function applicaSconto(codice: string) {
try {
caricamentodati.value = true;
const rissconto = await productStore.ApplicaSconto({
cart_id: myCart.value._id,
codice_sconto: codice,
});
if (rissconto) {
if (rissconto.msg) {
tools.showNeutralNotif($q, `${rissconto.msg}`);
} else if (rissconto.valido && rissconto.mycart) {
tools.showPositiveNotif($q, 'Sconto Applicato!');
} else {
tools.showNegativeNotif($q, `${rissconto.errmsg}`);
}
codice_sconto.value = '';
descr_sconto.value = '';
if (rissconto.mycart) {
myCart.value = rissconto.mycart
};
}
} catch (error: any) {
console.log('error ApplicaSconto', error);
tools.showNegativeNotif($q, `Sconto Non Applicato! ${error?.message || ''}`);
codice_sconto.value = '';
descr_sconto.value = '';
} finally {
caricamentodati.value = false;
}
}
async function confermaCodiceSconto() {
await applicaSconto(codice_sconto.value);
}
onMounted(mounted);
2023-11-30 14:27:46 +01:00
2021-09-04 15:05:34 +02:00
return {
myCart,
myTotalPrice,
totalPriceIntero,
2021-09-04 15:05:34 +02:00
ordersCart,
numOrders,
closecart,
2021-09-16 21:08:02 +02:00
getnumItemsCart,
existsOrders,
globalStore,
2024-01-13 00:29:02 +01:00
t,
codice_sconto,
confermaCodiceSconto,
caricamentodati,
descr_sconto,
idOrdersCart,
};
2021-09-04 15:05:34 +02:00
},
});