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

Commit

Permalink
Update useValidation to use the data store
Browse files Browse the repository at this point in the history
  • Loading branch information
opr committed Jun 7, 2022
1 parent 04482f4 commit b90fe27
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions assets/js/base/context/hooks/use-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ import type {
ValidationData,
ValidationContextError,
} from '@woocommerce/type-defs/contexts';

/**
* Internal dependencies
*/
import { useValidationContext } from '../providers/validation/';
import { useDispatch, useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Custom hook for setting for adding errors to the validation system.
*/
export const useValidation = (): ValidationData => {
const {
hasValidationErrors,
getValidationError,
clearValidationError,
hideValidationError,
setValidationErrors,
} = useValidationContext();
} = useDispatch( VALIDATION_STORE_KEY );
const { hasValidationErrors, getValidationError } = useSelect(
( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
hasValidationErrors: store.hasValidationErrors,
getValidationError: store.getValidationError(),
};
}
);
const prefix = 'extensions-errors';

return {
hasValidationErrors,
hasValidationErrors: hasValidationErrors(),
getValidationError: useCallback(
( validationErrorId: string ) =>
getValidationError( `${ prefix }-${ validationErrorId }` ),
Expand Down

0 comments on commit b90fe27

Please sign in to comment.