Skip to content

Commit

Permalink
DEV service layer, redux, graphSelectors
Browse files Browse the repository at this point in the history
* redux and service layer adjustments
* state service layer update, useReactRedux, generated multiselector
* useReactRedux multiselector, service layer flatten meta transform
* service layer fixes
* service layer annotation
  • Loading branch information
cdcabrera committed Nov 8, 2021
1 parent e458924 commit 97f3fdd
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 317 deletions.
18 changes: 17 additions & 1 deletion src/redux/hooks/useReactRedux.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useSelector as UseSelector, shallowEqual } from 'react-redux';
import { createSelector } from 'reselect';
import { store } from '../store';
import { helpers } from '../../common/helpers';

Expand Down Expand Up @@ -28,10 +29,25 @@ const useSelector = (selector, value = null, options = {}) => {
return UseSelector(selector, options.equality) ?? value;
};

/**
* Generate a selector from multiple selectors for use in "useSelector".
*
* @param {Array} params
* @returns {Array}
*/
const useMultiSelector = (...params) => {
const [firstSel, ...selectors] = params;
const updatedSelectors = (Array.isArray(firstSel) && firstSel) || selectors;
const multiSelector = createSelector(updatedSelectors, (...results) => results);

return useSelector(multiSelector, shallowEqual);
};

const reactReduxHooks = {
shallowEqual,
useDispatch,
useMultiSelector,
useSelector
};

export { reactReduxHooks as default, reactReduxHooks, shallowEqual, useDispatch, useSelector };
export { reactReduxHooks as default, reactReduxHooks, shallowEqual, useDispatch, useMultiSelector, useSelector };
160 changes: 0 additions & 160 deletions src/redux/selectors/__tests__/graphSelectors.test.js

This file was deleted.

117 changes: 0 additions & 117 deletions src/redux/selectors/graphSelectors.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/redux/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import appMessagesSelectors from './appMessagesSelectors';
import guestsListSelectors from './guestsListSelectors';
import graphSelectors from './graphSelectors';
import graphCardSelectors from './graphCardSelectors';
import inventoryListSelectors from './inventoryListSelectors';
import subscriptionsListSelectors from './subscriptionsListSelectors';
Expand All @@ -9,7 +8,6 @@ import userSelectors from './userSelectors';
const reduxSelectors = {
appMessages: appMessagesSelectors,
guestsList: guestsListSelectors,
graph: graphSelectors,
graphCard: graphCardSelectors,
inventoryList: inventoryListSelectors,
subscriptionsList: subscriptionsListSelectors,
Expand Down
12 changes: 6 additions & 6 deletions src/services/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ const camelCase = obj => {
};

/**
* Apply response schema callback to data.
* Apply data to a callback, pass original data on error.
*
* @param {object} data
* @param {Function} schema
* @param {Function} callback
* @returns {{data: *, error}}
*/
const responseNormalize = (data, schema) => {
const passDataToCallback = (data, callback) => {
let error;
let updatedData = data;

try {
updatedData = schema(data);
updatedData = callback(data);
} catch (e) {
error = e;
}
Expand Down Expand Up @@ -80,8 +80,8 @@ const schemaResponse = ({ casing, convert = true, id = null, response, schema }

const serviceHelpers = {
camelCase,
responseNormalize,
passDataToCallback,
schemaResponse
};

export { serviceHelpers as default, serviceHelpers, camelCase, responseNormalize, schemaResponse };
export { serviceHelpers as default, serviceHelpers, camelCase, passDataToCallback, schemaResponse };
Loading

0 comments on commit 97f3fdd

Please sign in to comment.