Skip to content

Commit

Permalink
Modularise media state (#47917)
Browse files Browse the repository at this point in the history
Fixes #42456.
  • Loading branch information
sgomes committed Dec 2, 2020
1 parent 4d69014 commit 4b2d92c
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/state/media/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/

import { castArray } from 'lodash';

/**
Expand Down Expand Up @@ -31,6 +30,7 @@ import {
} from 'calypso/state/action-types';

import 'calypso/state/data-layer/wpcom/sites/media';
import 'calypso/state/media/init';

/**
* Returns an action object used in signalling that media item(s) for the site
Expand Down
7 changes: 7 additions & 0 deletions client/state/media/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Internal dependencies
*/
import { registerReducer } from 'calypso/state/redux-store';
import reducer from './reducer';

registerReducer( [ 'media' ], reducer );
5 changes: 5 additions & 0 deletions client/state/media/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sideEffects": [
"./init.js"
]
}
6 changes: 4 additions & 2 deletions client/state/media/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
MEDIA_CLEAR_SITE,
MEDIA_ITEM_EDIT,
} from 'calypso/state/action-types';
import { combineReducers, withoutPersistence } from 'calypso/state/utils';
import { combineReducers, withoutPersistence, withStorageKey } from 'calypso/state/utils';
import MediaQueryManager from 'calypso/lib/query-manager/media';
import { ValidationErrors as MediaValidationErrors } from 'calypso/lib/media/constants';
import { transformSite as transformSiteTransientItems } from 'calypso/state/media/utils/transientItems';
Expand Down Expand Up @@ -527,11 +527,13 @@ export const fetching = withoutPersistence( ( state = {}, action ) => {
return state;
} );

export default combineReducers( {
const combinedReducer = combineReducers( {
errors,
queries,
queryRequests,
selectedItems,
transientItems,
fetching,
} );

export default withStorageKey( 'media', combinedReducer );
2 changes: 0 additions & 2 deletions client/state/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import i18n from './i18n/reducer';
import importerNux from './importer-nux/reducer';
import inlineSupportArticle from './inline-support-article/reducer';
import jitm from './jitm/reducer';
import media from './media/reducer';
import mySites from './my-sites/reducer';
import notices from './notices/reducer';
import { unseenCount as notificationsUnseenCount } from './notifications';
Expand All @@ -47,7 +46,6 @@ const reducers = {
importerNux,
inlineSupportArticle,
jitm,
media,
mySites,
notices,
notificationsUnseenCount,
Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-current-media-query.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Retrieves the current query for media items for a given site.
*
Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-media-errors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Retrieves all media validation errors for a specified site.
*
Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-media-item-errors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Retrieves all media validation errors for a certain media item of a specified site.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Retrieves the server ID for a given transient ID and site ID.
*
Expand Down
2 changes: 2 additions & 0 deletions client/state/selectors/get-media-library-selected-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import getMediaItem from 'calypso/state/selectors/get-media-item';

import 'calypso/state/media/init';

/**
* Retrieves the currently selected media library items for a specified site.
*
Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-media-query-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Gets the MediaQueryManager for the site
*
Expand Down
2 changes: 2 additions & 0 deletions client/state/selectors/get-media-sorted-by-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { values } from 'lodash';
import getMediaQueryManager from './get-media-query-manager';
import { sortItemsByDate } from 'calypso/lib/media/utils/sort-items-by-date';

import 'calypso/state/media/init';

/**
* Returns media for a specified site ID and query.
*
Expand Down
11 changes: 8 additions & 3 deletions client/state/selectors/get-media.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Returns media for a specified site ID and query.
*
*
* @param {object} query Query object
* @param {any} state the global state
* @param {string} siteId the site ID
* @param {object} query Query object
* @returns {?Array} Media
*/

export default function getMedia( state, siteId, query ) {
const queries = state.media.queries[ siteId ];

Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-next-page-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { get } from 'lodash';

/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Get the next page handle for the sites current media request
*
Expand Down
2 changes: 2 additions & 0 deletions client/state/selectors/get-next-page-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { isString, isNumber } from 'lodash';
import getNextPageHandle from 'calypso/state/selectors/get-next-page-handle';
import getCurrentMediaQuery from 'calypso/state/selectors/get-current-media-query';

import 'calypso/state/media/init';

const DEFAULT_QUERY = Object.freeze( { number: 20 } );

/**
Expand Down
5 changes: 5 additions & 0 deletions client/state/selectors/get-transient-media-item.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Retrieves a transient media item by ID for a given site.
*
Expand Down
2 changes: 2 additions & 0 deletions client/state/selectors/has-next-media-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import getNextPageHandle from 'calypso/state/selectors/get-next-page-handle';

import 'calypso/state/media/init';

export default function hasNextMediaPage( state, siteId ) {
if (
! ( siteId in state.media.fetching ) ||
Expand Down
6 changes: 6 additions & 0 deletions client/state/selectors/is-fetching-next-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
* External dependencies
*/
import { get } from 'lodash';

/**
* Internal dependencies
*/
import createSelector from 'calypso/lib/create-selector';

import 'calypso/state/media/init';

export default createSelector(
/**
* Returns true if media is being requested for a specified site ID and query.
Expand Down
6 changes: 5 additions & 1 deletion client/state/selectors/is-requesting-media-item.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/**
* External dependencies
*/

import { get } from 'lodash';

/**
* Internal dependencies
*/
import 'calypso/state/media/init';

/**
* Returns true if the media is being requested for a specified site ID and media ID.
*
Expand Down
5 changes: 3 additions & 2 deletions client/state/selectors/is-requesting-media.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
* Internal dependencies
*/

import MediaQueryManager from 'calypso/lib/query-manager/media';

import 'calypso/state/media/init';

/**
* Returns true if media is being requested for a specified site ID and query.
*
* @param {object} state Global state tree
* @param {number} siteId Site ID
* @param {object} query Query object
* @returns {bool} True if media is being requested
* @returns {boolean} True if media is being requested
*/
export default function isRequestingMedia( state, siteId, query ) {
const queryRequests = state.media.queryRequests[ siteId ];
Expand Down

0 comments on commit 4b2d92c

Please sign in to comment.