fix: addresses
This commit is contained in:
@@ -1,231 +1,58 @@
|
||||
import { useFetchJson } from '@/composable/useFetchJson'
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { AddressTemplate } from './cart'
|
||||
|
||||
export interface AddressFormData {
|
||||
street: string
|
||||
zipCode: string
|
||||
city: string
|
||||
country: string
|
||||
}
|
||||
import { ref } from 'vue'
|
||||
|
||||
export interface Address {
|
||||
id: number
|
||||
street: string
|
||||
zipCode: string
|
||||
city: string
|
||||
country: string
|
||||
country_id: number
|
||||
address_unparsed: Record<string, 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 = 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' }
|
||||
]
|
||||
}
|
||||
|
||||
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 totalItems = computed(() => filteredAddresses.value.length)
|
||||
const totalPages = computed(() => Math.ceil(totalItems.value / pageSize))
|
||||
|
||||
function getAddressById(id: number) {
|
||||
return addresses.value.find(addr => addr.id === id)
|
||||
}
|
||||
|
||||
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 setPage(page: number) {
|
||||
currentPage.value = page
|
||||
}
|
||||
|
||||
function setSearchQuery(query: string) {
|
||||
searchQuery.value = query
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
function resetPagination() {
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
initMockData()
|
||||
|
||||
const addresses = ref<Address[]>([])
|
||||
const addressLoading = ref(false)
|
||||
const addressError = ref<string | null>(null)
|
||||
const addressSearchQuery = ref('')
|
||||
const addressCurrentPage = ref(1)
|
||||
const addressPageSize = 20
|
||||
const addressTotalCount = ref(0)
|
||||
|
||||
function transformAddressResponse(address: any): Address {
|
||||
const info = address.address_info || address.addressInfo || {}
|
||||
|
||||
return {
|
||||
id: address.id ?? 0,
|
||||
country_id: address.country_id ?? address.countryId ?? 1,
|
||||
customer_id: address.customer_id ?? address.customerId ?? 0,
|
||||
address_info: info as Record<string, string>
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchAddresses() {
|
||||
addressLoading.value = true
|
||||
addressError.value = null
|
||||
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const queryParam = addressSearchQuery.value ? `&query=${encodeURIComponent(addressSearchQuery.value)}` : ''
|
||||
const response = await useFetchJson<Address[]>(`/api/v1/restricted/addresses/retrieve-addresses?page=${addressCurrentPage.value}&elems=${addressPageSize}${queryParam}`)
|
||||
addresses.value = response.items || []
|
||||
addressTotalCount.value = response.count ?? addresses.value.length
|
||||
} catch (error: unknown) {
|
||||
addressError.value = error instanceof Error ? error.message : 'Failed to load addresses'
|
||||
const res = await useFetchJson<Address[]>('/api/v1/restricted/addresses/retrieve-addresses')
|
||||
addresses.value = res.items ?? []
|
||||
} catch (e: unknown) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load addresses'
|
||||
} finally {
|
||||
addressLoading.value = false
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function addAddress(countryId: number, formData: AddressTemplate): Promise<Address | null> {
|
||||
addressLoading.value = true
|
||||
addressError.value = null
|
||||
|
||||
try {
|
||||
const response = await useFetchJson<any>(`/api/v1/restricted/addresses/add-new-address?country_id=${countryId}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(formData)
|
||||
})
|
||||
|
||||
await fetchAddresses()
|
||||
return transformAddressResponse(response.items ?? response)
|
||||
} catch (error: unknown) {
|
||||
addressError.value = error instanceof Error ? error.message : 'Failed to create address'
|
||||
return null
|
||||
} finally {
|
||||
addressLoading.value = false
|
||||
}
|
||||
async function deleteAddress(id: number) {
|
||||
await useFetchJson(`/api/v1/restricted/addresses/delete-address?address_id=${id}`, { method: 'DELETE' })
|
||||
addresses.value = addresses.value.filter((a) => a.id !== id)
|
||||
}
|
||||
|
||||
async function getAddressTemplate(countryId: number): Promise<AddressTemplate> {
|
||||
const response = await useFetchJson<any>(`/api/v1/restricted/addresses/get-template?country_id=${countryId}`)
|
||||
return response.items ?? {}
|
||||
async function getTemplate(countryId: number): Promise<Record<string, string>> {
|
||||
const res = await useFetchJson<Record<string, string>>(
|
||||
`/api/v1/restricted/addresses/get-template?country_id=${countryId}`
|
||||
)
|
||||
return res.items ?? {}
|
||||
}
|
||||
|
||||
async function updateAddress(id: number, countryId: number, formData: AddressTemplate): Promise<boolean> {
|
||||
addressLoading.value = true
|
||||
addressError.value = null
|
||||
|
||||
try {
|
||||
await useFetchJson<any>(`/api/v1/restricted/addresses/modify-address?country_id=${countryId}&address_id=${id}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(formData)
|
||||
})
|
||||
|
||||
await fetchAddresses()
|
||||
return true
|
||||
} catch (error: unknown) {
|
||||
addressError.value = error instanceof Error ? error.message : 'Failed to update address'
|
||||
return false
|
||||
} finally {
|
||||
addressLoading.value = false
|
||||
}
|
||||
async function createAddress(countryId: number, data: Record<string, string>) {
|
||||
await useFetchJson(`/api/v1/restricted/addresses/add-new-address?country_id=${countryId}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
await fetchAddresses()
|
||||
}
|
||||
|
||||
async function deleteAddress(id: number): Promise<boolean> {
|
||||
addressLoading.value = true
|
||||
addressError.value = null
|
||||
|
||||
try {
|
||||
await useFetchJson<any>(`/api/v1/restricted/addresses/delete-address?address_id=${id}`, {
|
||||
method: 'DELETE'
|
||||
})
|
||||
|
||||
await fetchAddresses()
|
||||
return true
|
||||
} catch (error: unknown) {
|
||||
addressError.value = error instanceof Error ? error.message : 'Failed to delete address'
|
||||
return false
|
||||
} finally {
|
||||
addressLoading.value = false
|
||||
}
|
||||
async function updateAddress(id: number, countryId: number, data: Record<string, string>) {
|
||||
await useFetchJson(`/api/v1/restricted/addresses/modify-address?country_id=${countryId}&address_id=${id}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
await fetchAddresses()
|
||||
}
|
||||
|
||||
const totalAddressItems = computed(() => addressTotalCount.value || addresses.value.length)
|
||||
const totalAddressPages = computed(() => Math.ceil(totalAddressItems.value / addressPageSize))
|
||||
|
||||
const paginatedAddresses = computed(() => addresses.value)
|
||||
|
||||
function setAddressPage(page: number) {
|
||||
addressCurrentPage.value = page
|
||||
return fetchAddresses()
|
||||
}
|
||||
|
||||
function setAddressSearchQuery(query: string) {
|
||||
addressSearchQuery.value = query
|
||||
addressCurrentPage.value = 1
|
||||
return fetchAddresses()
|
||||
}
|
||||
|
||||
return {
|
||||
addresses,
|
||||
loading,
|
||||
error,
|
||||
currentPage,
|
||||
pageSize,
|
||||
totalItems,
|
||||
totalPages,
|
||||
searchQuery,
|
||||
filteredAddresses,
|
||||
paginatedAddresses,
|
||||
getAddressById,
|
||||
addAddress,
|
||||
updateAddress,
|
||||
deleteAddress,
|
||||
setPage,
|
||||
setSearchQuery,
|
||||
resetPagination,
|
||||
addressLoading,
|
||||
addressError,
|
||||
addressSearchQuery,
|
||||
addressCurrentPage,
|
||||
addressPageSize,
|
||||
totalAddressItems,
|
||||
totalAddressPages,
|
||||
fetchAddresses,
|
||||
setAddressPage,
|
||||
setAddressSearchQuery,
|
||||
getAddressTemplate
|
||||
}
|
||||
})
|
||||
return { addresses, loading, error, fetchAddresses, deleteAddress, getTemplate, createAddress, updateAddress }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user