-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add billing address hook to customer accounts
- Loading branch information
1 parent
747718d
commit da6f4e1
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/ui-extensions-react/src/surfaces/customer-account/hooks/billing-address.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/ui-extensions-react/src/surfaces/customer-account/hooks/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...ges/ui-extensions-react/src/surfaces/customer-account/hooks/tests/billing-address.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |