Skip to content

Commit

Permalink
update for model specific selectors, actions, resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed Dec 4, 2018
1 parent f44e292 commit 05a459d
Show file tree
Hide file tree
Showing 18 changed files with 540 additions and 154 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export function* createEntity( modelName, entity ) {
return null;
}
const entityInstance = factory.createNew( entity );
yield receiveEntity( modelName, entityInstance );
yield receiveEntity( entityInstance );
return entityInstance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ function receiveAndReplaceEntityRecords( modelName, entities = {} ) {
* Action creator for adding an entity to the state (as opposed to an entity
* record)
*
* @param {string} modelName
* @param {BaseEntity} entity
* @return {{type: string, modelName: string, entity: BaseEntity}} An action
* @return {{type: string, entity: BaseEntity}} An action
* object.
*/
function receiveEntity( modelName, entity ) {
function receiveEntity( entity ) {
return {
type: types.RECEIVE_ENTITY,
modelName,
entity,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { types } = ACTION_TYPES.relations;
* Action creator for removing all indexed relations for a specific entity from
* the state.
*
* Note: The following things are accomplished by this action:
* Note: The following things are accomplished by this action (via the reducer):
* - The relation index for the relation on this entity is removed from state.
* - If the relation entities themselves have no other relation index, they
* are also removed from state.
Expand Down
19 changes: 8 additions & 11 deletions assets/src/data/eventespresso/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import reducer from './reducers';
import * as selectors from './selectors';
import * as actions from './actions';
import * as resolvers from './resolvers';
import { createEntitySelectors } from './models';
import {
selectors as modelSelectors,
actions as modelActions,
resolvers as modelResolvers,
} from './model';
import { REDUCER_KEY } from './constants';
import controls from '../base-controls';

/**
* creates specific model entity selectors (getEvents, getDatetimes etc)
* wrapping the generic selectors.
* @type {Object<Function>}
*/
const entitySelectors = createEntitySelectors( selectors );

/**
* Registers store for 'eventespresso/core'.
*/
export default registerStore( REDUCER_KEY, {
reducer,
actions,
selectors: { ...selectors, ...entitySelectors },
resolvers: { ...resolvers },
actions: { ...actions, ...modelActions },
selectors: { ...selectors, ...modelSelectors },
resolvers: { ...resolvers, ...modelResolvers },
controls,
} );
120 changes: 120 additions & 0 deletions assets/src/data/eventespresso/core/model/entity-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* External import
*/
import { MODEL_NAMES } from '@eventespresso/model';

/**
* Internal imports
*/
import { getMethodName } from '../../base-model';

/**
* Dynamic creation of actions for entities
* @param {Object} action The action object that dynamically created functions
* will be mapped to.
* @return {Object} The new action object containing functions for each model.
*/
export const createActions = ( action ) => MODEL_NAMES.reduce(
( actions, modelName ) => {
actions[ getMethodName(
modelName,
'',
'create'
) ] = ( entity ) => action.createEntity( modelName, entity );
actions[ getMethodName(
modelName,
'byId',
'delete'
) ] = ( entityId ) => action.deleteEntityById( modelName, entityId );
actions[ getMethodName(
modelName,
'byId',
'trash',
) ] = ( entityId ) => action.trashEntityById( modelName, entityId );
actions[ getMethodName(
modelName,
'record',
'persist'
) ] = ( entity ) => action.persistEntityRecord( modelName, entity );
actions[ getMethodName(
modelName,
'id',
'persistFor',
) ] = ( entityId ) => action.persistForEntityId( modelName, entityId );
actions[ getMethodName(
modelName,
'ids',
'persistFor',
) ] = ( entityIds ) => action.persistForEntityIds(
modelName,
entityIds
);
actions[ getMethodName(
modelName,
'',
'persistDeletesFor',
) ] = () => action.persistDeletesForModel( modelName );
actions[ getMethodName(
modelName,
'',
'persistTrashesFor',
) ] = () => action.persistTrashesForModel( modelName );
actions[ getMethodName(
modelName,
'byId',
'remove',
) ] = ( entityId ) => action.removeEntityById( modelName, entityId );
actions[ getMethodName(
modelName,
'id',
'removeDelete',
) ] = ( entityId ) => action.removeDeleteEntityId(
modelName,
entityId
);
actions[ getMethodName(
modelName,
'id',
'removeTrash',
) ] = ( entityId ) => action.removeTrashEntityId( modelName, entityId );
actions[ getMethodName(
modelName,
'records',
'receive',
) ] = ( entities ) => action.receiveEntityRecords(
modelName,
entities
);
actions[ getMethodName(
modelName,
'records',
'receiveAndReplace',
) ] = ( entities ) => action.recieveAndReplaceEntityRecords(
modelName,
entities
);
actions[ getMethodName(
modelName,
'byId',
'remove',
) ] = ( entityId ) => action.removeEntityById( modelName, entityId );
actions[ getMethodName(
modelName,
'id',
'receiveTrash',
) ] = ( entityId ) => action.receiveTrashEntityId(
modelName,
entityId
);
actions[ getMethodName(
modelName,
'id',
'receiveDelete',
) ] = ( entityId ) => action.receiveDeleteEntityId(
modelName,
entityId
);
return actions;
},
{}
);
28 changes: 28 additions & 0 deletions assets/src/data/eventespresso/core/model/entity-resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External import
*/
import { MODEL_NAMES } from '@eventespresso/model';

/**
* Internal imports
*/
import { getMethodName } from '../../base-model';

/**
* Dynamic creation of resolvers for entities
* @param {Object} resolver The resolver object that dynamically created
* functions will be mapped to.
* @return {Object} The new resolver object containing functions for each model.
*/
export const createResolvers = ( resolver ) => MODEL_NAMES.reduce(
( resolvers, modelName ) => {
resolvers[ getMethodName(
modelName,
'byId',
'get'
) ] = ( entityId ) => resolver.getEntityById( modelName, entityId );

return resolvers;
},
{}
);
52 changes: 52 additions & 0 deletions assets/src/data/eventespresso/core/model/entity-selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* External import
*/
import { MODEL_NAMES } from '@eventespresso/model';

/**
* Internal imports
*/
import { getMethodName } from '../../base-model';

/**
* Dynamic creation of selectors for entities
* @param {Object} selector selector object that dynamically created functions
* will be mapped to.
* @return {Object} The new selector objects for each model.
*/
export const createSelectors = ( selector ) => MODEL_NAMES.reduce(
( selectors, modelName ) => {
selectors[ getMethodName( modelName, 'records' ) ] = (
state
) => selector.getEntityRecordsForModel( state, modelName );
selectors[ getMethodName( modelName, '', 'get', true ) ] = (
state
) => selector.getEntitiesForModel( state, modelName );
selectors[ getMethodName( modelName, 'byId' ) ] = (
state,
entityId
) => selector.getEntityById( state, modelName, entityId );
selectors[ getMethodName( modelName, 'byIds', 'get', true ) ] = (
state,
entityIds,
) => selector.getEntitiesByIds( state, modelName, entityIds );
selectors[ getMethodName(
modelName,
'idsQueuedForTrash',
'get'
) ] = ( state ) => selector.getEntityIdsQueuedForTrash(
state,
modelName
);
selectors[ getMethodName(
modelName,
'idsQueuedForDelete',
'get'
) ] = ( state ) => selector.getEntityIdsQueuedforDelete(
state,
modelName
);
return selectors;
},
{}
);
22 changes: 22 additions & 0 deletions assets/src/data/eventespresso/core/model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as entityActions from './entity-actions';
import * as entityResolvers from './entity-resolvers';
import * as entitySelectors from './entity-selectors';
import * as relationActions from './relation-actions';
import * as relationSelectors from './relation-selectors';
import * as baseSelectors from '../selectors';
import * as baseActions from '../actions';
import * as baseResolvers from '../resolvers';

export const selectors = {
...entitySelectors.createSelectors( baseSelectors ),
...relationSelectors.createSelectors( baseSelectors ),
};

export const actions = {
...entityActions.createActions( baseActions ),
...relationActions.createActions( baseActions ),
};

export const resolvers = {
...entityResolvers.createResolvers( baseResolvers ),
};
Loading

0 comments on commit 05a459d

Please sign in to comment.