Skip to content

Commit

Permalink
Merge pull request #2371 from Shopify/checkout-unstable-release
Browse files Browse the repository at this point in the history
Unstable Release (checkout)
  • Loading branch information
hheyhhay authored Sep 27, 2024
2 parents 94b47bb + c9620ac commit be96113
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .changeset/breezy-mangos-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/ui-extensions-react': minor
'@shopify/ui-extensions': minor
---

Adds `type` property to `selectedPaymentOption`
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {
reactExtension,
ConsentCheckbox,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {
reactExtension,
BlockStack,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {
reactExtension,
ConsentPhoneField,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {
reactExtension,
BlockStack,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {
reactExtension,
Select,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
RenderExtensionTarget,
PaymentOption,
SelectedPaymentOption,
} from '@shopify/ui-extensions/checkout';
import {useMemo} from 'react';

import {ExtensionHasNoMethodError} from '../errors';

Expand Down Expand Up @@ -32,34 +32,15 @@ export function useAvailablePaymentOptions<
*/
export function useSelectedPaymentOptions<
Target extends RenderExtensionTarget = RenderExtensionTarget,
>(): PaymentOption[] {
>(): SelectedPaymentOption[] {
const api = useApi<Target>();

if (
!('selectedPaymentOptions' in api) ||
!('availablePaymentOptions' in api)
) {
throw new ExtensionHasNoMethodError(
'selectedPaymentOptions',
api.extension.target,
);
if ('selectedPaymentOptions' in api) {
return useSubscription(api.selectedPaymentOptions);
}

const selectedPaymentOptions = useSubscription(api.selectedPaymentOptions);
const availablePaymentOptions = useSubscription(api.availablePaymentOptions);

return useMemo(() => {
const availablePaymentOptionsMap: Record<string, PaymentOption> = {};

for (const option of availablePaymentOptions) {
availablePaymentOptionsMap[option.handle] = option;
}

return selectedPaymentOptions.map((paymentOption) => {
return {
handle: paymentOption.handle,
type: availablePaymentOptionsMap[paymentOption.handle]?.type,
};
});
}, [availablePaymentOptions, selectedPaymentOptions]);
throw new ExtensionHasNoMethodError(
'selectedPaymentOptions',
api.extension.target,
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {createRender} from '@quilted/react-testing';
import type {
ApiForExtension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ describe('Payment Methods API hooks', () => {
];

const selectedPaymentOptionHandles: SelectedPaymentOption[] = [
{handle: 'bogus'},
{handle: 'bogus', type: 'other'},
];

const selectedPaymentOptions: PaymentOption[] = [
const selectedPaymentOptions: SelectedPaymentOption[] = [
{handle: 'bogus', type: 'other'},
];

Expand All @@ -70,11 +70,11 @@ describe('Payment Methods API hooks', () => {
];

const selectedPaymentOptionHandles: SelectedPaymentOption[] = [
{handle: 'bogus'},
{handle: 'direct'},
{handle: 'bogus', type: 'other'},
{handle: 'direct', type: 'creditCard'},
];

const selectedPaymentOptions: PaymentOption[] = [
const selectedPaymentOptions: SelectedPaymentOption[] = [
{handle: 'bogus', type: 'other'},
{handle: 'direct', type: 'creditCard'},
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import {useTranslate} from '../translate';

import {mount} from './mount';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"jsx": "react"
"jsx": "react-jsx"
},
"include": ["./**/*"],
"exclude": []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';

import {
getExample,
getLinksByTag,
STANDARD_API_PROPERTIES_DESCRIPTION,
} from '../helper.docs';
Expand All @@ -24,6 +25,7 @@ const data: ReferenceEntityTemplateSchema = {
type: 'UseStorageGeneratedType',
},
],
defaultExample: getExample('storage', ['jsx']),
related: getLinksByTag('apis'),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect} from 'react';
import {useState, useEffect} from 'react';
import {
Banner,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect} from 'react';
import {useState, useEffect} from 'react';
import {
Banner,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react';
import {useState} from 'react';
import {
reactExtension,
useCustomerPrivacy,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {
BlockStack,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {
BlockStack,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {
BlockStack,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {
BlockStack,
reactExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {useEffect, useState} from 'react';
import {
reactExtension,
Checkbox,
useApi,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension(
'purchase.checkout.block.render',
() => <Extension />,
);

function Extension() {
const {storage} = useApi();
const [tosConsent, setTosConsent] =
useState(false);

useEffect(() => {
async function readFromStorage() {
const tosConsent = await storage.read(
'tos-consent',
);

setTosConsent(Boolean(tosConsent));
}

readFromStorage();
}, [storage]);

async function cacheConsent(value: boolean) {
setTosConsent(value);
await storage.write('tos-consent', value);
}

return (
<Checkbox
id="tos"
name="tos"
onChange={cacheConsent}
checked={tosConsent}
>
I agree with the terms of service
</Checkbox>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ The contents of the token are signed using your shared app secret. The optional
],
},
},
storage: {
description: '',
codeblock: {
title: 'Storage',
tabs: getExtensionCodeTabs('storage'),
},
},
'delivery-groups': {
description: '',
codeblock: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const data: ReferenceEntityTemplateSchema = {
name: 'purchase.thank-you.cart-line-item.render-after',
description: `A static extension target that renders on every line item, inside the details under the line item properties element.
> Caution:
> This extension target will not be rendered if the line item is a custom line item belonging to a draft order invoice.
`,
> Caution:
> This extension target will not be rendered if the line item is a custom line item belonging to a draft order invoice.
`,
defaultExample: getExample(
'purchase.thank-you.cart-line-item.render-after/default',
['jsx', 'js'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,17 +1167,7 @@ export interface PaymentOption {
handle: string;
}

/**
* A payment option selected by the buyer.
*/
export interface SelectedPaymentOption {
/**
* The unique handle referencing `PaymentOption.handle`.
*
* Refer to [availablePaymentOptions](https://shopify.dev/docs/api/checkout-ui-extensions/apis/payments#standardapi-propertydetail-availablepaymentoptions).
*/
handle: string;
}
export type SelectedPaymentOption = PaymentOption;

export interface CartDiscountCode {
/**
Expand Down

0 comments on commit be96113

Please sign in to comment.