Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix addresses not having preferred address first. #1051

Merged
merged 3 commits into from
Mar 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be preferred instead of [preferred]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.splice returns an array of removed items, so we need to destructure! (Alternatively, we could not destructure and spread it in the return value: [...preferred, ...addresses].)

return [preferred, ...addresses]
},

/**
Expand Down