Skip to content

Commit

Permalink
fix(shipping): CHECKOUT-4509 Always save shipping form after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Sanchez committed Oct 28, 2019
1 parent 59dc6a2 commit 8269e75
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/app/shipping/ShippingForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Cart, CheckoutSelectors, Consignment, ConsignmentAssignmentRequestBody, Country, CustomerAddress, CustomerRequestOptions, FormField, ShippingInitializeOptions, ShippingRequestOptions } from '@bigcommerce/checkout-sdk';
import { Address, Cart, CheckoutParams, CheckoutSelectors, Consignment, ConsignmentAssignmentRequestBody, Country, CustomerAddress, CustomerRequestOptions, FormField, RequestOptions, ShippingInitializeOptions, ShippingRequestOptions } from '@bigcommerce/checkout-sdk';
import React, { Component, ReactNode } from 'react';

import { withLanguage, WithLanguageProps } from '../locale';
Expand Down Expand Up @@ -33,7 +33,7 @@ export interface ShippingFormProps {
onUnhandledError(error: Error): void;
onUseNewAddress(address: Address, itemId: string): void;
signOut(options?: CustomerRequestOptions): void;
updateAddress(address: Partial<Address>): Promise<CheckoutSelectors>;
updateAddress(address: Partial<Address>, options: RequestOptions<CheckoutParams>): Promise<CheckoutSelectors>;
}

class ShippingForm extends Component<ShippingFormProps & WithLanguageProps> {
Expand Down
22 changes: 19 additions & 3 deletions src/app/shipping/SingleShippingForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ describe('SingleShippingForm', () => {
expect(defaultProps.updateAddress).toHaveBeenCalledWith({
...getShippingAddress(),
address1: 'foo 2',
});
},
{
params: {
include: {
'consignments.availableShippingOptions': true,
},
},
});
done();
}, SHIPPING_AUTOSAVE_DELAY * 1.1);
});
Expand Down Expand Up @@ -86,12 +93,21 @@ describe('SingleShippingForm', () => {
}, SHIPPING_AUTOSAVE_DELAY * 1.1);
});

it('does not call updateAddress if modified field does not affect shipping', done => {
it('calls updateAddress without including shipping options if modified field does not affect shipping', done => {
component.find('input[name="shippingAddress.address2"]')
.simulate('change', { target: { value: 'foo 1', name: 'shippingAddress.address2' } });

setTimeout(() => {
expect(defaultProps.updateAddress).not.toHaveBeenCalled();
expect(defaultProps.updateAddress).toHaveBeenCalledWith({
...getShippingAddress(),
address2: 'foo 1',
}, {
params: {
include: {
'consignments.availableShippingOptions': false,
},
},
});
done();
}, SHIPPING_AUTOSAVE_DELAY * 1.1);
});
Expand Down
24 changes: 15 additions & 9 deletions src/app/shipping/SingleShippingForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, CheckoutSelectors, Consignment, Country, CustomerAddress, CustomerRequestOptions, FormField, ShippingInitializeOptions, ShippingRequestOptions } from '@bigcommerce/checkout-sdk';
import { Address, CheckoutParams, CheckoutSelectors, Consignment, Country, CustomerAddress, CustomerRequestOptions, FormField, RequestOptions, ShippingInitializeOptions, ShippingRequestOptions } from '@bigcommerce/checkout-sdk';
import { withFormik, FormikProps } from 'formik';
import { debounce, noop } from 'lodash';
import React, { PureComponent, ReactNode } from 'react';
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface SingleShippingFormProps {
onSubmit(values: SingleShippingFormValues): void;
onUnhandledError?(error: Error): void;
signOut(options?: CustomerRequestOptions): void;
updateAddress(address: Partial<Address>): Promise<CheckoutSelectors>;
updateAddress(address: Partial<Address>, options?: RequestOptions<CheckoutParams>): Promise<CheckoutSelectors>;
}

export interface SingleShippingFormValues {
Expand Down Expand Up @@ -63,9 +63,15 @@ class SingleShippingForm extends PureComponent<SingleShippingFormProps & WithLan

const { updateAddress } = this.props;

this.debouncedUpdateAddress = debounce(async (address: Address) => {
this.debouncedUpdateAddress = debounce(async (address: Address, includeShippingOptions: boolean) => {
try {
await updateAddress(address);
await updateAddress(address, {
params: {
include: {
'consignments.availableShippingOptions': includeShippingOptions,
},
},
});
} finally {
this.setState({ isUpdatingShippingData: false });
}
Expand Down Expand Up @@ -143,7 +149,7 @@ class SingleShippingForm extends PureComponent<SingleShippingFormProps & WithLan
const { isValid } = this.props;

if (!prevIsValid && isValid) {
this.updateAddressWithFormData();
this.updateAddressWithFormData(true);
}
}

Expand Down Expand Up @@ -182,14 +188,14 @@ class SingleShippingForm extends PureComponent<SingleShippingFormProps & WithLan

const { isValid } = this.props;

if (!isValid || !isShippingField) {
if (!isValid) {
return;
}

this.updateAddressWithFormData();
this.updateAddressWithFormData(isShippingField);
};

private updateAddressWithFormData() {
private updateAddressWithFormData(includeShippingOptions: boolean) {
const {
shippingAddress,
values: { shippingAddress: addressForm },
Expand All @@ -202,7 +208,7 @@ class SingleShippingForm extends PureComponent<SingleShippingFormProps & WithLan
}

this.setState({ isUpdatingShippingData: true });
this.debouncedUpdateAddress(updatedShippingAddress);
this.debouncedUpdateAddress(updatedShippingAddress, includeShippingOptions);
}

private handleAddressSelect: (
Expand Down

0 comments on commit 8269e75

Please sign in to comment.