Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

192 wc editor create two way sync for the post metadata #21398

Draft
wants to merge 12 commits into
base: feature/woo-new-editor
Choose a base branch
from
19 changes: 18 additions & 1 deletion packages/js/src/helpers/create-watcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clone, isEqual } from "lodash";
import { clone, isEqual, reduce } from "lodash";

/**
* Creates a getter for an array of getters.
Expand Down Expand Up @@ -40,4 +40,21 @@ const createWatcher = ( getData, onChange ) => {
};
};

/**
* Creates a getter for an object of getters.
*
* Use this if you want to combine getData functions.
*
* @param {Object<string, function>} getters The getters.
* @returns {function} The combined getter.
*/
export const createCollectorFromObject = ( getters ) => () => reduce(
getters,
( result, getData, key ) => {
result[ key ] = getData();
return result;
},
{}
);

export default createWatcher;
24 changes: 18 additions & 6 deletions packages/js/src/helpers/fields/AdvancedFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export default class AdvancedFields {
* @returns {void}
*/
static set noIndex( value ) {
AdvancedFields.noIndexElement.value = value;
if ( AdvancedFields.noIndexElement ) {
AdvancedFields.noIndexElement.value = value;
}
}

/**
Expand All @@ -93,7 +95,9 @@ export default class AdvancedFields {
* @returns {void}
*/
static set noFollow( value ) {
AdvancedFields.noFollowElement.value = value;
if ( AdvancedFields.noFollowElement ) {
AdvancedFields.noFollowElement.value = value;
}
}

/**
Expand All @@ -113,7 +117,9 @@ export default class AdvancedFields {
* @returns {void}
*/
static set advanced( value ) {
AdvancedFields.advancedElement.value = value;
if ( AdvancedFields.advancedElement ) {
AdvancedFields.advancedElement.value = value;
}
}

/**
Expand All @@ -133,7 +139,9 @@ export default class AdvancedFields {
* @returns {void}
*/
static set breadcrumbsTitle( value ) {
AdvancedFields.breadcrumbsTitleElement.value = value;
if ( AdvancedFields.breadcrumbsTitleElement ) {
AdvancedFields.breadcrumbsTitleElement.value = value;
}
}

/**
Expand All @@ -153,7 +161,9 @@ export default class AdvancedFields {
* @returns {void}
*/
static set canonical( value ) {
AdvancedFields.canonicalElement.value = value;
if ( AdvancedFields.canonicalElement ) {
AdvancedFields.canonicalElement.value = value;
}
}

/**
Expand All @@ -173,6 +183,8 @@ export default class AdvancedFields {
* @returns {void}
*/
static set wordproofTimestamp( value ) {
AdvancedFields.wordproofTimestampElement.value = value ? "1" : "0";
if ( AdvancedFields.wordproofTimestampElement ) {
AdvancedFields.wordproofTimestampElement.value = value ? "1" : "0";
}
}
}
16 changes: 12 additions & 4 deletions packages/js/src/helpers/fields/FacebookFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export default class FacebookFields {
* @returns {void}
*/
static set title( value ) {
FacebookFields.titleElement.value = value;
if ( FacebookFields.titleElement ) {
FacebookFields.titleElement.value = value;
}
}

/**
Expand All @@ -66,7 +68,9 @@ export default class FacebookFields {
* @returns {void}
*/
static set description( value ) {
FacebookFields.descriptionElement.value = value;
if ( FacebookFields.descriptionElement ) {
FacebookFields.descriptionElement.value = value;
}
}

/**
Expand All @@ -86,7 +90,9 @@ export default class FacebookFields {
* @returns {void}
*/
static set imageId( value ) {
FacebookFields.imageIdElement.value = value;
if ( FacebookFields.imageIdElement ) {
FacebookFields.imageIdElement.value = value;
}
}

/**
Expand All @@ -106,7 +112,9 @@ export default class FacebookFields {
* @returns {void}
*/
static set imageUrl( value ) {
FacebookFields.imageUrlElement.value = value;
if ( FacebookFields.imageUrlElement ) {
FacebookFields.imageUrlElement.value = value;
}
}

/**
Expand Down
12 changes: 9 additions & 3 deletions packages/js/src/helpers/fields/SearchMetadataFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default class SearchMetadataFields {
* @returns {void}
*/
static set title( value ) {
SearchMetadataFields.titleElement.value = value;
if ( SearchMetadataFields.titleElement ) {
SearchMetadataFields.titleElement.value = value;
}
}

/**
Expand All @@ -66,7 +68,9 @@ export default class SearchMetadataFields {
* @returns {void}
*/
static set description( value ) {
SearchMetadataFields.descriptionElement.value = value;
if ( SearchMetadataFields.descriptionElement ) {
SearchMetadataFields.descriptionElement.value = value;
}
}

/**
Expand All @@ -86,6 +90,8 @@ export default class SearchMetadataFields {
* @returns {void}
*/
static set slug( value ) {
SearchMetadataFields.slugElement.value = value;
if ( SearchMetadataFields.slugElement ) {
SearchMetadataFields.slugElement.value = value;
}
}
}
16 changes: 12 additions & 4 deletions packages/js/src/helpers/fields/TwitterFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export default class TwitterFields {
* @returns {void}
*/
static set title( value ) {
TwitterFields.titleElement.value = value;
if ( TwitterFields.titleElement ) {
TwitterFields.titleElement.value = value;
}
}

/**
Expand All @@ -66,7 +68,9 @@ export default class TwitterFields {
* @returns {void}
*/
static set description( value ) {
TwitterFields.descriptionElement.value = value;
if ( TwitterFields.descriptionElement ) {
TwitterFields.descriptionElement.value = value;
}
}

/**
Expand All @@ -86,7 +90,9 @@ export default class TwitterFields {
* @returns {void}
*/
static set imageId( value ) {
TwitterFields.imageIdElement.value = value;
if ( TwitterFields.imageIdElement ) {
TwitterFields.imageIdElement.value = value;
}
}

/**
Expand All @@ -106,7 +112,9 @@ export default class TwitterFields {
* @returns {void}
*/
static set imageUrl( value ) {
TwitterFields.imageUrlElement.value = value;
if ( TwitterFields.imageUrlElement ) {
TwitterFields.imageUrlElement.value = value;
}
}

/**
Expand Down
17 changes: 10 additions & 7 deletions packages/js/src/initializers/editor-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { combineReducers, registerStore } from "@wordpress/data";
import { actions, reducers, selectors } from "@yoast/externals/redux";
import { get, pickBy } from "lodash";
import * as controls from "../redux/controls";
import { STORES } from "../shared-admin/constants";

/**
* Populates the store.
Expand Down Expand Up @@ -46,15 +47,17 @@ const populateStore = store => {

/**
* Initializes the Yoast SEO editor store.
*
* @param {Object} [extra] Additional store configuration.
* @returns {object} The Yoast SEO editor store.
*/
export default function initEditorStore() {
const store = registerStore( "yoast-seo/editor", {
reducer: combineReducers( reducers ),
selectors,
actions: pickBy( actions, x => typeof x === "function" ),
controls,
export default function initEditorStore( extra = {} ) {
const store = registerStore( STORES.editor, {
reducer: combineReducers( { ...reducers, ...extra.reducers } ),
initialState: { ...extra.initialState },
actions: pickBy( { ...actions, ...extra.actions }, x => typeof x === "function" ),
selectors: { ...selectors, ...extra.selectors },
controls: { ...controls, ...extra.controls },
resolvers: { ...extra.resolvers },
} );

populateStore( store );
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/redux/actions/postId.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const SET_POST_ID = "SET_POST_ID";

/**
* @param {number} postId The post ID.
* @returns {Object} Action object.
* @returns {{type: string, payload: number}} Action object.
*/
export const setPostId = ( postId ) => ( {
type: SET_POST_ID,
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/redux/actions/primaryTaxonomies.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const SET_PRIMARY_TAXONOMY = `${ PREFIX }SET_PRIMARY_TAXONOMY`;
* Redux action creator to set the primary taxonomy ID.
*
* @param {string} taxonomy The taxonomy.
* @param {string} termId The term ID.
* @param {number} termId The term ID.
*
* @returns {object} The action.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/redux/reducers/advancedSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
* Initial state
*/
const initialState = {
noIndex: "",
noFollow: "",
noIndex: "0",
noFollow: "0",
advanced: [],
breadcrumbsTitle: "",
canonical: "",
Expand Down
6 changes: 3 additions & 3 deletions packages/js/src/redux/reducers/postId.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { SET_POST_ID } from "../actions/postId";
/**
* A reducer for the post ID.
*
* @param {boolean} state The current state of the object.
* @param {Object} action The current action received.
* @param {?number} state The current state of the object.
* @param {{type: string, payload: number}} action The current action received.
*
* @returns {boolean} The state.
*/
const postId = ( state = false, action ) => {
const postId = ( state = null, action ) => {
switch ( action.type ) {
case SET_POST_ID:
return action.payload;
Expand Down
30 changes: 15 additions & 15 deletions packages/js/src/redux/selectors/advancedSettings.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { get } from "lodash";

/**
* Gets the twitter title from the state.
* Gets the robots no-index from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter title.
* @returns {string} The robots no-index.
*/
export const getNoIndex = state => get( state, "advancedSettings.noIndex", "" );
export const getNoIndex = state => get( state, "advancedSettings.noIndex", "0" );

/**
* Gets the twitter description from the state.
* Gets the robots no-follow from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter description.
* @returns {string} The robots no-follow.
*/
export const getNoFollow = state => get( state, "advancedSettings.noFollow", "" );
export const getNoFollow = state => get( state, "advancedSettings.noFollow", "0" );

/**
* Gets the twitter image URL from the state.
* Gets the robots advanced settings from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter image URL.
* @returns {string[]} The robots advanced settings.
*/
export const getAdvanced = state => get( state, "advancedSettings.advanced", "" );
export const getAdvanced = state => get( state, "advancedSettings.advanced", [] );

/**
* Gets the twitter image type from the state.
* Gets the breadcrumbs title from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter image type.
* @returns {string} The breadcrumbs title
*/
export const getBreadcrumbsTitle = state => get( state, "advancedSettings.breadcrumbsTitle", "summary" );

/**
* Gets the Twitter image src from the state.
* Gets the canonical from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter image src.
* @returns {string} The canonical.
*/
export const getCanonical = state => get( state, "advancedSettings.canonical", "" );

Expand All @@ -54,10 +54,10 @@ export const getCanonical = state => get( state, "advancedSettings.canonical", "
*/
export const getWordProofTimestamp = state => get( state, "advancedSettings.wordproofTimestamp", false );

/** Gets the Twitter image src from the state.
/** Gets whether the advanced settings is loading from the state.
*
* @param {Object} state The state.
*
* @returns {String} Twitter image src.
* @returns {boolean} Whether the advanced settings is loading.
*/
export const getIsLoading = state => get( state, "advancedSettings.isLoading", true );
11 changes: 10 additions & 1 deletion packages/js/src/redux/selectors/facebookEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ export const getFacebookTitle = state => get( state, "facebookEditor.title", ""
*/
export const getFacebookDescription = state => get( state, "facebookEditor.description", "" );

/**
* Gets the facebook image ID from the state.
*
* @param {Object} state The state.
*
* @returns {?number} Facebook image ID.
*/
export const getFacebookImageId = state => get( state, "facebookEditor.image.id", null );

/**
* Gets the facebook image URL from the state.
*
* @param {Object} state The state.
*
* @returns {String} Facebook image URL.
*/
export const getFacebookImageUrl = state => get( state, "facebookEditor.image.url" );
export const getFacebookImageUrl = state => get( state, "facebookEditor.image.url", "" );

/**
* Gets the facebook image src from the state.
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/redux/selectors/postId.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get } from "lodash";

/**
* @param {Ojbect} state The current Redux state.
* @returns {number} The post ID.
* @param {Object} state The current Redux state.
* @returns {?number} The post ID.
*/
export const getPostId = ( state ) => get( state, "postId", null );
Loading
Loading