-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring in @Shopify/checkout-ui-extensions@0.22.0
- Loading branch information
Showing
16 changed files
with
282 additions
and
10 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
'@shopify/ui-extensions': patch | ||
'@shopify/ui-extensions-react': patch | ||
--- | ||
|
||
Bring in @shopify/checkout-ui-extensions@0.22.0 |
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
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
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
42 changes: 42 additions & 0 deletions
42
packages/ui-extensions-react/src/surfaces/checkout/hooks/discounts.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,42 @@ | ||
import type { | ||
CartDiscountAllocation, | ||
CartDiscountCode, | ||
StandardApi, | ||
RenderExtensionPoint, | ||
} from '@shopify/ui-extensions/checkout'; | ||
|
||
import {useExtensionApi} from './api'; | ||
import {useSubscription} from './subscription'; | ||
|
||
/** | ||
* Returns the current discount codes applied to the cart, and automatically re-renders | ||
* your component if discount codes are added or removed. | ||
*/ | ||
export function useDiscountCodes< | ||
ID extends RenderExtensionPoint = RenderExtensionPoint, | ||
>(): CartDiscountCode[] { | ||
const {discountCodes} = useExtensionApi<ID>(); | ||
|
||
return useSubscription(discountCodes); | ||
} | ||
|
||
/** | ||
* Returns the current discount allocations applied to the cart, and automatically re-renders | ||
* your component if discount allocations changed. | ||
*/ | ||
export function useDiscountAllocations< | ||
ID extends RenderExtensionPoint = RenderExtensionPoint, | ||
>(): CartDiscountAllocation[] { | ||
const {discountAllocations} = useExtensionApi<ID>(); | ||
|
||
return useSubscription(discountAllocations); | ||
} | ||
|
||
/** | ||
* Returns a function to add or remove discount codes. | ||
*/ | ||
export function useApplyDiscountCodeChange< | ||
ID extends RenderExtensionPoint = RenderExtensionPoint, | ||
>(): StandardApi<ID>['applyDiscountCodeChange'] { | ||
return useExtensionApi<ID>().applyDiscountCodeChange; | ||
} |
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
70 changes: 70 additions & 0 deletions
70
packages/ui-extensions-react/src/surfaces/checkout/hooks/tests/discounts.test.tsx
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,70 @@ | ||
import { | ||
CartDiscountCode, | ||
CartDiscountAllocation, | ||
} from '@shopify/ui-extensions/checkout'; | ||
|
||
import {useDiscountAllocations, useDiscountCodes} from '../discounts'; | ||
|
||
import {mount, createMockStatefulRemoteSubscribable} from './mount'; | ||
import type {PartialExtensionApi} from './mount'; | ||
|
||
describe('Discounts API hooks', () => { | ||
describe('useDiscountCodes', () => { | ||
it('returns the current discount codes', async () => { | ||
const discountCodes: CartDiscountCode[] = [ | ||
{code: '20off'}, | ||
{code: 'free_shipping'}, | ||
]; | ||
|
||
const extensionApi: PartialExtensionApi = { | ||
discountCodes: createMockStatefulRemoteSubscribable(discountCodes), | ||
}; | ||
|
||
const {value} = mount.hook(() => useDiscountCodes(), {extensionApi}); | ||
|
||
expect(value).toBe(discountCodes); | ||
}); | ||
}); | ||
|
||
describe('useDiscountAllocations', () => { | ||
it('returns the current discount allocations', async () => { | ||
const discountAllocations: CartDiscountAllocation[] = [ | ||
{ | ||
code: '20off', | ||
discountedAmount: { | ||
amount: 20, | ||
currencyCode: 'USD', | ||
}, | ||
type: 'code', | ||
}, | ||
{ | ||
title: '10% off', | ||
discountedAmount: { | ||
amount: 10, | ||
currencyCode: 'USD', | ||
}, | ||
type: 'automatic', | ||
}, | ||
{ | ||
title: '15% off', | ||
discountedAmount: { | ||
amount: 15, | ||
currencyCode: 'USD', | ||
}, | ||
type: 'custom', | ||
}, | ||
]; | ||
|
||
const extensionApi: PartialExtensionApi = { | ||
discountAllocations: | ||
createMockStatefulRemoteSubscribable(discountAllocations), | ||
}; | ||
|
||
const {value} = mount.hook(() => useDiscountAllocations(), { | ||
extensionApi, | ||
}); | ||
|
||
expect(value).toBe(discountAllocations); | ||
}); | ||
}); | ||
}); |
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
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
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
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
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
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
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
Oops, something went wrong.