71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { computed, ref, onMounted } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import { useCartStore } from '@/stores/cart'
|
|
import { confirm } from '@/composables/useConfirm'
|
|
|
|
export function useCartPage() {
|
|
const router = useRouter()
|
|
const cartStore = useCartStore()
|
|
const showFullDialog = ref(false)
|
|
|
|
const items = computed(() => cartStore.activeCart?.items ?? [])
|
|
const isEmpty = computed(() => items.value.length === 0)
|
|
const companyName = computed(() => cartStore.companyNamesLabel || cartStore.activeCart?.companyName || '')
|
|
|
|
async function removeItem(itemId: string) {
|
|
const item = items.value.find((entry) => entry.id === itemId)
|
|
const ok = await confirm({
|
|
title: 'حذف از سبد',
|
|
message: item
|
|
? `آیا از حذف «${item.productName}» از سبد خرید مطمئن هستید؟`
|
|
: 'آیا از حذف این آیتم از سبد خرید مطمئن هستید؟',
|
|
confirmLabel: 'حذف',
|
|
cancelLabel: 'انصراف',
|
|
tone: 'danger',
|
|
})
|
|
if (!ok) return
|
|
cartStore.removeItem(itemId)
|
|
}
|
|
|
|
function proceedToCheckout() {
|
|
if (items.value.length === 0) return
|
|
if (cartStore.isFull) {
|
|
showFullDialog.value = true
|
|
return
|
|
}
|
|
router.push('/checkout/settlement')
|
|
}
|
|
|
|
function proceedAnyway() {
|
|
showFullDialog.value = false
|
|
router.push('/checkout/settlement')
|
|
}
|
|
|
|
async function createNewInvoice() {
|
|
await cartStore.createNewCartForSameCompany()
|
|
showFullDialog.value = false
|
|
const companyId = cartStore.selectedCompanyId || cartStore.activeCart?.companyId
|
|
if (companyId) {
|
|
router.push(`/companies/${companyId}/catalogs`)
|
|
} else {
|
|
router.push('/')
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
cartStore.ensureActiveCartForCartPage()
|
|
})
|
|
|
|
return {
|
|
cartStore,
|
|
items,
|
|
isEmpty,
|
|
companyName,
|
|
showFullDialog,
|
|
removeItem,
|
|
proceedToCheckout,
|
|
proceedAnyway,
|
|
createNewInvoice,
|
|
}
|
|
}
|