Files
salvato.newfreeplanet/src/components/CProductCard/CProductCard.ts

196 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-09-16 21:08:02 +02:00
import { defineComponent, ref, toRef, computed, PropType, watch } from 'vue'
import { useI18n } from '@src/boot/i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
2023-11-28 15:31:25 +01:00
import { CTitleBanner } from '../CTitleBanner'
2021-09-16 21:08:02 +02:00
import { CCardState } from '../CCardState'
import { CCopyBtn } from '../CCopyBtn'
import { IOrder, IProduct } from '@src/model'
import { tools } from '@store/Modules/tools'
import { useProducts } from '@store/Products'
export default defineComponent({
name: 'CProductCard',
props: {
product: {
type: Object as PropType<IProduct | null>,
required: false,
default: null,
},
code: {
type: String,
required: false,
default: '',
},
complete: {
type: Boolean,
required: false,
default: false,
},
order: {
type: Object as PropType<IOrder>,
required: false,
default: () => {
return {
idapp: process.env.APP_ID,
quantity: 1,
idStorehouse: ''
}
},
},
},
components: { CTitleBanner, CCardState, CCopyBtn },
setup(props, { emit }) {
const $q = useQuasar()
const { t } = useI18n()
const userStore = useUserStore()
const globalStore = useGlobalStore()
const products = useProducts()
const myorder = toRef(props, 'order')
const myproduct = ref(<IProduct> {})
2023-11-30 15:43:32 +01:00
const storeSelected = ref('')
2021-09-16 21:08:02 +02:00
function iconWhishlist(order: IProduct) {
if (true) {
return 'far fa-heart'
} else {
return 'fas fa-heart'
}
}
function decqty() {
2023-11-30 15:43:32 +01:00
if (myorder.value.quantity > 0)
myorder.value.quantity--
2021-09-16 21:08:02 +02:00
}
function addqty() {
2023-11-30 15:43:32 +01:00
if (myorder.value.quantity < 10)
myorder.value.quantity++
2021-09-16 21:08:02 +02:00
}
function addtoCart() {
if (!userStore.isLogged) {
2023-11-30 14:27:46 +01:00
tools.showNeutralNotif($q, t('ecomm.area_personale'))
2021-09-16 21:08:02 +02:00
globalStore.rightDrawerOpen = true
return false
}
// Controlla se esiste già nel carrello il prodotto
if (products.existProductInCart(myproduct.value._id)) {
2023-11-30 14:27:46 +01:00
tools.showNegativeNotif($q, t('ecomm.already_in_cart'))
2021-09-16 21:08:02 +02:00
} else {
products.addToCart({ product: myproduct.value, order: props.order }).then((ris) => {
2023-11-30 14:27:46 +01:00
let strprod = t('ecomm.prodotto')
2023-11-30 15:43:32 +01:00
if (myorder.value.quantity > 1)
2023-11-30 14:27:46 +01:00
strprod = t('ecomm.prodotti')
2021-09-16 21:08:02 +02:00
if (ris)
2023-11-30 14:27:46 +01:00
tools.showPositiveNotif($q, t('ecomm.haveadded') + ' ' + myorder.value.quantity + ' ' + strprod + ' ' + t('ecomm.tocart'))
2021-09-16 21:08:02 +02:00
else
2023-11-30 14:27:46 +01:00
tools.showNegativeNotif($q, t('ecomm.error_cart'))
2021-09-16 21:08:02 +02:00
})
}
}
function getnumstore() {
if (myproduct.value.storehouses)
return myproduct.value.storehouses.length
else
return 0
}
function getSingleStorehouse() {
const mystore = myproduct.value.storehouses[0]
2023-11-30 14:27:46 +01:00
if (mystore)
return mystore.name + ' (' + mystore.city + ')'
else
return ''
2021-09-16 21:08:02 +02:00
}
function getStorehouses() {
const myarr: any = []
let ind = 1
myproduct.value.storehouses.forEach((store) => {
myarr.push(
{
id: ind,
label: store.name + ' (' + store.city + ')',
value: store._id
})
ind++
})
// console.log('arraystore', myarr)
return myarr
}
function checkifCartDisable() {
return !myorder.value.idStorehouse
}
watch(() => props.code, (newval, oldval) => {
console.log('change code')
load()
})
2023-11-30 15:43:32 +01:00
watch(() => storeSelected.value, (newval, oldval) => {
myorder.value.idStorehouse = newval
})
2021-09-16 21:08:02 +02:00
async function load() {
// console.log('created Cproductcard', code)
if (props.code) {
myproduct.value = await products.loadProduct({ code: props.code })
} else {
// @ts-ignore
myproduct.value = props.product
}
console.log('myproduct', myproduct, 'product', props.product)
if (!!myproduct.value) {
if (myproduct.value.storehouses.length === 1) {
myorder.value.idStorehouse = myproduct.value.storehouses[0]._id
}
}
}
function getmycardcl() {
return (props.complete) ? 'my-card-big' : 'my-card'
}
function getclimg() {
return (props.complete) ? 'myimgtitle centermydiv' : 'centermydiv'
}
load()
return {
decqty,
addqty,
addtoCart,
iconWhishlist,
getmycardcl,
getclimg,
getnumstore,
getSingleStorehouse,
getStorehouses,
checkifCartDisable,
myproduct,
myorder,
2023-11-30 14:27:46 +01:00
tools,
t,
2023-11-30 15:43:32 +01:00
storeSelected,
2021-09-16 21:08:02 +02:00
}
}
})