-
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.
Merge pull request #2583 from Shopify/add-billing-address-hook
Add billing address hook to customer accounts
- Loading branch information
Showing
5 changed files
with
83 additions
and
19 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
35 changes: 35 additions & 0 deletions
35
...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,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); | ||
}); | ||
}); |
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
19 changes: 0 additions & 19 deletions
19
...s/ui-extensions-react/src/surfaces/customer-account/hooks/tests/shopping-address.test.tsx
This file was deleted.
Oops, something went wrong.