Files
fabric_trade_user/src/stores/catalog.ts
T

67 lines
1.7 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
companies as mockCompanies,
getCatalogsByCompany,
getProductsByCatalog,
getCatalogById,
getProductById,
getCompanyById,
} from '@/data/mockData'
import type { Catalog, Company, Product } from '@/types'
export const useCatalogStore = defineStore('catalog', () => {
const companies = ref<Company[]>(mockCompanies.filter((c) => c.isActive))
const searchQuery = ref('')
function getActiveCompanies(): Company[] {
return [...companies.value].sort((a, b) => a.displayOrder - b.displayOrder)
}
function getCompany(id: string): Company | undefined {
return getCompanyById(id)
}
function fetchCatalogs(companyId: string, query = ''): Catalog[] {
let list = getCatalogsByCompany(companyId)
if (query.trim()) {
const q = query.trim()
list = list.filter((c) => c.code.includes(q) || c.name.includes(q))
}
return list
}
function fetchProducts(catalogId: string, colorFilter?: string): Product[] {
let list = getProductsByCatalog(catalogId)
if (colorFilter && colorFilter !== 'all') {
list = list.filter((p) => p.color.includes(colorFilter))
}
return list
}
function getCatalog(id: string) {
return getCatalogById(id)
}
function getProduct(id: string) {
return getProductById(id)
}
function getUniqueColors(catalogId: string): string[] {
const products = getProductsByCatalog(catalogId)
return [...new Set(products.map((p) => p.color))]
}
return {
companies,
searchQuery,
getActiveCompanies,
getCompany,
fetchCatalogs,
fetchProducts,
getCatalog,
getProduct,
getUniqueColors,
}
})