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

Lodash: Remove remaining _.get() from core-data #48310

Merged
merged 2 commits into from
Feb 22, 2023
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
3 changes: 1 addition & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { capitalCase, pascalCase } from 'change-case';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -45,7 +44,7 @@ export const rootEntitiesConfig = [
kind: 'root',
baseURL: '/wp/v2/settings',
getTitle: ( record ) => {
return get( record, [ 'title' ], __( 'Site Title' ) );
return record?.title ?? __( 'Site Title' );
},
},
{
Expand Down
7 changes: 5 additions & 2 deletions packages/core-data/src/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import createSelector from 'rememo';
import EquivalentKeyMap from 'equivalent-key-map';
import { get, set } from 'lodash';
import { set } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -66,7 +66,10 @@ function getQueriedItemsUncached( state, query ) {

for ( let f = 0; f < fields.length; f++ ) {
const field = fields[ f ].split( '.' );
const value = get( item, field );
let value = item;
field.forEach( ( fieldName ) => {
value = value[ fieldName ];
} );
set( filteredItem, field, value );
}
} else {
Expand Down
8 changes: 2 additions & 6 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import fastDeepEqual from 'fast-deep-equal/es6';
import { groupBy, get } from 'lodash';
import { groupBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -245,11 +245,7 @@ function entity( entityConfig ) {
// comparison.
! fastDeepEqual(
edits[ key ],
get(
record[ key ],
'raw',
record[ key ]
)
record[ key ]?.raw ?? record[ key ]
) &&
// Sometimes the server alters the sent value which means
// we need to also remove the edits before the api request.
Expand Down
160 changes: 58 additions & 102 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import createSelector from 'rememo';
import { set, get } from 'lodash';
import { set } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -36,6 +36,7 @@ export interface State {
themeBaseGlobalStyles: Record< string, Object >;
themeGlobalStyleVariations: Record< string, string >;
undo: UndoState;
userPermissions: Record< string, boolean >;
users: UserState;
}

Expand All @@ -46,9 +47,20 @@ interface EntitiesState {
records: Record< string, Record< string, EntityState< ET.EntityRecord > > >;
}

interface QueriedData {
items: Record< ET.Context, Record< number, ET.EntityRecord > >;
itemIsComplete: Record< ET.Context, Record< number, boolean > >;
queries: Record< ET.Context, Record< string, Array< number > > >;
}

interface EntityState< EntityRecord extends ET.EntityRecord > {
edits: Record< string, Partial< EntityRecord > >;
saving: Record< string, { pending: boolean } >;
saving: Record<
string,
Partial< { pending: boolean; isAutosave: boolean; error: Error } >
>;
deleting: Record< string, Partial< { pending: boolean; error: Error } > >;
queriedData: QueriedData;
}

interface EntityConfig {
Expand Down Expand Up @@ -298,11 +310,8 @@ export const getEntityRecord = createSelector(
key: EntityRecordKey,
query?: GetRecordsHttpQuery
): EntityRecord | undefined => {
const queriedState = get( state.entities.records, [
kind,
name,
'queriedData',
] );
const queriedState =
state.entities.records?.[ kind ]?.[ name ]?.queriedData;
if ( ! queriedState ) {
return undefined;
}
Expand All @@ -323,7 +332,10 @@ export const getEntityRecord = createSelector(
const fields = getNormalizedCommaSeparable( query._fields ) ?? [];
for ( let f = 0; f < fields.length; f++ ) {
const field = fields[ f ].split( '.' );
const value = get( item, field );
let value = item;
field.forEach( ( fieldName ) => {
value = value[ fieldName ];
} );
set( filteredItem, field, value );
}
return filteredItem as EntityRecord;
Expand All @@ -334,22 +346,11 @@ export const getEntityRecord = createSelector(
( state: State, kind, name, recordId, query ) => {
const context = query?.context ?? 'default';
return [
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
state.entities.records?.[ kind ]?.[ name ]?.queriedData?.items[
context
]?.[ recordId ],
state.entities.records?.[ kind ]?.[ name ]?.queriedData
?.itemIsComplete[ context ]?.[ recordId ],
];
}
) as GetEntityRecord;
Expand Down Expand Up @@ -403,11 +404,7 @@ export const getRawEntityRecord = createSelector(
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
accumulator[ _key ] = get(
record[ _key ],
'raw',
record[ _key ]
);
accumulator[ _key ] = record[ _key ]?.raw ?? record[ _key ];
} else {
accumulator[ _key ] = record[ _key ];
}
Expand All @@ -425,22 +422,11 @@ export const getRawEntityRecord = createSelector(
const context = query?.context ?? 'default';
return [
state.entities.config,
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
state.entities.records?.[ kind ]?.[ name ]?.queriedData?.items[
context
]?.[ recordId ],
state.entities.records?.[ kind ]?.[ name ]?.queriedData
?.itemIsComplete[ context ]?.[ recordId ],
];
}
);
Expand Down Expand Up @@ -519,11 +505,8 @@ export const getEntityRecords = ( <
): EntityRecord[] | null => {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist.
const queriedState = get( state.entities.records, [
kind,
name,
'queriedData',
] );
const queriedState =
state.entities.records?.[ kind ]?.[ name ]?.queriedData;
if ( ! queriedState ) {
return null;
}
Expand Down Expand Up @@ -661,12 +644,9 @@ export function getEntityRecordEdits(
name: string,
recordId: EntityRecordKey
): Optional< any > {
return get( state.entities.records, [
kind,
name,
'edits',
recordId as string | number,
] );
return state.entities.records?.[ kind ]?.[ name ]?.edits?.[
recordId as string | number
];
}

/**
Expand Down Expand Up @@ -704,7 +684,7 @@ export const getEntityRecordNonTransientEdits = createSelector(
},
( state: State, kind: string, name: string, recordId: EntityRecordKey ) => [
state.entities.config,
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
state.entities.records?.[ kind ]?.[ name ]?.edits?.[ recordId ],
]
);

Expand Down Expand Up @@ -763,23 +743,12 @@ export const getEditedEntityRecord = createSelector(
const context = query?.context ?? 'default';
return [
state.entities.config,
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
state.entities.records?.[ kind ]?.[ name ]?.queriedData.items[
context
]?.[ recordId ],
state.entities.records?.[ kind ]?.[ name ]?.queriedData
.itemIsComplete[ context ]?.[ recordId ],
state.entities.records?.[ kind ]?.[ name ]?.edits?.[ recordId ],
];
}
);
Expand All @@ -800,11 +769,8 @@ export function isAutosavingEntityRecord(
name: string,
recordId: EntityRecordKey
): boolean {
const { pending, isAutosave } = get(
state.entities.records,
[ kind, name, 'saving', recordId ],
{}
);
const { pending, isAutosave } =
state.entities.records?.[ kind ]?.[ name ]?.saving?.[ recordId ] ?? {};
return Boolean( pending && isAutosave );
}

Expand All @@ -824,10 +790,10 @@ export function isSavingEntityRecord(
name: string,
recordId: EntityRecordKey
): boolean {
return get(
state.entities.records,
[ kind, name, 'saving', recordId as EntityRecordKey, 'pending' ],
false
return (
state.entities.records?.[ kind ]?.[ name ]?.saving?.[
recordId as EntityRecordKey
]?.pending ?? false
);
}

Expand All @@ -847,10 +813,10 @@ export function isDeletingEntityRecord(
name: string,
recordId: EntityRecordKey
): boolean {
return get(
state.entities.records,
[ kind, name, 'deleting', recordId, 'pending' ],
false
return (
state.entities.records?.[ kind ]?.[ name ]?.deleting?.[
recordId as EntityRecordKey
]?.pending ?? false
);
}

Expand All @@ -870,13 +836,8 @@ export function getLastEntitySaveError(
name: string,
recordId: EntityRecordKey
): any {
return get( state.entities.records, [
kind,
name,
'saving',
recordId,
'error',
] );
return state.entities.records?.[ kind ]?.[ name ]?.saving?.[ recordId ]
?.error;
}

/**
Expand All @@ -895,13 +856,8 @@ export function getLastEntityDeleteError(
name: string,
recordId: EntityRecordKey
): any {
return get( state.entities.records, [
kind,
name,
'deleting',
recordId,
'error',
] );
return state.entities.records?.[ kind ]?.[ name ]?.deleting?.[ recordId ]
?.error;
}

/**
Expand Down Expand Up @@ -1057,7 +1013,7 @@ export function canUser(
id?: EntityRecordKey
): boolean | undefined {
const key = [ action, resource, id ].filter( Boolean ).join( '/' );
return get( state, [ 'userPermissions', key ] );
return state.userPermissions[ key ];
}

/**
Expand Down