fix: page Addresses

This commit is contained in:
2026-03-18 16:31:29 +01:00
parent c79e08dbb8
commit 22e8556c9d
8 changed files with 203 additions and 154 deletions

View File

@@ -22,42 +22,20 @@ export const useAddressStore = defineStore('address', () => {
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 pageSize = 20
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'
}
{ 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
}
if (!searchQuery.value) return addresses.value
const query = searchQuery.value.toLowerCase()
@@ -68,34 +46,62 @@ export const useAddressStore = defineStore('address', () => {
addr.zipCode.toLowerCase().includes(query)
)
})
const totalItems = computed(() => filteredAddresses.value.length)
const totalPages = computed(() => Math.ceil(totalItems.value / pageSize))
const paginatedAddresses = computed(() => {
const start = (currentPage.value - 1) * pageSize
const end = start + pageSize
return filteredAddresses.value.slice(start, end)
return filteredAddresses.value.slice(start, start + pageSize)
})
function getAddressById(id: number): Address | undefined {
function getAddressById(id: number) {
return addresses.value.find(addr => addr.id === id)
}
function addAddress(formData: AddressFormData): Address {
const now = new Date().toISOString()
function normalize(data: AddressFormData): AddressFormData {
return {
street: data.street.trim(),
zipCode: data.zipCode.trim(),
city: data.city.trim(),
country: data.country.trim()
}
}
function generateId(): number {
return Math.max(0, ...addresses.value.map(a => a.id)) + 1
}
function addAddress(formData: AddressFormData): Address {
const newAddress: Address = {
id: Date.now(),
...formData,
id: generateId(),
...normalize(formData)
}
addresses.value.push(newAddress)
addresses.value.unshift(newAddress)
resetPagination()
return newAddress
}
function updateAddress(id: number, formData: AddressFormData): boolean {
const index = addresses.value.findIndex(a => a.id === id)
if (index === -1) return false
addresses.value[index] = {
...addresses.value[index],
...normalize(formData)
}
return true
}
function deleteAddress(id: number): boolean {
const index = addresses.value.findIndex(addr => addr.id === id)
const index = addresses.value.findIndex(a => a.id === id)
if (index === -1) return false
addresses.value.splice(index, 1)
resetPagination()
return true
}
@@ -128,9 +134,10 @@ export const useAddressStore = defineStore('address', () => {
paginatedAddresses,
getAddressById,
addAddress,
updateAddress,
deleteAddress,
setPage,
setSearchQuery,
resetPagination
}
})
})