fix: added page PageProductCardFull and Addresses
This commit is contained in:
136
bo/src/stores/address.ts
Normal file
136
bo/src/stores/address.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
|
||||
export interface AddressFormData {
|
||||
street: string
|
||||
zipCode: string
|
||||
city: string
|
||||
country: string
|
||||
}
|
||||
|
||||
export interface Address {
|
||||
id: number
|
||||
street: string
|
||||
zipCode: string
|
||||
city: string
|
||||
country: string
|
||||
}
|
||||
|
||||
export const useAddressStore = defineStore('address', () => {
|
||||
const addresses = ref<Address[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const currentPage = ref(1)
|
||||
const pageSize = 10
|
||||
const totalItems = computed(() => filteredAddresses.value.length)
|
||||
const totalPages = computed(() => Math.ceil(totalItems.value / pageSize))
|
||||
|
||||
const searchQuery = ref('')
|
||||
|
||||
function initMockData() {
|
||||
addresses.value = [
|
||||
{
|
||||
id: 1,
|
||||
street: 'Main Street 123',
|
||||
zipCode: '10-001',
|
||||
city: 'New York',
|
||||
country: 'United States'
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
street: 'Oak Avenue 123',
|
||||
zipCode: '90-001',
|
||||
city: 'Los Angeles',
|
||||
country: 'United States'
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
street: 'Pine Road 123 ',
|
||||
zipCode: '60-601',
|
||||
city: 'Chicago',
|
||||
country: 'United States'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const filteredAddresses = computed(() => {
|
||||
if (!searchQuery.value) {
|
||||
return addresses.value
|
||||
}
|
||||
|
||||
const query = searchQuery.value.toLowerCase()
|
||||
|
||||
return addresses.value.filter(addr =>
|
||||
addr.street.toLowerCase().includes(query) ||
|
||||
addr.city.toLowerCase().includes(query) ||
|
||||
addr.country.toLowerCase().includes(query) ||
|
||||
addr.zipCode.toLowerCase().includes(query)
|
||||
)
|
||||
})
|
||||
const paginatedAddresses = computed(() => {
|
||||
const start = (currentPage.value - 1) * pageSize
|
||||
const end = start + pageSize
|
||||
return filteredAddresses.value.slice(start, end)
|
||||
})
|
||||
|
||||
function getAddressById(id: number): Address | undefined {
|
||||
return addresses.value.find(addr => addr.id === id)
|
||||
}
|
||||
|
||||
function addAddress(formData: AddressFormData): Address {
|
||||
const now = new Date().toISOString()
|
||||
|
||||
const newAddress: Address = {
|
||||
id: Date.now(),
|
||||
...formData,
|
||||
}
|
||||
|
||||
addresses.value.push(newAddress)
|
||||
|
||||
return newAddress
|
||||
}
|
||||
|
||||
function deleteAddress(id: number): boolean {
|
||||
const index = addresses.value.findIndex(addr => addr.id === id)
|
||||
if (index === -1) return false
|
||||
|
||||
addresses.value.splice(index, 1)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function setPage(page: number) {
|
||||
currentPage.value = page
|
||||
}
|
||||
|
||||
function setSearchQuery(query: string) {
|
||||
searchQuery.value = query
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
function resetPagination() {
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
initMockData()
|
||||
|
||||
return {
|
||||
addresses,
|
||||
loading,
|
||||
error,
|
||||
currentPage,
|
||||
pageSize,
|
||||
totalItems,
|
||||
totalPages,
|
||||
searchQuery,
|
||||
filteredAddresses,
|
||||
paginatedAddresses,
|
||||
getAddressById,
|
||||
addAddress,
|
||||
deleteAddress,
|
||||
setPage,
|
||||
setSearchQuery,
|
||||
resetPagination
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user