Skip to content

Commit

Permalink
Remove cart Element (#487)
Browse files Browse the repository at this point in the history
* remove cart Element code and types
  • Loading branch information
tylersmith-stripe authored Apr 2, 2024
1 parent 86eaf3f commit 53bcc21
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 570 deletions.
165 changes: 0 additions & 165 deletions examples/hooks/10-Cart.js

This file was deleted.

24 changes: 1 addition & 23 deletions src/components/Elements.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import React, {StrictMode} from 'react';
import {render, act} from '@testing-library/react';
import {renderHook} from '@testing-library/react-hooks';

import {
Elements,
useElements,
ElementsConsumer,
useCartElement,
useCartElementState,
} from './Elements';
import {Elements, useElements, ElementsConsumer} from './Elements';
import * as mocks from '../../test/mocks';
import {useStripe} from './useStripe';

Expand Down Expand Up @@ -300,22 +294,6 @@ describe('Elements', () => {
);
});

test('throws when trying to call useCartElement outside of Elements context', () => {
const {result} = renderHook(() => useCartElement());

expect(result.error && result.error.message).toBe(
'Could not find Elements context; You need to wrap the part of your app that calls useCartElement() in an <Elements> provider.'
);
});

test('throws when trying to call useCartElementState outside of Elements context', () => {
const {result} = renderHook(() => useCartElementState());

expect(result.error && result.error.message).toBe(
'Could not find Elements context; You need to wrap the part of your app that calls useCartElementState() in an <Elements> provider.'
);
});

describe('React.StrictMode', () => {
test('creates elements twice in StrictMode', () => {
const TestComponent = () => {
Expand Down
79 changes: 1 addition & 78 deletions src/components/Elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,6 @@ export const parseElementsContext = (
return ctx;
};

interface CartElementContextValue {
cart: stripeJs.StripeCartElement | null;
cartState: stripeJs.StripeCartElementPayloadEvent | null;
setCart: (cart: stripeJs.StripeCartElement | null) => void;
setCartState: (
cartState: stripeJs.StripeCartElementPayloadEvent | null
) => void;
}

const CartElementContext = React.createContext<CartElementContextValue | null>(
null
);
CartElementContext.displayName = 'CartElementContext';

export const parseCartElementContext = (
ctx: CartElementContextValue | null,
useCase: string
): CartElementContextValue => {
if (!ctx) {
throw new Error(
`Could not find Elements context; You need to wrap the part of your app that ${useCase} in an <Elements> provider.`
);
}

return ctx;
};

interface ElementsProps {
/**
* A [Stripe object](https://stripe.com/docs/js/initializing) or a `Promise` resolving to a `Stripe` object.
Expand Down Expand Up @@ -110,14 +83,6 @@ export const Elements: FunctionComponent<PropsWithChildren<ElementsProps>> = (({
rawStripeProp,
]);

const [cart, setCart] = React.useState<stripeJs.StripeCartElement | null>(
null
);
const [
cartState,
setCartState,
] = React.useState<stripeJs.StripeCartElementPayloadEvent | null>(null);

// For a sync stripe instance, initialize into context
const [ctx, setContext] = React.useState<ElementsContextValue>(() => ({
stripe: parsed.tag === 'sync' ? parsed.stripe : null,
Expand Down Expand Up @@ -191,13 +156,7 @@ export const Elements: FunctionComponent<PropsWithChildren<ElementsProps>> = (({
}, [ctx.stripe]);

return (
<ElementsContext.Provider value={ctx}>
<CartElementContext.Provider
value={{cart, setCart, cartState, setCartState}}
>
{children}
</CartElementContext.Provider>
</ElementsContext.Provider>
<ElementsContext.Provider value={ctx}>{children}</ElementsContext.Provider>
);
}) as FunctionComponent<PropsWithChildren<ElementsProps>>;

Expand All @@ -213,24 +172,6 @@ export const useElementsContextWithUseCase = (
return parseElementsContext(ctx, useCaseMessage);
};

const DUMMY_CART_ELEMENT_CONTEXT: CartElementContextValue = {
cart: null,
cartState: null,
setCart: () => {},
setCartState: () => {},
};

export const useCartElementContextWithUseCase = (
useCaseMessage: string,
isInCustomCheckout = false
): CartElementContextValue => {
const ctx = React.useContext(CartElementContext);
if (isInCustomCheckout) {
return DUMMY_CART_ELEMENT_CONTEXT;
}
return parseCartElementContext(ctx, useCaseMessage);
};

/**
* @docs https://stripe.com/docs/stripe-js/react#useelements-hook
*/
Expand All @@ -239,24 +180,6 @@ export const useElements = (): stripeJs.StripeElements | null => {
return elements;
};

/**
* @docs https://stripe.com/docs/payments/checkout/cart-element
*/
export const useCartElement = (): stripeJs.StripeCartElement | null => {
const {cart} = useCartElementContextWithUseCase('calls useCartElement()');
return cart;
};

/**
* @docs https://stripe.com/docs/payments/checkout/cart-element
*/
export const useCartElementState = (): stripeJs.StripeCartElementPayloadEvent | null => {
const {cartState} = useCartElementContextWithUseCase(
'calls useCartElementState()'
);
return cartState;
};

interface ElementsConsumerProps {
children: (props: ElementsContextValue) => ReactNode;
}
Expand Down
Loading

0 comments on commit 53bcc21

Please sign in to comment.