Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.

Commit 050d7d6

Browse files
Merge pull request #1119 from Yoast/P2-694-create-schema-blocks-redux-store
P2 694 create schema blocks redux store
2 parents f3593ea + 7053134 commit 050d7d6

File tree

15 files changed

+166
-10
lines changed

15 files changed

+166
-10
lines changed

packages/schema-blocks/src/core/blocks/BlockDefinition.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class BlockDefinition extends Definition {
8585
configuration.edit = props => this.edit( props );
8686
configuration.save = props => this.save( props );
8787

88-
logger.info( "registering block " + name );
88+
logger.debug( "registering block " + name );
8989

9090
// Register the block to WordPress.
9191
registerBlockType( name, configuration );

packages/schema-blocks/src/functions/gutenberg/storeBlockValidation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { dispatch } from "@wordpress/data";
22
import { BlockValidationResult } from "../../core/validation";
33
import logger from "../logger";
4+
import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from "../redux";
45

56
/**
67
* Updates the store with information about whether a block is valid or why it isn't.
@@ -13,12 +14,14 @@ export default function storeBlockValidation( validations: BlockValidationResult
1314

1415
logger.debug( "Updating the store with the validation results." );
1516

16-
const store = dispatch( "yoast-seo/editor" );
17+
const store = dispatch( YOAST_SCHEMA_BLOCKS_STORE_NAME );
1718
if ( store ) {
1819
store.resetBlockValidation();
1920
validations.forEach( blockValidation => {
2021
logger.debug( "storing validation: ", blockValidation );
2122
store.addBlockValidation( blockValidation );
2223
} );
24+
} else {
25+
logger.debug( "No Store!" );
2326
}
2427
}

packages/schema-blocks/src/functions/gutenberg/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function validateBlocks( blocks: BlockInstance[] ): BlockValidationResult
122122
export default function watch() {
123123
subscribe(
124124
debounce( () => {
125-
if ( updatingSchema || select( "core/block-editor" ).isTyping() ) {
125+
if ( updatingSchema ) {
126126
return;
127127
}
128128

packages/schema-blocks/src/functions/initialize.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { registerBlockType } from "@wordpress/blocks";
2-
31
import "../instructions";
4-
import logger, { LogLevel } from "./logger";
2+
import { registerBlockType } from "@wordpress/blocks";
3+
import { initializeSchemaBlocksStore } from "../functions/redux";
54
import { WarningBlock } from "../blocks/warning-block/configuration";
65
import { processBlock, processSchema } from "./process";
76
import filter from "./gutenberg/filter";
87
import watch from "./gutenberg/watch";
8+
import logger, { LogLevel } from "./logger";
99

1010
/**
1111
* Removes all whitespace including line breaks from a string.
@@ -26,7 +26,9 @@ function removeWhitespace( text: string ): string {
2626
export function initialize( logLevel: LogLevel = LogLevel.ERROR ) {
2727
logger.setLogLevel( logLevel );
2828

29-
registerBlockType( "yoast/warning-block", WarningBlock );
29+
initializeSchemaBlocksStore();
30+
31+
registerInternalBlocks();
3032

3133
jQuery( 'script[type="text/schema-template"]' ).each( function() {
3234
try {
@@ -54,3 +56,10 @@ export function initialize( logLevel: LogLevel = LogLevel.ERROR ) {
5456
// Watch Gutenberg for block changes that require schema updates.
5557
watch();
5658
}
59+
60+
/**
61+
* Registers additional blocks needed for schema blocks use cases.
62+
*/
63+
function registerInternalBlocks() {
64+
registerBlockType( "yoast/warning-block", WarningBlock );
65+
}

packages/schema-blocks/src/functions/presenters/SidebarWarningPresenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { select } from "@wordpress/data";
22
import { __, sprintf } from "@wordpress/i18n";
3-
3+
import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from "../redux";
44
import { BlockValidation, BlockValidationResult } from "../../core/validation";
55
import { getHumanReadableBlockName } from "../BlockHelper";
66
import { BlockPresence } from "../../core/validation/BlockValidationResult";
@@ -29,7 +29,7 @@ export type SidebarWarning = {
2929
* @returns {BlockValidationResult} The validation results, or null if none were found.
3030
*/
3131
function getValidationResult( clientId: string ): BlockValidationResult | null {
32-
const validationResults: clientIdValidation = select( "yoast-seo/editor" ).getSchemaBlocksValidationResults();
32+
const validationResults: clientIdValidation = select( YOAST_SCHEMA_BLOCKS_STORE_NAME ).getSchemaBlocksValidationResults();
3333
if ( ! validationResults ) {
3434
return null;
3535
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { BlockValidationResult } from "../../core/validation";
2+
3+
export interface SchemaBlocksState {
4+
validations: Record<string, BlockValidationResult>;
5+
}
6+
7+
export const SchemaBlocksDefaultState: SchemaBlocksState = {
8+
validations: {},
9+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { BlockValidationResult } from "../../../core/validation";
2+
3+
import * as schemaBlocksActions from "./schemaBlocks";
4+
5+
const PREFIX = "WPSEO_";
6+
7+
export const SchemaBlocksStoreActions = {
8+
ADD_BLOCK_VALIDATION: `${ PREFIX }ADD_BLOCK_VALIDATION`,
9+
RESET_BLOCK_VALIDATIONS: `${ PREFIX }CLEAR_BLOCK_VALIDATIONS`,
10+
};
11+
12+
export interface SchemaBlocksStoreCommand {
13+
type: string;
14+
validation?: BlockValidationResult;
15+
}
16+
17+
/**
18+
* Configures a redux command to store a validation result.
19+
*
20+
* @param validation The validation to store.
21+
* @returns The configured AddBlockValidation command.
22+
*/
23+
export function AddBlockValidationCommand( validation: BlockValidationResult ): SchemaBlocksStoreCommand {
24+
return {
25+
type: SchemaBlocksStoreActions.ADD_BLOCK_VALIDATION,
26+
validation: validation,
27+
};
28+
}
29+
30+
/**
31+
* Configures a redux command to reset the current validation results.
32+
*
33+
* @returns Thge configured ResetBlockValidation command
34+
*/
35+
export function ResetBlockValidationCommand(): SchemaBlocksStoreCommand {
36+
return {
37+
type: SchemaBlocksStoreActions.RESET_BLOCK_VALIDATIONS,
38+
};
39+
}
40+
41+
export { schemaBlocksActions };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { BlockValidationResult } from "../../../core/validation";
2+
import { SchemaBlocksStoreCommand, AddBlockValidationCommand, ResetBlockValidationCommand } from "./";
3+
4+
/**
5+
* Updates whether a block is valid.
6+
*
7+
* @param {blockValidationResult} blockValidationResult The block validation to store.
8+
*
9+
* @returns {SchemaBlocksStoreAction} An action for redux.
10+
*/
11+
export function addBlockValidation( blockValidationResult: BlockValidationResult ): SchemaBlocksStoreCommand {
12+
return AddBlockValidationCommand( blockValidationResult );
13+
}
14+
15+
/**
16+
* Commands the store to reset the block validation store to initial state.
17+
* @returns {SchemaBlocksStoreAction} The command to set the initial state for the block validation store.
18+
*/
19+
export function resetBlockValidation(): SchemaBlocksStoreCommand {
20+
return ResetBlockValidationCommand();
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from "./actions";
2+
export * from "./reducer";
3+
export * from "./selectors";
4+
export * from "./initializeSchemaStore";
5+
6+
export const YOAST_SCHEMA_BLOCKS_STORE_NAME = "yoast-seo/schema-blocks";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { registerStore, StoreConfig } from "@wordpress/data";
2+
import { SchemaBlocksDefaultState, SchemaBlocksState } from "./SchemaBlocksState";
3+
import { schemaBlocksActions } from "./actions";
4+
import { schemaBlocksReducer } from "./reducer";
5+
import * as schemaBlocksSelectors from "./selectors";
6+
import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from ".";
7+
8+
/**
9+
* Initializes the Schema Blocks store.
10+
*/
11+
export function initializeSchemaBlocksStore() {
12+
const storeOptions: StoreConfig<SchemaBlocksState> = {
13+
reducer: schemaBlocksReducer,
14+
selectors: {
15+
...schemaBlocksSelectors,
16+
},
17+
actions: {
18+
...schemaBlocksActions,
19+
},
20+
initialState: SchemaBlocksDefaultState,
21+
};
22+
23+
registerStore( YOAST_SCHEMA_BLOCKS_STORE_NAME, storeOptions );
24+
}

0 commit comments

Comments
 (0)