Skip to content

Commit

Permalink
Add billing address hook to customer accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandroliynyk committed Jan 24, 2025
1 parent 747718d commit da6f4e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type {
MailingAddress,
RenderOrderStatusExtensionTarget,
} from '@shopify/ui-extensions/customer-account';

import {ExtensionHasNoFieldError, ScopeNotGrantedError} from '../errors';

import {useApi} from './api';
import {useSubscription} from './subscription';

/**
* Returns 'billingAddress' specified in the order.
*/
export function useBillingAddress<
Target extends RenderOrderStatusExtensionTarget = RenderOrderStatusExtensionTarget,
>(): MailingAddress | undefined {
const api = useApi<Target>();
const extensionTarget = api.extension.target;

if (!('billingAddress' in api)) {
throw new ExtensionHasNoFieldError('billingAddress', extensionTarget);
}

const billingAddress = api.billingAddress;

if (!billingAddress) {
throw new ScopeNotGrantedError(
'Using billing address requires having billing address permissions granted to your app.',
);
}

return useSubscription(billingAddress);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {useBillingAddress} from './billing-address';
export {useExtensionApi, useApi} from './api';
export {useCurrency} from './currency';
export {useLanguage} from './language';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {MailingAddress} from '@shopify/ui-extensions/customer-account';

import {useBillingAddress} from '..';

import {mount, createMockStatefulRemoteSubscribable} from './mount';

describe('useBillingAddress', () => {
it('returns latest billing address', async () => {
const address: MailingAddress = {countryCode: 'CA'};
const extensionApi = {
extension: {
target: 'customer-account.order-status.block.render' as const,
},
billingAddress: createMockStatefulRemoteSubscribable(address),
};

const {value} = mount.hook(() => useBillingAddress(), {extensionApi});

expect(value).toMatchObject(address);
});
});

0 comments on commit da6f4e1

Please sign in to comment.