Skip to content

Commit

Permalink
refactor(redux): rename user to appReducer (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Jan 31, 2024
1 parent af80cda commit d13462f
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { helpers } from './common';
*/
const App = ({ getLocale, useDispatch: useAliasDispatch, useSelector: useAliasSelector }) => {
const dispatch = useAliasDispatch();
const { value: locale } = useAliasSelector(({ user }) => user?.locale?.data, {});
const { value: locale } = useAliasSelector(({ app }) => app?.locale?.data, {});
let platformNotifications = null;

useMount(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('AuthenticationContext', () => {

const { result: mockStoreSuccessResponse } = await renderHook(() => useGetAuthorization(), {
state: {
user: {
app: {
auth: {
fulfilled: true,
data: [
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('AuthenticationContext', () => {

const { result: mockStoreErrorResponse } = await renderHook(() => useGetAuthorization(), {
state: {
user: {
app: {
auth: {
error: true,
data: []
Expand Down
6 changes: 3 additions & 3 deletions src/components/authentication/authenticationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ const useGetAuthorization = ({
const dispatch = useAliasDispatch();
const { updateDocumentTitle = helpers.noop } = useAliasChrome();
const { data, error, fulfilled, pending, responses } = useAliasSelectorsResponse([
{ id: 'auth', selector: ({ user }) => user?.auth },
{ id: 'locale', selector: ({ user }) => user?.locale },
{ id: 'auth', selector: ({ app }) => app?.auth },
{ id: 'locale', selector: ({ app }) => app?.locale },
{
id: 'errors',
selector: ({ user }) => (user?.errors?.error === true && user.errors) || { fulfilled: true, data: [] }
selector: ({ app }) => (app?.errors?.error === true && app.errors) || { fulfilled: true, data: [] }
}
]);

Expand Down
2 changes: 1 addition & 1 deletion src/components/optinView/optinView.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const OptinView = ({
}) => {
const dispatch = useAliasDispatch();
const { errorStatus } = useAliasSession();
const { error, fulfilled, pending } = useAliasSelectorsResponse(({ user }) => user?.optin);
const { error, fulfilled, pending } = useAliasSelectorsResponse(({ app }) => app?.optin);

/**
* Submit and update account opt-in.
Expand Down
63 changes: 28 additions & 35 deletions src/redux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<dd></dd>
<dt><a href="#Middleware.module_StatusMiddleware">StatusMiddleware</a></dt>
<dd></dd>
<dt><a href="#Reducers.module_AppReducer">AppReducer</a></dt>
<dd><p>Application related state reducer.</p>
</dd>
<dt><a href="#Reducers.module_GraphReducer">GraphReducer</a></dt>
<dd><p>Graph/Chart related API and user state reducer.</p>
</dd>
Expand All @@ -33,9 +36,6 @@
<dt><a href="#Reducers.module_ToolbarReducer">ToolbarReducer</a></dt>
<dd><p>Toolbar related user state reducer.</p>
</dd>
<dt><a href="#Reducers.module_UserReducer">UserReducer</a></dt>
<dd><p>User related API, platform and user state reducer.</p>
</dd>
<dt><a href="#Reducers.module_ViewReducer">ViewReducer</a></dt>
<dd><p>View query related user state reducer.</p>
</dd>
Expand Down Expand Up @@ -1018,6 +1018,31 @@ Apply a status type based on actions, such as those generated from redux-promise
</tr> </tbody>
</table>

<a name="Reducers.module_AppReducer"></a>

## AppReducer
Application related state reducer.

<a name="Reducers.module_AppReducer..appReducer"></a>

### AppReducer~appReducer(state, action) ⇒ <code>object</code> \| <code>Object</code>
Apply application observer/reducer logic to state, against actions.

**Kind**: inner method of [<code>AppReducer</code>](#Reducers.module_AppReducer)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>state</td><td><code>object</code></td>
</tr><tr>
<td>action</td><td><code>object</code></td>
</tr> </tbody>
</table>

<a name="Reducers.module_GraphReducer"></a>

## GraphReducer
Expand Down Expand Up @@ -1121,31 +1146,6 @@ Apply user observer/reducer logic for toolbar to state, against actions.
</tr> </tbody>
</table>

<a name="Reducers.module_UserReducer"></a>

## UserReducer
User related API, platform and user state reducer.

<a name="Reducers.module_UserReducer..userReducer"></a>

### UserReducer~userReducer(state, action) ⇒ <code>object</code> \| <code>Object</code>
Apply user observer/reducer logic for session to state, against actions.

**Kind**: inner method of [<code>UserReducer</code>](#Reducers.module_UserReducer)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>state</td><td><code>object</code></td>
</tr><tr>
<td>action</td><td><code>object</code></td>
</tr> </tbody>
</table>

<a name="Reducers.module_ViewReducer"></a>

## ViewReducer
Expand Down Expand Up @@ -1195,7 +1195,6 @@ Create a Redux store.
* [~queryTypes](#Types.module_ReduxTypes..queryTypes) : <code>Object</code>
* [~rhsmTypes](#Types.module_ReduxTypes..rhsmTypes) : <code>Object</code>
* [~toolbarTypes](#Types.module_ReduxTypes..toolbarTypes) : <code>Object</code>
* [~userTypes](#Types.module_ReduxTypes..userTypes) : <code>Object</code>

<a name="Types.module_ReduxTypes..appTypes"></a>

Expand Down Expand Up @@ -1244,12 +1243,6 @@ RHSM API action, reducer types.
### ReduxTypes~toolbarTypes : <code>Object</code>
Filter, toolbar action, reducer types.

**Kind**: inner constant of [<code>ReduxTypes</code>](#Types.module_ReduxTypes)
<a name="Types.module_ReduxTypes..userTypes"></a>

### ReduxTypes~userTypes : <code>Object</code>
User action, reducer types.

**Kind**: inner constant of [<code>ReduxTypes</code>](#Types.module_ReduxTypes)
<a name="reduxMiddleware"></a>

Expand Down
6 changes: 3 additions & 3 deletions src/redux/actions/__tests__/platformActions.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import promiseMiddleware from 'redux-promise-middleware';
import { applyMiddleware, combineReducers, createStore } from 'redux';
import { platformActions } from '../platformActions';
import { userReducer } from '../../reducers';
import { appReducer } from '../../reducers';

describe('PlatformActions', () => {
const middleware = [promiseMiddleware];
const generateStore = () =>
createStore(
combineReducers({
user: userReducer
app: appReducer
}),
applyMiddleware(...middleware)
);
Expand All @@ -18,7 +18,7 @@ describe('PlatformActions', () => {
const dispatcher = platformActions.authorizeUser();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;
const response = store.getState().app;

expect(response.auth.fulfilled).toBe(true);
done();
Expand Down
12 changes: 6 additions & 6 deletions src/redux/actions/__tests__/userActions.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import promiseMiddleware from 'redux-promise-middleware';
import { applyMiddleware, combineReducers, createStore } from 'redux';
import moxios from 'moxios';
import { userReducer } from '../../reducers';
import { appReducer } from '../../reducers';
import { userActions } from '../userActions';

describe('UserActions', () => {
const middleware = [promiseMiddleware];
const generateStore = () =>
createStore(
combineReducers({
user: userReducer
app: appReducer
}),
applyMiddleware(...middleware)
);
Expand All @@ -36,7 +36,7 @@ describe('UserActions', () => {
const dispatcher = userActions.getLocale();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;
const response = store.getState().app;

expect(response.locale.fulfilled).toBe(true);
done();
Expand All @@ -48,7 +48,7 @@ describe('UserActions', () => {
const dispatcher = userActions.deleteAccountOptIn();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;
const response = store.getState().app;
expect(response.optin.fulfilled).toBe(true);
done();
});
Expand All @@ -59,7 +59,7 @@ describe('UserActions', () => {
const dispatcher = userActions.getAccountOptIn();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;
const response = store.getState().app;
expect(response.optin.fulfilled).toBe(true);
done();
});
Expand All @@ -70,7 +70,7 @@ describe('UserActions', () => {
const dispatcher = userActions.updateAccountOptIn();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;
const response = store.getState().app;
expect(response.optin.fulfilled).toBe(true);
done();
});
Expand Down
10 changes: 5 additions & 5 deletions src/redux/actions/userActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { userTypes } from '../types';
import { appTypes } from '../types';
import { userServices } from '../../services/user/userServices';
import { helpers } from '../../common/helpers';
import { translate } from '../../components/i18n/i18n';
Expand All @@ -17,7 +17,7 @@ import { translate } from '../../components/i18n/i18n';
*/
const getLocale = () => dispatch =>
dispatch({
type: userTypes.USER_LOCALE,
type: appTypes.USER_LOCALE,
payload: userServices.getLocale()
});

Expand All @@ -28,7 +28,7 @@ const getLocale = () => dispatch =>
*/
const deleteAccountOptIn = () => dispatch =>
dispatch({
type: userTypes.DELETE_USER_OPTIN,
type: appTypes.DELETE_USER_OPTIN,
payload: userServices.deleteAccountOptIn(),
meta: {
notifications: {}
Expand All @@ -42,7 +42,7 @@ const deleteAccountOptIn = () => dispatch =>
*/
const getAccountOptIn = () => dispatch =>
dispatch({
type: userTypes.GET_USER_OPTIN,
type: appTypes.GET_USER_OPTIN,
payload: userServices.getAccountOptIn(),
meta: {
notifications: {}
Expand All @@ -59,7 +59,7 @@ const updateAccountOptIn =
(query = {}) =>
dispatch =>
dispatch({
type: userTypes.UPDATE_USER_OPTIN,
type: appTypes.UPDATE_USER_OPTIN,
payload: userServices.updateAccountOptIn(query),
meta: {
query,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import userReducer from '../userReducer';
import appReducer from '../appReducer';
import { rhsmConstants } from '../../../services/rhsm/rhsmConstants';
import { platformConstants } from '../../../services/platform/platformConstants';
import { appTypes, platformTypes, userTypes as types } from '../../types';
import { appTypes as types, platformTypes } from '../../types';
import { reduxHelpers } from '../../common';

describe('UserReducer', () => {
it('should return the initial state', () => {
expect(userReducer.initialState).toBeDefined();
expect(appReducer.initialState).toBeDefined();
});

it('should handle specific http status types', () => {
const specificTypes = [
{ type: appTypes.STATUS_4XX, status: 400 },
{ type: appTypes.STATUS_4XX, status: 401 },
{ type: appTypes.STATUS_4XX, status: 403 }
{ type: types.STATUS_4XX, status: 400 },
{ type: types.STATUS_4XX, status: 401 },
{ type: types.STATUS_4XX, status: 403 }
];

specificTypes.forEach(value => {
Expand All @@ -37,7 +37,7 @@ describe('UserReducer', () => {
}
};

const resultState = userReducer(undefined, dispatched);
const resultState = appReducer(undefined, dispatched);

expect({ type: value.type, result: resultState }).toMatchSnapshot(`http status ${value.type} ${value.status}`);
});
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('UserReducer', () => {
}
};

const resultState = userReducer(undefined, dispatched);
const resultState = appReducer(undefined, dispatched);

expect({ type: reduxHelpers.REJECTED_ACTION(value), result: resultState }).toMatchSnapshot(
`rejected types ${value}`
Expand All @@ -90,7 +90,7 @@ describe('UserReducer', () => {
type: reduxHelpers.PENDING_ACTION(value)
};

const resultState = userReducer(undefined, dispatched);
const resultState = appReducer(undefined, dispatched);

expect({ type: reduxHelpers.PENDING_ACTION(value), result: resultState }).toMatchSnapshot(
`pending types ${value}`
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('UserReducer', () => {
}
};

const resultState = userReducer(undefined, dispatched);
const resultState = appReducer(undefined, dispatched);

expect({ type: reduxHelpers.FULFILLED_ACTION(value), result: resultState }).toMatchSnapshot(
`fulfilled types ${value}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import _get from 'lodash/get';
import { appTypes, platformTypes, userTypes } from '../types';
import { appTypes, platformTypes } from '../types';
import { rhsmConstants } from '../../services/rhsm/rhsmConstants';
import { reduxHelpers } from '../common';

/**
* User related API, platform and user state reducer.
* Application related state reducer.
*
* @memberof Reducers
* @module UserReducer
* @module AppReducer
*/

/**
Expand All @@ -24,13 +24,13 @@ const initialState = {
};

/**
* Apply user observer/reducer logic for session to state, against actions.
* Apply application observer/reducer logic to state, against actions.
*
* @param {object} state
* @param {object} action
* @returns {object|{}}
*/
const userReducer = (state = initialState, action) => {
const appReducer = (state = initialState, action) => {
switch (action.type) {
case reduxHelpers.HTTP_STATUS_RANGE(appTypes.STATUS_4XX):
const actionStatus = reduxHelpers.getStatusFromResults(action);
Expand Down Expand Up @@ -58,8 +58,8 @@ const userReducer = (state = initialState, action) => {
default:
return reduxHelpers.generatedPromiseActionReducer(
[
{ ref: 'locale', type: userTypes.USER_LOCALE },
{ ref: 'optin', type: [userTypes.DELETE_USER_OPTIN, userTypes.GET_USER_OPTIN, userTypes.UPDATE_USER_OPTIN] },
{ ref: 'locale', type: appTypes.USER_LOCALE },
{ ref: 'optin', type: [appTypes.DELETE_USER_OPTIN, appTypes.GET_USER_OPTIN, appTypes.UPDATE_USER_OPTIN] },
{ ref: 'auth', type: platformTypes.PLATFORM_USER_AUTH }
],
state,
Expand All @@ -68,6 +68,6 @@ const userReducer = (state = initialState, action) => {
}
};

userReducer.initialState = initialState;
appReducer.initialState = initialState;

export { userReducer as default, initialState, userReducer };
export { appReducer as default, initialState, appReducer };
Loading

0 comments on commit d13462f

Please sign in to comment.