Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Feature: Data Store Migration #6612

Merged
merged 23 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3b91c09
Convert checkout context to data store - part 1 (#6232)
alexflorisca Jun 10, 2022
5556e46
Move checkout state code into thunks and rename `CheckoutState` conte…
alexflorisca Jun 21, 2022
a103b8b
Fix lint issues
alexflorisca Jun 23, 2022
408bf0e
Fixed missing lint error
alexflorisca Jun 23, 2022
8b6a861
Convert validation context to data store (#6402)
Jul 1, 2022
d3de14f
Feature: Data Store Migration - Payments (#6619)
alexflorisca Jul 8, 2022
9d1bc89
Fix lint errors
opr Aug 15, 2022
698b230
Refactor registration of payment methods and update unit tests for pa…
opr Aug 22, 2022
3c41a63
Remove Notices context
alexflorisca Aug 22, 2022
e5ce213
Remove missing import
opr Aug 22, 2022
1857381
Remove all traces of Notification Context
alexflorisca Aug 22, 2022
96abc7d
Revert "Include @wordpress/redux-routine in transformIgnorePatterns j…
opr Aug 22, 2022
40e9628
Only allow payment methods to show that are in availablePaymentMethods
opr Aug 23, 2022
0cfe327
Update assets/js/blocks/cart-checkout-shared/payment-methods/payment-…
alexflorisca Aug 23, 2022
143b060
Fix docs linting errors
opr Aug 25, 2022
39451c2
Fix payment status being set to pristine not working correctly
alexflorisca Aug 26, 2022
051966a
Fix bug where active payment methods were checked too early
alexflorisca Sep 1, 2022
ca0faad
Remove unused properties from store select
alexflorisca Sep 1, 2022
d4c500a
Fix failing express payment method unit test (#7165)
opr Sep 21, 2022
1c9d6ab
Refactor `getValidationError` and `getValidationErrorId` selectors in…
opr Sep 22, 2022
1cc831e
Tests for the Checkout data store (#7044)
alexflorisca Sep 22, 2022
f761e76
Document the selectors for the wc/store/checkout data store (#7085)
nielslange Sep 23, 2022
9703a33
Rebuild package-lock
alexflorisca Sep 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { decodeEntities } from '@wordpress/html-entities';
import { SelectControl } from 'wordpress-components';
import { useEffect } from 'react';
import classnames from 'classnames';
import {
ValidationInputError,
useValidationContext,
} from '@woocommerce/base-context';
import { ValidationInputError } from '@woocommerce/base-components/validation-input-error';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { useDispatch, useSelect } from '@wordpress/data';

// Default option for select boxes.
const selectAnOption = {
Expand All @@ -32,10 +31,17 @@ const AttributeSelectControl = ( {
'woo-gutenberg-products-block'
),
} ) => {
const { getValidationError, setValidationErrors, clearValidationError } =
useValidationContext();
const errorId = attributeName;
const error = getValidationError( errorId ) || {};

const { setValidationErrors, clearValidationError } =
useDispatch( VALIDATION_STORE_KEY );

const { error } = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
error: store.getValidationError( errorId ) || {},
};
} );

useEffect( () => {
if ( value ) {
Expand Down Expand Up @@ -73,7 +79,7 @@ const AttributeSelectControl = ( {
className={ classnames(
'wc-block-components-product-add-to-cart-attribute-picker__select',
{
'has-error': error.message && ! error.hidden,
'has-error': error?.message && ! error?.hidden,
}
) }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
BillingStateInput,
ShippingStateInput,
} from '@woocommerce/base-components/state-input';
import { useValidationContext } from '@woocommerce/base-context';
import { useEffect, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { withInstanceId } from '@wordpress/compose';
Expand All @@ -22,6 +21,11 @@ import {
defaultAddressFields,
EnteredAddress,
} from '@woocommerce/settings';
import { useSelect, useDispatch } from '@wordpress/data';
import {
VALIDATION_STORE_KEY,
FieldValidationStatus,
} from '@woocommerce/block-data';

/**
* Internal dependencies
Expand All @@ -32,17 +36,20 @@ import prepareAddressFields from './prepare-address-fields';
// values without having set the country first, show an error.
const validateShippingCountry = (
values: EnteredAddress,
setValidationErrors: ( errors: Record< string, unknown > ) => void,
setValidationErrors: (
errors: Record< string, FieldValidationStatus >
) => void,
clearValidationError: ( error: string ) => void,
hasValidationError: boolean
): void => {
const validationErrorId = 'shipping-missing-country';
if (
! hasValidationError &&
! values.country &&
( values.city || values.state || values.postcode )
) {
setValidationErrors( {
'shipping-missing-country': {
[ validationErrorId ]: {
message: __(
'Please select a country to calculate rates.',
'woo-gutenberg-products-block'
Expand All @@ -52,7 +59,7 @@ const validateShippingCountry = (
} );
}
if ( hasValidationError && values.country ) {
clearValidationError( 'shipping-missing-country' );
clearValidationError( validationErrorId );
}
};

Expand Down Expand Up @@ -87,17 +94,16 @@ const AddressForm = ( {
type = 'shipping',
values,
}: AddressFormProps ): JSX.Element => {
const { getValidationError, setValidationErrors, clearValidationError } =
useValidationContext();
const validationErrorId = 'shipping-missing-country';
const { setValidationErrors, clearValidationError } =
useDispatch( VALIDATION_STORE_KEY );

const currentFields = useShallowEqual( fields );
const countryValidationError = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return store.getValidationError( validationErrorId );
} );

const countryValidationError = ( getValidationError(
'shipping-missing-country'
) || {} ) as {
message: string;
hidden: boolean;
};
const currentFields = useShallowEqual( fields );

const addressFormFields = useMemo( () => {
return prepareAddressFields(
Expand Down Expand Up @@ -125,14 +131,14 @@ const AddressForm = ( {
values,
setValidationErrors,
clearValidationError,
!! countryValidationError.message &&
! countryValidationError.hidden
!! countryValidationError?.message &&
! countryValidationError?.hidden
);
}
}, [
values,
countryValidationError.message,
countryValidationError.hidden,
countryValidationError?.message,
countryValidationError?.hidden,
setValidationErrors,
clearValidationError,
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { __ } from '@wordpress/i18n';
import Button from '@woocommerce/base-components/button';
import { useState } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { useValidationContext } from '@woocommerce/base-context';
import type { EnteredAddress, AddressFields } from '@woocommerce/settings';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -25,12 +26,18 @@ const ShippingCalculatorAddress = ( {
addressFields,
}: ShippingCalculatorAddressProps ): JSX.Element => {
const [ address, setAddress ] = useState( initialAddress );
const { hasValidationErrors, showAllValidationErrors } =
useValidationContext();
const { showAllValidationErrors } = useDispatch( VALIDATION_STORE_KEY );

const { hasValidationErrors } = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
hasValidationErrors: store.hasValidationErrors,
};
} );

const validateSubmit = () => {
showAllValidationErrors();
return ! hasValidationErrors;
return ! hasValidationErrors();
};

return (
Expand Down
35 changes: 21 additions & 14 deletions assets/js/base/components/cart-checkout/totals/coupon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import { __ } from '@wordpress/i18n';
import { useState, useEffect, useRef } from '@wordpress/element';
import Button from '@woocommerce/base-components/button';
import { ValidatedTextInput } from '@woocommerce/base-components/text-input';
import { Panel } from '@woocommerce/blocks-checkout';
import Label from '@woocommerce/base-components/label';
import LoadingMask from '@woocommerce/base-components/loading-mask';
import { withInstanceId } from '@wordpress/compose';
import {
ValidationInputError,
useValidationContext,
} from '@woocommerce/base-context';
import { Panel } from '@woocommerce/blocks-checkout';
import { ValidatedTextInput } from '@woocommerce/base-components/text-input';
import ValidationInputError from '@woocommerce/base-components/validation-input-error';
import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -46,8 +45,17 @@ export const TotalsCoupon = ( {
}: TotalsCouponProps ): JSX.Element => {
const [ couponValue, setCouponValue ] = useState( '' );
const currentIsLoading = useRef( false );
const { getValidationError, getValidationErrorId } = useValidationContext();
const validationError = getValidationError( 'coupon' );

const validationErrorKey = 'coupon';
const textInputId = `wc-block-components-totals-coupon__input-${ instanceId }`;

const { validationError, validationErrorId } = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
validationError: store.getValidationError( validationErrorKey ),
validationErrorId: store.getValidationErrorId( textInputId ),
};
} );

useEffect( () => {
if ( currentIsLoading.current !== isLoading ) {
Expand All @@ -58,8 +66,6 @@ export const TotalsCoupon = ( {
}
}, [ isLoading, couponValue, validationError ] );

const textInputId = `wc-block-components-totals-coupon__input-${ instanceId }`;

return (
<Panel
className="wc-block-components-totals-coupon"
Expand Down Expand Up @@ -98,9 +104,7 @@ export const TotalsCoupon = ( {
'woo-gutenberg-products-block'
) }
value={ couponValue }
ariaDescribedBy={ getValidationErrorId(
textInputId
) }
ariaDescribedBy={ validationErrorId }
onChange={ ( newCouponValue ) => {
setCouponValue( newCouponValue );
} }
Expand All @@ -112,7 +116,10 @@ export const TotalsCoupon = ( {
disabled={ isLoading || ! couponValue }
showSpinner={ isLoading }
onClick={ (
e: React.MouseEvent< HTMLElement, 'click' >
e: React.MouseEvent<
HTMLButtonElement,
MouseEvent
>
) => {
e.preventDefault();
onSubmit( couponValue );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*/
import { useArgs } from '@storybook/client-api';
import { Story, Meta } from '@storybook/react';
import {
useValidationContext,
ValidationContextProvider,
} from '@woocommerce/base-context';
import { INTERACTION_TIMEOUT } from '@woocommerce/storybook-controls';
import { useDispatch } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -52,7 +50,7 @@ LoadingState.args = {
};

export const ErrorState: Story< TotalsCouponProps > = ( args ) => {
const { setValidationErrors } = useValidationContext();
const { setValidationErrors } = useDispatch( VALIDATION_STORE_KEY );

setValidationErrors( { coupon: INVALID_COUPON_ERROR } );

Expand All @@ -61,10 +59,6 @@ export const ErrorState: Story< TotalsCouponProps > = ( args ) => {

ErrorState.decorators = [
( StoryComponent ) => {
return (
<ValidationContextProvider>
<StoryComponent />
</ValidationContextProvider>
);
return <StoryComponent />;
},
];
28 changes: 12 additions & 16 deletions assets/js/base/components/combobox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
import { ComboboxControl } from 'wordpress-components';
import {
ValidationInputError,
useValidationContext,
} from '@woocommerce/base-context';
import { ValidationInputError } from '@woocommerce/base-components/validation-input-error';
import { isObject } from '@woocommerce/types';
import { useDispatch, useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -55,19 +54,16 @@ const Combobox = ( {
instanceId = '0',
autoComplete = 'off',
}: ComboboxProps ): JSX.Element => {
const { getValidationError, setValidationErrors, clearValidationError } =
useValidationContext();

const controlRef = useRef< HTMLDivElement >( null );
const controlId = id || 'control-' + instanceId;
const errorId = incomingErrorId || controlId;
const error = ( getValidationError( errorId ) || {
message: '',
hidden: false,
} ) as {
message: string;
hidden: boolean;
};

const { setValidationErrors, clearValidationError } =
useDispatch( VALIDATION_STORE_KEY );
const error = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return store.getValidationError( errorId );
} );

useEffect( () => {
if ( ! required || value ) {
Expand Down Expand Up @@ -99,7 +95,7 @@ const Combobox = ( {
id={ controlId }
className={ classnames( 'wc-block-components-combobox', className, {
'is-active': value,
'has-error': error.message && ! error.hidden,
'has-error': error?.message && ! error?.hidden,
} ) }
ref={ controlRef }
>
Expand Down Expand Up @@ -142,7 +138,7 @@ const Combobox = ( {
value={ value || '' }
allowReset={ false }
autoComplete={ autoComplete }
aria-invalid={ error.message && ! error.hidden }
aria-invalid={ error?.message && ! error?.hidden }
/>
<ValidationInputError propertyName={ errorId } />
</div>
Expand Down
16 changes: 4 additions & 12 deletions assets/js/base/components/country-input/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* External dependencies
*/
import { Story, Meta } from '@storybook/react';
import {
useValidationContext,
ValidationContextProvider,
} from '@woocommerce/base-context';
import { useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
Expand All @@ -31,21 +29,15 @@ export default {
options: { table: { disable: true } },
value: { control: false },
},
decorators: [
( StoryComponent ) => (
<ValidationContextProvider>
<StoryComponent />
</ValidationContextProvider>
),
],
decorators: [ ( StoryComponent ) => <StoryComponent /> ],
} as Meta< CountryInputWithCountriesProps >;

const Template: Story< CountryInputWithCountriesProps > = ( args ) => {
const [ selectedCountry, selectCountry ] = useState< CountryCode | '' >(
''
);
const { clearValidationError, showValidationError } =
useValidationContext();
useDispatch( VALIDATION_STORE_KEY );

useEffect( () => {
showValidationError( 'country' );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/base/components/state-input/state-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element';
import classnames from 'classnames';
import { ValidatedTextInput } from '@woocommerce/base-components/text-input';

/**
* Internal dependencies
*/
import { ValidatedTextInput } from '../text-input';
import Combobox from '../combobox';
import './style.scss';
import type { StateInputWithStatesProps } from './StateInputProps';
Expand Down
Loading