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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 22x 15x 22x 22x 15x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 5x 19x 1x 20x 22x 18x 18x 18x 18x 18x 18x 16x 2x 2x 2x 2x 2x 4x 2x 15x 6x 15x 6x 6x 15x 20x 15x 23x 23x 23x 23x 23x | import { CheckoutSelectors, Consignment } from '@bigcommerce/checkout-sdk'; import { withFormik, FormikProps } from 'formik'; import { noop } from 'lodash'; import React, { PureComponent, ReactNode } from 'react'; import { StaticAddress } from '../../address'; import { TranslatedString } from '../../locale'; import { LoadingOverlay } from '../../ui/loading'; import getRecommendedShippingOption from '../getRecommendedShippingOption'; import StaticConsignmentItemList from '../StaticConsignmentItemList'; import { ShippingOptionsProps, WithCheckoutShippingOptionsProps } from './ShippingOptions'; import './ShippingOptionsForm.scss'; import ShippingOptionsList from './ShippingOptionsList'; export type ShippingOptionsFormProps = ShippingOptionsProps & WithCheckoutShippingOptionsProps; class ShippingOptionsForm extends PureComponent<ShippingOptionsFormProps & FormikProps<ShippingOptionsFormValues>> { private unsubscribe?: () => void; componentDidMount(): void { const { subscribeToConsignments } = this.props; this.unsubscribe = subscribeToConsignments(this.selectDefaultShippingOptions); } componentWillUnmount(): void { if (this.unsubscribe) { this.unsubscribe(); this.unsubscribe = undefined; } } render(): ReactNode { const { consignments, isMultiShippingMode, selectShippingOption, isLoading, shouldShowShippingOptions, invalidShippingMessage, methodId, } = this.props; if (!consignments || !consignments.length || !shouldShowShippingOptions ) { return ( <LoadingOverlay isLoading={ isLoading() }> { this.renderNoShippingOptions( <TranslatedString id={ methodId || isMultiShippingMode ? 'shipping.select_shipping_address_text' : 'shipping.enter_shipping_address_text' } /> ) } </LoadingOverlay> ); } return <> { consignments .slice(0, isMultiShippingMode ? undefined : 1) .sort((a, b) => (a.id > b.id ? -1 : 1)) .map(consignment => ( <div className="shippingOptions-container form-fieldset" key={ consignment.id }> { isMultiShippingMode && this.renderConsignment(consignment) } <ShippingOptionsList consignmentId={ consignment.id } inputName={ getRadioInputName(consignment.id) } isLoading={ isLoading(consignment.id) } onSelectedOption={ selectShippingOption } selectedShippingOptionId={ consignment.selectedShippingOption && consignment.selectedShippingOption.id } shippingOptions={ consignment.availableShippingOptions } /> { (!consignment.availableShippingOptions || !consignment.availableShippingOptions.length) && <LoadingOverlay hideContentWhenLoading isLoading={ isLoading(consignment.id) }> { this.renderNoShippingOptions(invalidShippingMessage) } </LoadingOverlay> } </div> )) } </>; } private selectDefaultShippingOptions: (state: CheckoutSelectors) => void = async ({ data }) => { const { selectShippingOption, setFieldValue } = this.props; const consignment = (data.getConsignments() || []).find(({ selectedShippingOption, availableShippingOptions: shippingOptions, }) => !selectedShippingOption && shippingOptions); if (!consignment || !consignment.availableShippingOptions) { return; } const { availableShippingOptions, id } = consignment; const recommendedOption = getRecommendedShippingOption(availableShippingOptions); const singleShippingOption = availableShippingOptions.length === 1 && availableShippingOptions[0]; const defaultShippingOption = recommendedOption || singleShippingOption; Iif (!defaultShippingOption) { return; } await selectShippingOption(id, defaultShippingOption.id); setFieldValue(`shippingOptionIds.${id}`, defaultShippingOption.id); }; private renderNoShippingOptions(message: ReactNode): ReactNode { return ( <div className="shippingOptions-panel optimizedCheckout-overlay"> <p className="shippingOptions-panel-message optimizedCheckout-primaryContent"> { message } </p> </div> ); } private renderConsignment(consignment: Consignment): ReactNode { const { cart } = this.props; return ( <div className="staticConsignment"> <strong> <TranslatedString id="shipping.shipping_address_heading" /> </strong> <StaticAddress address={ consignment.shippingAddress } /> <StaticConsignmentItemList cart={ cart } consignment={ consignment } /> </div> ); } } function getRadioInputName(consignmentId: string): string { return `shippingOptionIds.${consignmentId}`; } export interface ShippingOptionsFormValues { shippingOptionIds: { [shippingOptionIds: string]: string; }; } export default withFormik<ShippingOptionsFormProps, ShippingOptionsFormValues>({ handleSubmit: noop, mapPropsToValues({ consignments }) { const shippingOptionIds: { [id: string]: string } = {}; (consignments || []).forEach(consignment => { shippingOptionIds[consignment.id] = consignment.selectedShippingOption ? consignment.selectedShippingOption.id : ''; }); return { shippingOptionIds }; }, })(ShippingOptionsForm); |