Skip to content

Commit

Permalink
refactor according to self review
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu authored and adamziel committed May 21, 2020
1 parent 088ef48 commit 86b3a9e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
21 changes: 19 additions & 2 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,13 @@ _Returns_

<a name="deleteEntityRecord" href="#deleteEntityRecord">#</a> **deleteEntityRecord**

Undocumented declaration.
Action triggered to delete an entity record.

_Parameters_

- _kind_ `string`: Kind of the deleted entity.
- _name_ `string`: Name of the deleted entity.
- _recordId_ `Object`: Record to be deleted.

<a name="editEntityRecord" href="#editEntityRecord">#</a> **editEntityRecord**

Expand Down Expand Up @@ -619,7 +625,18 @@ edit to an entity record, if any.

<a name="removeItems" href="#removeItems">#</a> **removeItems**

Undocumented declaration.
Returns an action object used in signalling that entity records have been
deleted and it needs to be removed from entities state.

_Parameters_

- _kind_ `string`: Kind of the removed entity.
- _name_ `string`: Name of the removed entity.
- _records_ `(Array|Object)`: Records removed.

_Returns_

- `Object`: Action object.

<a name="saveEditedEntityRecord" href="#saveEditedEntityRecord">#</a> **saveEditedEntityRecord**

Expand Down
21 changes: 19 additions & 2 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ _Returns_

<a name="deleteEntityRecord" href="#deleteEntityRecord">#</a> **deleteEntityRecord**

Undocumented declaration.
Action triggered to delete an entity record.

_Parameters_

- _kind_ `string`: Kind of the deleted entity.
- _name_ `string`: Name of the deleted entity.
- _recordId_ `Object`: Record to be deleted.

<a name="editEntityRecord" href="#editEntityRecord">#</a> **editEntityRecord**

Expand Down Expand Up @@ -202,7 +208,18 @@ edit to an entity record, if any.

<a name="removeItems" href="#removeItems">#</a> **removeItems**

Undocumented declaration.
Returns an action object used in signalling that entity records have been
deleted and it needs to be removed from entities state.

_Parameters_

- _kind_ `string`: Kind of the removed entity.
- _name_ `string`: Name of the removed entity.
- _records_ `(Array|Object)`: Records removed.

_Returns_

- `Object`: Action object.

<a name="saveEditedEntityRecord" href="#saveEditedEntityRecord">#</a> **saveEditedEntityRecord**

Expand Down
20 changes: 18 additions & 2 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ export function receiveEntityRecords(
};
}

/**
* Returns an action object used in signalling that entity records have been
* deleted and it needs to be removed from entities state.
*
* @param {string} kind Kind of the removed entity.
* @param {string} name Name of the removed entity.
* @param {Array|Object} records Records removed.
*
* @return {Object} Action object.
*/
export function removeItems( kind, name, records ) {
return {
type: 'REMOVE_ITEMS',
Expand Down Expand Up @@ -149,6 +159,13 @@ export function receiveEmbedPreview( url, preview ) {
};
}

/**
* Action triggered to delete an entity record.
*
* @param {string} kind Kind of the deleted entity.
* @param {string} name Name of the deleted entity.
* @param {Object} recordId Record to be deleted.
*/
export function* deleteEntityRecord( kind, name, recordId ) {
const entities = yield getKindEntities( kind );
const entity = find( entities, { kind, name } );
Expand All @@ -162,6 +179,7 @@ export function* deleteEntityRecord( kind, name, recordId ) {
name,
recordId,
};

yield removeItems( kind, name, recordId );

let error;
Expand Down Expand Up @@ -484,7 +502,6 @@ export function* saveEntityRecord(
name,
recordId
);

yield receiveEntityRecords(
kind,
name,
Expand All @@ -498,7 +515,6 @@ export function* saveEntityRecord(
method: recordId ? 'PUT' : 'POST',
data,
} );

yield receiveEntityRecords(
kind,
name,
Expand Down
7 changes: 2 additions & 5 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { map, flowRight } from 'lodash';
import { map, flowRight, omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -83,10 +83,7 @@ function items( state = {}, action ) {
}, {} ),
};
case 'REMOVE_ITEMS':
const newState = { ...state };
for ( const id of action.items ) {
delete newState[ id ];
}
const newState = omit( state, action.items );
return newState;
}
return state;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { groupBy, isEqual, difference, remove } from 'lodash';
import { groupBy, isEqual, difference } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -131,7 +131,6 @@ export default function useNavigationBlocks( menuId ) {
for ( const deletedClientId of deletedClientIds ) {
const menuItem = menuItemsRef.current[ deletedClientId ];
deleteMenuItem( menuItem.id );
remove( deletedClientIds, deletedClientId );
}

createSuccessNotice( __( 'Navigation saved.' ), {
Expand Down

0 comments on commit 86b3a9e

Please sign in to comment.