Skip to content

Commit

Permalink
refactor(redux): discovery-8 middleware, hooks, restructure (#82)
Browse files Browse the repository at this point in the history
* helpers, aggregatedError func for redux hooks
* redux actions, named exports
* redux hooks, useReactRedux
* redux middleware, middleware export, multi-actions
* store, move middleware, named export
  • Loading branch information
cdcabrera committed Jun 28, 2022
1 parent 90a4dfd commit acabcf8
Show file tree
Hide file tree
Showing 23 changed files with 1,933 additions and 50 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
"collectCoverageFrom": [
"src/**/*.{js,jsx}",
"!src/**/.*/**",
"!src/components/app.js",
"!src/index.js",
"!src/setupTests.js",
"!src/components/app.js",
"!src/components/**/index.js",
"!src/common/index.js",
"!src/redux/index.js",
"!src/redux/store.js",
"!src/redux/middleware/**",
"!src/redux/actions/index.js",
"!src/redux/common/index.js",
"!src/redux/hooks/index.js",
"!src/redux/reducers/index.js",
"!src/redux/selectors/index.js"
],
Expand Down
1 change: 1 addition & 0 deletions src/common/__tests__/__snapshots__/helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Object {
"UI_SENTENCE_START_NAME": "The Quipucords tool",
"UI_SHORT_NAME": "Quipucords",
"UI_VERSION": "0.0.0.0000000",
"aggregatedError": [Function],
"copyClipboard": [Function],
"createViewQueryObject": [Function],
"devModeNormalizeCount": [Function],
Expand Down
25 changes: 25 additions & 0 deletions src/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import moment from 'moment';
import _get from 'lodash/get';
import _set from 'lodash/set';

/**
* Fill for AggregatedError
*
* @param {Array|*} errors An array of errors
* @param {string|*} message
* @param {object} options
* @param {string} options.name
* @returns {Error|window.AggregateError<Error>}
*/
const aggregatedError = (errors, message, { name = 'AggregateError' } = {}) => {
const { AggregateError, Error } = window;
let err;

if (AggregateError) {
err = new AggregateError(errors, message);
} else {
err = new Error(message);
err.name = name;
err.errors = (Array.isArray(errors) && errors) || [errors];
err.isEmulated = true;
}
return err;
};

const copyClipboard = text => {
let successful;

Expand Down Expand Up @@ -315,6 +339,7 @@ const UI_SHORT_NAME = process.env.REACT_APP_UI_SHORT_NAME;
const UI_VERSION = process.env.REACT_APP_UI_VERSION;

const helpers = {
aggregatedError,
copyClipboard,
devModeNormalizeCount,
downloadData,
Expand Down
3 changes: 3 additions & 0 deletions src/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { helpers } from './helpers';

export { helpers as default, helpers };
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './components/app';
import { baseName } from './components/router/router';
import store from './redux/store';
import { store } from './redux/store';
import 'patternfly/dist/css/rcue.css';
import 'patternfly/dist/css/rcue-additions.css';
import './styles/index.scss';
Expand Down
22 changes: 20 additions & 2 deletions src/redux/actions/credentialsActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { credentialsTypes } from '../constants';
import credentialsService from '../../services/credentialsService';
import { credentialsService } from '../../services';

const addCredential = data => dispatch =>
dispatch({
Expand Down Expand Up @@ -41,4 +41,22 @@ const deleteCredentials =
payload: credentialsService.deleteCredentials(ids)
});

export { addCredential, deleteCredential, deleteCredentials, getCredential, getCredentials, updateCredential };
const credentialsActions = {
addCredential,
deleteCredential,
deleteCredentials,
getCredential,
getCredentials,
updateCredential
};

export {
credentialsActions as default,
credentialsActions,
addCredential,
deleteCredential,
deleteCredentials,
getCredential,
getCredentials,
updateCredential
};
8 changes: 6 additions & 2 deletions src/redux/actions/factsActions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { factsTypes } from '../constants';
import factsService from '../../services/factsService';
import { factsService } from '../../services';

const addFacts = data => dispatch =>
dispatch({
type: factsTypes.ADD_FACTS,
payload: factsService.addFacts(data)
});

export { addFacts as default, addFacts };
const factsActions = {
addFacts
};

export { factsActions as default, factsActions, addFacts };
19 changes: 8 additions & 11 deletions src/redux/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as credentialsActions from './credentialsActions';
import * as factsActions from './factsActions';
import * as reportsActions from './reportsActions';
import * as scansActions from './scansActions';
import * as sourcesActions from './sourcesActions';
import * as statusActions from './statusActions';
import * as userActions from './userActions';
import { credentialsActions } from './credentialsActions';
import { factsActions } from './factsActions';
import { reportsActions } from './reportsActions';
import { scansActions } from './scansActions';
import { sourcesActions } from './sourcesActions';
import { statusActions } from './statusActions';
import { userActions } from './userActions';

const actions = {
const reduxActions = {
credentials: credentialsActions,
facts: factsActions,
reports: reportsActions,
Expand All @@ -16,12 +16,9 @@ const actions = {
user: userActions
};

const reduxActions = { ...actions };

export {
reduxActions as default,
reduxActions,
actions,
credentialsActions,
factsActions,
reportsActions,
Expand Down
9 changes: 7 additions & 2 deletions src/redux/actions/reportsActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reportsTypes } from '../constants';
import reportsService from '../../services/reportsService';
import { reportsService } from '../../services';

const getReportsDownload = id => dispatch =>
dispatch({
Expand All @@ -13,4 +13,9 @@ const mergeReports = data => dispatch =>
payload: reportsService.mergeReports(data)
});

export { getReportsDownload, mergeReports };
const reportsActions = {
getReportsDownload,
mergeReports
};

export { reportsActions as default, reportsActions, getReportsDownload, mergeReports };
18 changes: 17 additions & 1 deletion src/redux/actions/scansActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { scansTypes } from '../constants';
import scansService from '../../services/scansService';
import { scansService } from '../../services';

const addScan = data => dispatch =>
dispatch({
Expand Down Expand Up @@ -83,7 +83,23 @@ const restartScan = id => dispatch =>
meta: { id }
});

const scansActions = {
addScan,
addStartScan,
getScans,
getScanJobs,
getScanJob,
getConnectionScanResults,
getInspectionScanResults,
startScan,
pauseScan,
cancelScan,
restartScan
};

export {
scansActions as default,
scansActions,
addScan,
addStartScan,
getScans,
Expand Down
24 changes: 22 additions & 2 deletions src/redux/actions/sourcesActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sourcesTypes } from '../constants';
import sourcesService from '../../services/sourcesService';
import { sourcesService } from '../../services';

const addSource =
(data, query = {}) =>
Expand Down Expand Up @@ -51,4 +51,24 @@ const updateSource = (id, data) => dispatch =>
payload: sourcesService.updateSource(id, data)
});

export { addSource, deleteSource, deleteSources, getScansSources, getSource, getSources, updateSource };
const sourcesActions = {
addSource,
deleteSource,
deleteSources,
getScansSources,
getSource,
getSources,
updateSource
};

export {
sourcesActions as default,
sourcesActions,
addSource,
deleteSource,
deleteSources,
getScansSources,
getSource,
getSources,
updateSource
};
6 changes: 4 additions & 2 deletions src/redux/actions/statusActions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { statusTypes } from '../constants';
import statusService from '../../services/statusService';
import { statusService } from '../../services';

const getStatus = () => dispatch =>
dispatch({
type: statusTypes.STATUS_INFO,
payload: statusService.getStatus()
});

export { getStatus as default, getStatus };
const statusActions = { getStatus };

export { statusActions as default, statusActions, getStatus };
10 changes: 8 additions & 2 deletions src/redux/actions/userActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { userTypes } from '../constants';
import userService from '../../services/userService';
import { userService } from '../../services';

const authorizeUser = () => dispatch =>
dispatch({
Expand All @@ -18,4 +18,10 @@ const logoutUser = () => dispatch =>
payload: userService.logoutUser()
});

export { authorizeUser, getLocale, logoutUser };
const userActions = {
authorizeUser,
getLocale,
logoutUser
};

export { userActions as default, userActions, authorizeUser, getLocale, logoutUser };
3 changes: 3 additions & 0 deletions src/redux/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { reduxHelpers } from './reduxHelpers';

export { reduxHelpers as default, reduxHelpers };
Loading

0 comments on commit acabcf8

Please sign in to comment.