Skip to content

Commit

Permalink
Fix addresses not having preferred address first. (#1051)
Browse files Browse the repository at this point in the history
* Fix addresses not having preferred address first.

* Include all addresses, not just preferred address twice.

* Correctly include preferred address.
  • Loading branch information
wjhsf authored Mar 12, 2023
1 parent fc789fe commit c72bb52
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ export default function useCustomer() {

/** Returns the customer's saved addresses with the 'preferred' address in the first index */
get addresses() {
// TODO: This performs array manipulation every time it is accessed; should it be
// changed to only execute once and save the result?
if (!customer?.addresses) {
return undefined
}
const preferredAddressIndex = customer.addresses.find((addr) => addr.preferred)
if (preferredAddressIndex > -1) {
return [
customer.addresses[preferredAddressIndex],
customer.addresses.slice(preferredAddressIndex, preferredAddressIndex + 1)
]
}
return customer.addresses
// Cloned so that we can manipulate the order
const addresses = [...customer.addresses]
const preferredIndex = addresses.findIndex((addr) => addr.preferred)
if (preferredIndex === -1) return addresses
const [preferred] = addresses.splice(preferredIndex, 1)
return [preferred, ...addresses]
},

/**
Expand Down

0 comments on commit c72bb52

Please sign in to comment.