Initial commit: fabric-trade-user Vue 3 app with mobile and desktop layouts
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { Cart, CartItem, Company, Product } from '@/types'
|
||||
import { generateId } from '@/utils/format'
|
||||
import { validateMeterage } from '@/utils/validation'
|
||||
import { getItem, setItem, STORAGE_KEYS } from '@/services/storage'
|
||||
import { getCompanyById } from '@/data/mockData'
|
||||
|
||||
function loadCarts(): Cart[] {
|
||||
return getItem<Cart[]>(STORAGE_KEYS.carts, [])
|
||||
}
|
||||
|
||||
function saveCarts(carts: Cart[]) {
|
||||
setItem(STORAGE_KEYS.carts, carts)
|
||||
}
|
||||
|
||||
function loadActiveCartId(): string | null {
|
||||
return getItem<string | null>(STORAGE_KEYS.activeCartId, null)
|
||||
}
|
||||
|
||||
function saveActiveCartId(id: string | null) {
|
||||
setItem(STORAGE_KEYS.activeCartId, id)
|
||||
}
|
||||
|
||||
function getCartTotalMeterage(cart: Cart): number {
|
||||
return cart.items.reduce((sum, item) => sum + item.meterage, 0)
|
||||
}
|
||||
|
||||
function findOpenCartForCompany(carts: Cart[], company: Company): Cart | undefined {
|
||||
return carts.find(
|
||||
(cart) =>
|
||||
cart.companyId === company.id &&
|
||||
getCartTotalMeterage(cart) < company.maxMeteragePerInvoice,
|
||||
)
|
||||
}
|
||||
|
||||
export const useCartStore = defineStore('cart', () => {
|
||||
const carts = ref<Cart[]>(loadCarts())
|
||||
const activeCartId = ref<string | null>(loadActiveCartId())
|
||||
const selectedCompanyId = ref<string | null>(null)
|
||||
|
||||
const activeCart = computed(() => carts.value.find((c) => c.id === activeCartId.value) ?? null)
|
||||
|
||||
const totalMeterage = computed(
|
||||
() => activeCart.value?.items.reduce((sum, i) => sum + i.meterage, 0) ?? 0,
|
||||
)
|
||||
|
||||
const itemCount = computed(() => activeCart.value?.items.length ?? 0)
|
||||
|
||||
const maxMeterage = computed(() => {
|
||||
if (!activeCart.value) return 5000
|
||||
const company = getCompanyById(activeCart.value.companyId)
|
||||
return company?.maxMeteragePerInvoice ?? 5000
|
||||
})
|
||||
|
||||
const remainingCapacity = computed(() => Math.max(0, maxMeterage.value - totalMeterage.value))
|
||||
|
||||
const isFull = computed(() => totalMeterage.value >= maxMeterage.value)
|
||||
|
||||
function persist() {
|
||||
saveCarts(carts.value)
|
||||
saveActiveCartId(activeCartId.value)
|
||||
}
|
||||
|
||||
function initCartForCompany(company: Company): Cart {
|
||||
const existing = findOpenCartForCompany(carts.value, company)
|
||||
if (existing) {
|
||||
activeCartId.value = existing.id
|
||||
selectedCompanyId.value = company.id
|
||||
persist()
|
||||
return existing
|
||||
}
|
||||
|
||||
const cart: Cart = {
|
||||
id: generateId(),
|
||||
companyId: company.id,
|
||||
companyName: company.name,
|
||||
items: [],
|
||||
}
|
||||
carts.value.push(cart)
|
||||
activeCartId.value = cart.id
|
||||
selectedCompanyId.value = company.id
|
||||
persist()
|
||||
return cart
|
||||
}
|
||||
|
||||
function switchCompanyWarning(currentCompanyName: string, newCompanyName: string): string {
|
||||
return `سبد خرید فعلی مربوط به ${currentCompanyName} است. برای خرید از ${newCompanyName} باید سبد خرید جداگانهای ایجاد کنید.`
|
||||
}
|
||||
|
||||
function canSwitchToCompany(companyId: string): { allowed: boolean; message?: string } {
|
||||
if (!activeCart.value || activeCart.value.items.length === 0) {
|
||||
return { allowed: true }
|
||||
}
|
||||
if (activeCart.value.companyId === companyId) {
|
||||
return { allowed: true }
|
||||
}
|
||||
const newCompany = getCompanyById(companyId)
|
||||
return {
|
||||
allowed: false,
|
||||
message: switchCompanyWarning(activeCart.value.companyName, newCompany?.name ?? ''),
|
||||
}
|
||||
}
|
||||
|
||||
function createNewCartForSameCompany(): Cart | null {
|
||||
if (!activeCart.value) return null
|
||||
const company = getCompanyById(activeCart.value.companyId)
|
||||
if (!company) return null
|
||||
const cart: Cart = {
|
||||
id: generateId(),
|
||||
companyId: company.id,
|
||||
companyName: company.name,
|
||||
items: [],
|
||||
}
|
||||
carts.value.push(cart)
|
||||
activeCartId.value = cart.id
|
||||
persist()
|
||||
return cart
|
||||
}
|
||||
|
||||
function addItem(
|
||||
product: Product,
|
||||
catalogCode: string,
|
||||
meterage: number,
|
||||
): { success: boolean; message?: string } {
|
||||
if (!activeCart.value) {
|
||||
return { success: false, message: 'ابتدا شرکت را انتخاب کنید.' }
|
||||
}
|
||||
|
||||
if (activeCart.value.companyId !== product.companyId) {
|
||||
const currentCompany = activeCart.value.companyName
|
||||
const newCompany = getCompanyById(product.companyId)
|
||||
return {
|
||||
success: false,
|
||||
message: switchCompanyWarning(currentCompany, newCompany?.name ?? ''),
|
||||
}
|
||||
}
|
||||
|
||||
const validation = validateMeterage(meterage, product, totalMeterage.value, maxMeterage.value)
|
||||
if (!validation.valid) {
|
||||
return { success: false, message: validation.message }
|
||||
}
|
||||
|
||||
const existingIndex = activeCart.value.items.findIndex((i) => i.productId === product.id)
|
||||
if (existingIndex >= 0) {
|
||||
const existing = activeCart.value.items[existingIndex]!
|
||||
const revalidation = validateMeterage(
|
||||
meterage,
|
||||
product,
|
||||
totalMeterage.value,
|
||||
maxMeterage.value,
|
||||
existing.meterage,
|
||||
)
|
||||
if (!revalidation.valid) {
|
||||
return { success: false, message: revalidation.message }
|
||||
}
|
||||
existing.meterage = meterage
|
||||
} else {
|
||||
const item: CartItem = {
|
||||
id: generateId(),
|
||||
productId: product.id,
|
||||
catalogCode,
|
||||
productName: product.name,
|
||||
productCode: product.code,
|
||||
color: product.color,
|
||||
meterage,
|
||||
unitPrice: product.basePrice,
|
||||
}
|
||||
activeCart.value.items.push(item)
|
||||
}
|
||||
|
||||
persist()
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
function updateItemMeterage(
|
||||
itemId: string,
|
||||
meterage: number,
|
||||
product: Product,
|
||||
): { success: boolean; message?: string } {
|
||||
if (!activeCart.value) return { success: false, message: 'سبد خرید یافت نشد.' }
|
||||
const item = activeCart.value.items.find((i) => i.id === itemId)
|
||||
if (!item) return { success: false, message: 'آیتم یافت نشد.' }
|
||||
|
||||
const validation = validateMeterage(
|
||||
meterage,
|
||||
product,
|
||||
totalMeterage.value,
|
||||
maxMeterage.value,
|
||||
item.meterage,
|
||||
)
|
||||
if (!validation.valid) {
|
||||
return { success: false, message: validation.message }
|
||||
}
|
||||
|
||||
item.meterage = meterage
|
||||
persist()
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
function removeItem(itemId: string) {
|
||||
if (!activeCart.value) return
|
||||
activeCart.value.items = activeCart.value.items.filter((i) => i.id !== itemId)
|
||||
persist()
|
||||
}
|
||||
|
||||
function setNotes(notes: string) {
|
||||
if (!activeCart.value) return
|
||||
activeCart.value.notes = notes
|
||||
persist()
|
||||
}
|
||||
|
||||
function clearActiveCart() {
|
||||
if (!activeCart.value) return
|
||||
activeCart.value.items = []
|
||||
activeCart.value.notes = undefined
|
||||
persist()
|
||||
}
|
||||
|
||||
function ensureActiveCartForCartPage() {
|
||||
if (activeCart.value?.items.length) return
|
||||
|
||||
const cartWithItems = carts.value.find((cart) => cart.items.length > 0)
|
||||
if (!cartWithItems) return
|
||||
|
||||
activeCartId.value = cartWithItems.id
|
||||
selectedCompanyId.value = cartWithItems.companyId
|
||||
persist()
|
||||
}
|
||||
|
||||
function hydrate() {
|
||||
carts.value = loadCarts()
|
||||
activeCartId.value = loadActiveCartId()
|
||||
if (activeCart.value) {
|
||||
selectedCompanyId.value = activeCart.value.companyId
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
carts,
|
||||
activeCartId,
|
||||
selectedCompanyId,
|
||||
activeCart,
|
||||
totalMeterage,
|
||||
itemCount,
|
||||
maxMeterage,
|
||||
remainingCapacity,
|
||||
isFull,
|
||||
initCartForCompany,
|
||||
canSwitchToCompany,
|
||||
createNewCartForSameCompany,
|
||||
addItem,
|
||||
updateItemMeterage,
|
||||
removeItem,
|
||||
setNotes,
|
||||
clearActiveCart,
|
||||
ensureActiveCartForCartPage,
|
||||
hydrate,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user