swipe to delete address
This commit is contained in:
@@ -127,18 +127,46 @@ final class ApiService {
|
||||
}
|
||||
|
||||
func addCustomerAddress(_ address: CustomerAddress) async throws -> ApiEnvelope<CustomerProfile> {
|
||||
return try await saveCustomerAddress(address, replacingAddressId: nil)
|
||||
}
|
||||
|
||||
func saveCustomerAddress(_ address: CustomerAddress, replacingAddressId: String?) async throws -> ApiEnvelope<CustomerProfile> {
|
||||
let currentProfile = try await profile()
|
||||
guard currentProfile.error == false, let customer = currentProfile.result else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
var addressBook = (customer.addressBook ?? []).map(CustomerAddressPayload.init(from:))
|
||||
addressBook.insert(CustomerAddressPayload(from: address), at: 0)
|
||||
let currentAddressBook = customer.addressBook ?? []
|
||||
var addressBook = currentAddressBook.map(CustomerAddressPayload.init(from:))
|
||||
let addressPayload = CustomerAddressPayload(from: address)
|
||||
|
||||
let payload = CustomerProfileUpdatePayload(addressBook: addressBook)
|
||||
let body = try JSONEncoder().encode(payload)
|
||||
let req = ApiRequest(path: "/api/customer/\(customer.id)", method: "POST", module: .customer, requiresAuth: true, body: body)
|
||||
return try await sendEnvelope(req)
|
||||
if let replacingAddressId,
|
||||
let replaceIndex = currentAddressBook.firstIndex(where: { $0.id == replacingAddressId }) {
|
||||
addressBook[replaceIndex] = addressPayload
|
||||
} else {
|
||||
addressBook.insert(addressPayload, at: 0)
|
||||
}
|
||||
|
||||
return try await updateCustomerAddressBook(customerId: customer.id, addressBook: addressBook)
|
||||
}
|
||||
|
||||
func deleteCustomerAddress(_ address: CustomerAddress) async throws -> ApiEnvelope<CustomerProfile> {
|
||||
let currentProfile = try await profile()
|
||||
guard currentProfile.error == false, let customer = currentProfile.result else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
var currentAddressBook = customer.addressBook ?? []
|
||||
if let targetId = address.id, targetId.isEmpty == false {
|
||||
if let index = currentAddressBook.firstIndex(where: { $0.id == targetId }) {
|
||||
currentAddressBook.remove(at: index)
|
||||
}
|
||||
} else if let index = currentAddressBook.firstIndex(where: { matchesAddress($0, address) }) {
|
||||
currentAddressBook.remove(at: index)
|
||||
}
|
||||
|
||||
let updatedAddressBook = currentAddressBook.map(CustomerAddressPayload.init(from:))
|
||||
return try await updateCustomerAddressBook(customerId: customer.id, addressBook: updatedAddressBook)
|
||||
}
|
||||
|
||||
func lookupZipCode(_ zipCode: String) async throws -> ApiEnvelope<CepLookupResult> {
|
||||
@@ -157,6 +185,24 @@ final class ApiService {
|
||||
return try await sendEnvelope(req)
|
||||
}
|
||||
|
||||
private func updateCustomerAddressBook(customerId: String, addressBook: [CustomerAddressPayload]) async throws -> ApiEnvelope<CustomerProfile> {
|
||||
let payload = CustomerProfileUpdatePayload(addressBook: addressBook)
|
||||
let body = try JSONEncoder().encode(payload)
|
||||
let req = ApiRequest(path: "/api/customer/\(customerId)", method: "POST", module: .customer, requiresAuth: true, body: body)
|
||||
return try await sendEnvelope(req)
|
||||
}
|
||||
|
||||
private func matchesAddress(_ lhs: CustomerAddress, _ rhs: CustomerAddress) -> Bool {
|
||||
lhs.label == rhs.label &&
|
||||
lhs.address == rhs.address &&
|
||||
lhs.number == rhs.number &&
|
||||
lhs.complement == rhs.complement &&
|
||||
lhs.neighborhood == rhs.neighborhood &&
|
||||
lhs.city == rhs.city &&
|
||||
lhs.state == rhs.state &&
|
||||
lhs.zipCode == rhs.zipCode
|
||||
}
|
||||
|
||||
// MARK: - Stores
|
||||
|
||||
func listPublicCategories() async throws -> ApiEnvelope<[PublicCategory]> {
|
||||
|
||||
Reference in New Issue
Block a user