96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
import type { PickupPoint } from '~/server/shared/types/yandex_pvz'
|
|
import { createSharedComposable, useStorage } from '@vueuse/core'
|
|
|
|
export const useCheckout = createSharedComposable(() => {
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
|
|
const isPickupPointSelected = ref(false)
|
|
|
|
const checkoutPickupPoint = useStorage<PickupPoint | undefined>(
|
|
'checkout-pickupPoint',
|
|
undefined,
|
|
undefined,
|
|
{
|
|
serializer: {
|
|
read: (v: string) => v ? JSON.parse(v) : undefined,
|
|
write: (v: PickupPoint | undefined) => JSON.stringify(v),
|
|
},
|
|
},
|
|
)
|
|
|
|
const setCheckoutPickupPoint = (point: PickupPoint | undefined) => {
|
|
checkoutPickupPoint.value = point
|
|
}
|
|
|
|
const checkoutContacts = useStorage('checkout-contacts', { name: '', surname: '', phone: '', email: '' })
|
|
|
|
const setCheckoutContacts = (data: { name: string, surname: string, phone: string, email: string }) => {
|
|
checkoutContacts.value = data
|
|
}
|
|
|
|
const checkoutSteps = [
|
|
{
|
|
step: 1,
|
|
title: 'delivery',
|
|
},
|
|
{
|
|
step: 2,
|
|
title: 'contacts',
|
|
},
|
|
{
|
|
step: 3,
|
|
title: 'summary',
|
|
},
|
|
]
|
|
|
|
const currentCheckoutStep
|
|
= ref(checkoutSteps.find(value => value.title === route.path.split('/').pop()) || checkoutSteps[0])
|
|
|
|
function previewStep() {
|
|
if (isPickupPointSelected.value) {
|
|
isPickupPointSelected.value = false
|
|
setCheckoutPickupPoint(undefined)
|
|
return
|
|
}
|
|
|
|
const findIndex = checkoutSteps.findIndex(value => value.step === currentCheckoutStep.value.step)
|
|
if (findIndex !== 0) {
|
|
currentCheckoutStep.value = checkoutSteps[findIndex - 1]
|
|
router.push(`/checkout/${currentCheckoutStep?.value.title}`)
|
|
}
|
|
else {
|
|
router.push(`/cart`)
|
|
}
|
|
}
|
|
|
|
function nextStep() {
|
|
const findIndex = checkoutSteps.findIndex(value => value.step === currentCheckoutStep.value.step)
|
|
if (findIndex + 1 !== checkoutSteps.length) {
|
|
currentCheckoutStep.value = checkoutSteps[findIndex + 1]
|
|
router.push(`/checkout/${currentCheckoutStep?.value.title}`)
|
|
}
|
|
}
|
|
|
|
function setStep(pathName: string) {
|
|
currentCheckoutStep.value = checkoutSteps.find(value => value.title === pathName) || checkoutSteps[0]
|
|
router.push(`/checkout/${pathName}`)
|
|
}
|
|
|
|
return {
|
|
isPickupPointSelected,
|
|
checkoutPickupPoint,
|
|
setCheckoutPickupPoint,
|
|
|
|
checkoutContacts,
|
|
setCheckoutContacts,
|
|
|
|
checkoutSteps,
|
|
currentCheckoutStep,
|
|
|
|
previewStep,
|
|
nextStep,
|
|
setStep,
|
|
}
|
|
})
|