From c72bb5261d95557413289b3228da7a91a15814c5 Mon Sep 17 00:00:00 2001 From: Will Harney <62956339+wjhsf@users.noreply.github.com> Date: Sun, 12 Mar 2023 11:48:36 -0400 Subject: [PATCH] Fix addresses not having preferred address first. (#1051) * Fix addresses not having preferred address first. * Include all addresses, not just preferred address twice. * Correctly include preferred address. --- .../app/commerce-api/hooks/useCustomer.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/template-retail-react-app/app/commerce-api/hooks/useCustomer.js b/packages/template-retail-react-app/app/commerce-api/hooks/useCustomer.js index 4404d86260..fe0cfe54fd 100644 --- a/packages/template-retail-react-app/app/commerce-api/hooks/useCustomer.js +++ b/packages/template-retail-react-app/app/commerce-api/hooks/useCustomer.js @@ -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] }, /**