All files / app/shipping/shippingOption ShippingOptions.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95  14x   14x 14x   14x                                     14x 17x 17x 16x       14x 17x 17x 17x 17x 17x   17x 17x                       17x         17x 17x 17x 17x 17x         34x   17x 17x 17x 17x 17x   17x       17x 17x   17x                       14x  
import { Cart, CheckoutSelectors, Consignment } from '@bigcommerce/checkout-sdk';
import { createSelector } from 'reselect';
 
import { withCheckout, CheckoutContextProps } from '../../checkout';
import getShippingMethodId from '../getShippingMethodId';
 
import ShippingOptionsForm from './ShippingOptionsForm';
 
export interface ShippingOptionsProps {
    isMultiShippingMode: boolean;
    isUpdatingAddress?: boolean;
    shouldShowShippingOptions: boolean;
}
 
export interface WithCheckoutShippingOptionsProps {
    invalidShippingMessage: string;
    methodId?: string;
    consignments?: Consignment[];
    cart: Cart;
    isSelectingShippingOption(consignmentId?: string): boolean;
    subscribeToConsignments(subscriber: (state: CheckoutSelectors) => void): () => void;
    selectShippingOption(consignmentId: string, optionId: string): Promise<CheckoutSelectors>;
    isLoading(consignmentId?: string): boolean;
}
 
const subscribeToConsignmentsSelector = createSelector(
    ({ checkoutService }: CheckoutContextProps) => checkoutService.subscribe,
    subscribe => (subscriber: (state: CheckoutSelectors) => void) => {
        return subscribe(subscriber, ({ data }) => data.getConsignments());
    }
);
 
const isLoadingSelector = createSelector(
    (_: CheckoutSelectors, { isUpdatingAddress }: ShippingOptionsProps) => isUpdatingAddress,
    ({ statuses }: CheckoutSelectors) => statuses.isLoadingShippingOptions,
    ({ statuses }: CheckoutSelectors) => statuses.isSelectingShippingOption,
    ({ statuses }: CheckoutSelectors) => statuses.isUpdatingConsignment,
    ({ statuses }: CheckoutSelectors) => statuses.isCreatingConsignments,
    (isUpdatingAddress, isLoadingShippingOptions, isSelectingShippingOption, isUpdatingConsignment, isCreatingConsignments) => {
        return (consignmentId?: string) => {
            return (
                isUpdatingAddress ||
                isLoadingShippingOptions() ||
                isSelectingShippingOption(consignmentId) ||
                isUpdatingConsignment(consignmentId) ||
                isCreatingConsignments()
            );
        };
    }
);
 
function mapToShippingOptions(
    { checkoutService, checkoutState }: CheckoutContextProps,
    props: ShippingOptionsProps
): WithCheckoutShippingOptionsProps | null {
    const {
        data: {
            getCart,
            getConsignments,
            getConfig,
            getCustomer,
            getCheckout,
        },
        statuses: {
            isSelectingShippingOption,
        },
    } = checkoutState;
 
    const consignments = getConsignments() || [];
    const customer = getCustomer();
    const cart = getCart();
    const config = getConfig();
    const checkout = getCheckout();
 
    Iif (!config || !checkout || !customer || !cart) {
        return null;
    }
 
    const methodId = getShippingMethodId(checkout);
    const { shippingQuoteFailedMessage } = config.checkoutSettings;
 
    return {
        cart,
        consignments,
        invalidShippingMessage: shippingQuoteFailedMessage,
        isLoading: isLoadingSelector(checkoutState, props),
        isSelectingShippingOption,
        methodId,
        selectShippingOption: checkoutService.selectConsignmentShippingOption,
        subscribeToConsignments: subscribeToConsignmentsSelector({ checkoutService, checkoutState }),
    };
}
 
export default withCheckout(mapToShippingOptions)(ShippingOptionsForm);