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

P2 694 create schema blocks redux store #1119

Merged
merged 12 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/schema-blocks/src/core/blocks/BlockDefinition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class BlockDefinition extends Definition {
configuration.edit = props => this.edit( props );
configuration.save = props => this.save( props );

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

// Register the block to WordPress.
registerBlockType( name, configuration );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dispatch } from "@wordpress/data";
import { BlockValidationResult } from "../../core/validation";
import logger from "../logger";
import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from "../redux";

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

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

const store = dispatch( "yoast-seo/editor" );
const store = dispatch( YOAST_SCHEMA_BLOCKS_STORE_NAME );
if ( store ) {
store.resetBlockValidation();
validations.forEach( blockValidation => {
logger.debug( "storing validation: ", blockValidation );
store.addBlockValidation( blockValidation );
} );
} else {
logger.debug( "No Store!" );
}
}
2 changes: 1 addition & 1 deletion packages/schema-blocks/src/functions/gutenberg/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function validateBlocks( blocks: BlockInstance[] ): BlockValidationResult
export default function watch() {
subscribe(
debounce( () => {
if ( updatingSchema || select( "core/block-editor" ).isTyping() ) {
if ( updatingSchema ) {
return;
}

Expand Down
17 changes: 13 additions & 4 deletions packages/schema-blocks/src/functions/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { registerBlockType } from "@wordpress/blocks";

import "../instructions";
import logger, { LogLevel } from "./logger";
import { registerBlockType } from "@wordpress/blocks";
import { initializeSchemaBlocksStore } from "../functions/redux";
import { WarningBlock } from "../blocks/warning-block/configuration";
import { processBlock, processSchema } from "./process";
import filter from "./gutenberg/filter";
import watch from "./gutenberg/watch";
import logger, { LogLevel } from "./logger";

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

registerBlockType( "yoast/warning-block", WarningBlock );
initializeSchemaBlocksStore();

registerInternalBlocks();

jQuery( 'script[type="text/schema-template"]' ).each( function() {
try {
Expand Down Expand Up @@ -54,3 +56,10 @@ export function initialize( logLevel: LogLevel = LogLevel.ERROR ) {
// Watch Gutenberg for block changes that require schema updates.
watch();
}

/**
* Registers additional blocks needed for schema blocks use cases.
*/
function registerInternalBlocks() {
registerBlockType( "yoast/warning-block", WarningBlock );
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { select } from "@wordpress/data";
import { __, sprintf } from "@wordpress/i18n";

import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from "../redux";
import { BlockValidation, BlockValidationResult } from "../../core/validation";
import { getHumanReadableBlockName } from "../BlockHelper";
import { BlockPresence } from "../../core/validation/BlockValidationResult";
Expand Down Expand Up @@ -29,7 +29,7 @@ export type SidebarWarning = {
* @returns {BlockValidationResult} The validation results, or null if none were found.
*/
function getValidationResult( clientId: string ): BlockValidationResult | null {
const validationResults: clientIdValidation = select( "yoast-seo/editor" ).getSchemaBlocksValidationResults();
const validationResults: clientIdValidation = select( YOAST_SCHEMA_BLOCKS_STORE_NAME ).getSchemaBlocksValidationResults();
if ( ! validationResults ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BlockValidationResult } from "../../core/validation";

export interface SchemaBlocksState {
validations: Record<string, BlockValidationResult>;
}

export const SchemaBlocksDefaultState: SchemaBlocksState = {
validations: {},
};
41 changes: 41 additions & 0 deletions packages/schema-blocks/src/functions/redux/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { BlockValidationResult } from "../../../core/validation";

import * as schemaBlocksActions from "./schemaBlocks";

const PREFIX = "WPSEO_";

export const SchemaBlocksStoreActions = {
ADD_BLOCK_VALIDATION: `${ PREFIX }ADD_BLOCK_VALIDATION`,
RESET_BLOCK_VALIDATIONS: `${ PREFIX }CLEAR_BLOCK_VALIDATIONS`,
};

export interface SchemaBlocksStoreCommand {
type: string;
validation?: BlockValidationResult;
}

/**
* Configures a redux command to store a validation result.
*
* @param validation The validation to store.
* @returns The configured AddBlockValidation command.
*/
export function AddBlockValidationCommand( validation: BlockValidationResult ): SchemaBlocksStoreCommand {
return {
type: SchemaBlocksStoreActions.ADD_BLOCK_VALIDATION,
validation: validation,
};
}

/**
* Configures a redux command to reset the current validation results.
*
* @returns Thge configured ResetBlockValidation command
*/
export function ResetBlockValidationCommand(): SchemaBlocksStoreCommand {
return {
type: SchemaBlocksStoreActions.RESET_BLOCK_VALIDATIONS,
};
}

export { schemaBlocksActions };
21 changes: 21 additions & 0 deletions packages/schema-blocks/src/functions/redux/actions/schemaBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BlockValidationResult } from "../../../core/validation";
import { SchemaBlocksStoreCommand, AddBlockValidationCommand, ResetBlockValidationCommand } from "./";

/**
* Updates whether a block is valid.
*
* @param {blockValidationResult} blockValidationResult The block validation to store.
*
* @returns {SchemaBlocksStoreAction} An action for redux.
*/
export function addBlockValidation( blockValidationResult: BlockValidationResult ): SchemaBlocksStoreCommand {
return AddBlockValidationCommand( blockValidationResult );
}

/**
* Commands the store to reset the block validation store to initial state.
* @returns {SchemaBlocksStoreAction} The command to set the initial state for the block validation store.
*/
export function resetBlockValidation(): SchemaBlocksStoreCommand {
return ResetBlockValidationCommand();
}
6 changes: 6 additions & 0 deletions packages/schema-blocks/src/functions/redux/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./actions";
export * from "./reducer";
export * from "./selectors";
export * from "./initializeSchemaStore";

export const YOAST_SCHEMA_BLOCKS_STORE_NAME = "yoast-seo/schema-blocks";
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { registerStore, StoreConfig } from "@wordpress/data";
import { SchemaBlocksDefaultState, SchemaBlocksState } from "./SchemaBlocksState";
import { schemaBlocksActions } from "./actions";
import { schemaBlocksReducer } from "./reducer";
import * as schemaBlocksSelectors from "./selectors";
import { YOAST_SCHEMA_BLOCKS_STORE_NAME } from ".";

/**
* Initializes the Schema Blocks store.
*/
export function initializeSchemaBlocksStore() {
const storeOptions: StoreConfig<SchemaBlocksState> = {
reducer: schemaBlocksReducer,
selectors: {
...schemaBlocksSelectors,
},
actions: {
...schemaBlocksActions,
},
initialState: SchemaBlocksDefaultState,
};

registerStore( YOAST_SCHEMA_BLOCKS_STORE_NAME, storeOptions );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { schemaBlocksReducer } from "./schemaBlocks";
30 changes: 30 additions & 0 deletions packages/schema-blocks/src/functions/redux/reducer/schemaBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SchemaBlocksStoreCommand, SchemaBlocksStoreActions } from "../actions";
import { SchemaBlocksState, SchemaBlocksDefaultState } from "../SchemaBlocksState";

/**
* A reducer for the Schema blocks.
*
* @param {SchemaBlocksState} state The current state of the object.
* @param {SetBlockValidation} command The received command.
*
* @returns {SchemaBlocksState} The state.
*/
export function schemaBlocksReducer( state: SchemaBlocksState = SchemaBlocksDefaultState, command: SchemaBlocksStoreCommand ): SchemaBlocksState {
switch ( command.type ) {
case SchemaBlocksStoreActions.RESET_BLOCK_VALIDATIONS: {
return SchemaBlocksDefaultState;
}
case SchemaBlocksStoreActions.ADD_BLOCK_VALIDATION: {
const newState = Object.assign( {}, state );
const validation = command.validation;

newState.validations = newState.validations || {};
newState.validations[ validation.clientId ] = validation;

return newState;
}
default: {
return state;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./schemaBlocks";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SchemaBlocksState, SchemaBlocksDefaultState } from "../SchemaBlocksState";

/**
* The schema validation results.
*
* @param {object} state The current state.
*
* @returns {Record<string, BlockValidationResult>} The schema blocks validation results.
*/
export function getSchemaBlocksValidationResults( state: SchemaBlocksState ): object {
return state.validations || SchemaBlocksDefaultState.validations;
}
1 change: 0 additions & 1 deletion packages/schema-blocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import { RenderEditProps, RenderSaveProps } from "./core/blocks/BlockDefinition"
import { LogLevel } from "./functions/logger";

export { BlockInstruction, RenderSaveProps, RenderEditProps, LogLevel };

export default initialize;