Files
fabric_trade_user/src/composables/useCartPage.ts
T

57 lines
1.3 KiB
TypeScript
Raw Normal View History

import { computed, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '@/stores/cart'
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.activeCart?.companyName ?? '')
function removeItem(itemId: string) {
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')
}
function createNewInvoice() {
cartStore.createNewCartForSameCompany()
showFullDialog.value = false
const companyId = cartStore.activeCart?.companyId
if (companyId) {
router.push(`/companies/${companyId}/catalogs`)
}
}
onMounted(() => {
cartStore.ensureActiveCartForCartPage()
})
return {
cartStore,
items,
isEmpty,
companyName,
showFullDialog,
removeItem,
proceedToCheckout,
proceedAnyway,
createNewInvoice,
}
}