Skip to content

Commit

Permalink
Merge pull request #2583 from Shopify/add-billing-address-hook
Browse files Browse the repository at this point in the history
Add billing address hook to customer accounts
  • Loading branch information
oleksandroliynyk authored Jan 27, 2025
2 parents e739ca6 + f8e9ab7 commit 055e9c5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 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,35 @@
import {MailingAddress} from '@shopify/ui-extensions/customer-account';

import {useBillingAddress} from '..';

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

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);
});

it('raises an exception without approval scopes granted', () => {
expect(() =>
mount.hook(() => useBillingAddress(), {
extensionApi: {
extension: {
target: 'customer-account.order-status.block.render' as const,
},
billingAddress: undefined,
},
}),
).toThrow(ScopeNotGrantedError);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {MailingAddress} from '@shopify/ui-extensions/customer-account';
import {useShippingAddress} from '..';

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

describe('useShippingAddress', () => {
it('returns latest shipping address', async () => {
Expand All @@ -18,4 +19,17 @@ describe('useShippingAddress', () => {

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

it('raises an exception without approval scopes granted', () => {
expect(() =>
mount.hook(() => useShippingAddress(), {
extensionApi: {
extension: {
target: 'customer-account.order-status.block.render' as const,
},
shippingAddress: undefined,
},
}),
).toThrow(ScopeNotGrantedError);
});
});

This file was deleted.

0 comments on commit 055e9c5

Please sign in to comment.