Skip to content

Commit

Permalink
Resolves #5299
Browse files Browse the repository at this point in the history
Previously, the importer subsystem was using actions to dispatch changes
and these actions were all defined inside of `client/lib/importer`

As part of #5046 (conversion to a single global Redux tree), this commit
defines new action types inside of `client/state/action-types.js` and
converts the importer to use these globally-defined actions instead of
the locally-defined actions.

There are no visual or functional changes and this code should behave
exactly the same as before.
  • Loading branch information
dmsnell committed May 9, 2016
1 parent 46592d3 commit c0bbc0a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 89 deletions.
65 changes: 33 additions & 32 deletions client/lib/importer/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ const wpcom = require( 'lib/wp' ).undocumented();
/**
* Internal dependencies
*/
import { actionTypes, appStates } from './constants';
import {
IMPORTS_AUTHORS_SET_MAPPING,
IMPORTS_AUTHORS_START_MAPPING,
IMPORTS_FETCH,
IMPORTS_FETCH_FAILED,
IMPORTS_FETCH_COMPLETED,
IMPORTS_IMPORT_CANCEL,
IMPORTS_IMPORT_LOCK,
IMPORTS_IMPORT_RESET,
IMPORTS_IMPORT_START,
IMPORTS_IMPORT_UNLOCK,
IMPORTS_START_IMPORTING,
IMPORTS_UPLOAD_FAILED,
IMPORTS_UPLOAD_COMPLETED,
IMPORTS_UPLOAD_SET_PROGRESS,
IMPORTS_UPLOAD_START,
} from 'state/action-types';
import { appStates } from './constants';
import { fromApi, toApi } from './common';

const ID_GENERATOR_PREFIX = 'local-generated-id-';
Expand All @@ -31,21 +48,16 @@ const expiryOrder = ( siteId, importerId ) => toApi( { importerId, importerState
/** Creates a request object to start performing the actual import */
const importOrder = importerStatus => toApi( Object.assign( {}, importerStatus, { importerState: appStates.IMPORTING } ) );

const apiStart = () => Dispatcher.handleViewAction( { type: actionTypes.API_REQUEST } );
const apiSuccess = data => {
Dispatcher.handleViewAction( { type: actionTypes.API_SUCCESS } );

return data;
};
const apiStart = () => Dispatcher.handleViewAction( { type: IMPORTS_FETCH } );
const apiFailure = data => {
Dispatcher.handleViewAction( { type: actionTypes.API_FAILURE } );
Dispatcher.handleViewAction( { type: IMPORTS_FETCH_FAILED } );

return data;
};
const setImportLock = ( shouldEnableLock, importerId ) => {
const type = shouldEnableLock
? actionTypes.LOCK_IMPORT
: actionTypes.UNLOCK_IMPORT;
? IMPORTS_IMPORT_LOCK
: IMPORTS_IMPORT_UNLOCK;

Dispatcher.handleViewAction( { type, importerId } );
};
Expand All @@ -56,7 +68,7 @@ const asArray = a => [].concat( a );

function receiveImporterStatus( importerStatus ) {
Dispatcher.handleViewAction( {
type: actionTypes.RECEIVE_IMPORT_STATUS,
type: IMPORTS_FETCH_COMPLETED,
importerStatus
} );
}
Expand All @@ -65,7 +77,7 @@ export function cancelImport( siteId, importerId ) {
lockImport( importerId );

Dispatcher.handleViewAction( {
type: actionTypes.CANCEL_IMPORT,
type: IMPORTS_IMPORT_CANCEL,
importerId,
siteId
} );
Expand All @@ -79,15 +91,14 @@ export function cancelImport( siteId, importerId ) {
apiStart();
wpcom
.updateImporter( siteId, cancelOrder( siteId, importerId ) )
.then( apiSuccess )
.then( fromApi )
.then( receiveImporterStatus )
.catch( apiFailure );
}

export function failUpload( importerId, { message: error } ) {
Dispatcher.handleViewAction( {
type: actionTypes.FAIL_UPLOAD,
type: IMPORTS_UPLOAD_FAILED,
importerId,
error
} );
Expand All @@ -97,7 +108,6 @@ export function fetchState( siteId ) {
apiStart();

return wpcom.fetchImporterState( siteId )
.then( apiSuccess )
.then( asArray )
.then( importers => importers.map( fromApi ) )
.then( importers => importers.map( receiveImporterStatus ) )
Expand All @@ -106,14 +116,14 @@ export function fetchState( siteId ) {

export function finishUpload( importerId, importerStatus ) {
Dispatcher.handleViewAction( {
type: actionTypes.FINISH_UPLOAD,
type: IMPORTS_UPLOAD_COMPLETED,
importerId, importerStatus
} );
}

export function mapAuthor( importerId, sourceAuthor, targetAuthor ) {
Dispatcher.handleViewAction( {
type: actionTypes.MAP_AUTHORS,
type: IMPORTS_AUTHORS_SET_MAPPING,
importerId,
sourceAuthor,
targetAuthor
Expand All @@ -125,40 +135,31 @@ export function resetImport( siteId, importerId ) {
lockImport( importerId );

Dispatcher.handleViewAction( {
type: actionTypes.RESET_IMPORT,
type: IMPORTS_IMPORT_RESET,
importerId,
siteId
} );

apiStart();
wpcom
.updateImporter( siteId, expiryOrder( siteId, importerId ) )
.then( apiSuccess )
.then( fromApi )
.then( receiveImporterStatus )
.catch( apiFailure );
}

// Use when developing to force a new state into the store
export function setState( newState ) {
Dispatcher.handleViewAction( {
type: actionTypes.DEV_SET_STATE,
newState
} );
}

export function startMappingAuthors( importerId ) {
lockImport( importerId );

Dispatcher.handleViewAction( {
type: actionTypes.START_MAPPING_AUTHORS,
type: IMPORTS_AUTHORS_START_MAPPING,
importerId
} );
}

export function setUploadProgress( importerId, data ) {
Dispatcher.handleViewAction( {
type: actionTypes.SET_UPLOAD_PROGRESS,
type: IMPORTS_UPLOAD_SET_PROGRESS,
uploadLoaded: data.uploadLoaded,
uploadTotal: data.uploadTotal,
importerId
Expand All @@ -170,7 +171,7 @@ export function startImport( siteId, importerType ) {
let importerId = `${ ID_GENERATOR_PREFIX }${ Math.round( Math.random() * 10000 ) }`;

Dispatcher.handleViewAction( {
type: actionTypes.START_IMPORT,
type: IMPORTS_IMPORT_START,
importerId,
importerType,
siteId
Expand All @@ -183,7 +184,7 @@ export function startImporting( importerStatus ) {
unlockImport( importerId );

Dispatcher.handleViewAction( {
type: actionTypes.START_IMPORTING,
type: IMPORTS_START_IMPORTING,
importerId
} );

Expand Down Expand Up @@ -213,7 +214,7 @@ export function startUpload( importerStatus, file ) {
.catch( partial( failUpload, importerId ) );

Dispatcher.handleViewAction( {
type: actionTypes.START_UPLOAD,
type: IMPORTS_UPLOAD_START,
filename: file.name,
importerId
} );
Expand Down
27 changes: 0 additions & 27 deletions client/lib/importer/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,3 @@ export const appStates = Object.freeze( {

export const WORDPRESS = 'importer-type-wordpress';
export const MEDIUM = 'importer-type-medium';

export const actionTypes = Object.freeze( {
API_REQUEST: 'importer-api-request',
API_FAILURE: 'importer-api-failure',
API_SUCCESS: 'importer-api-success',

LOCK_IMPORT: 'importer-lock-import',
UNLOCK_IMPORT: 'importer-unlock-import',

RECEIVE_IMPORT_STATUS: 'importer-receive-import-status',

CANCEL_IMPORT: 'importer-cancel',
FAIL_UPLOAD: 'importer-fail-upload',
FINISH_UPLOAD: 'importer-finish-upload',
RESET_IMPORT: 'importer-reset',
SET_UPLOAD_PROGRESS: 'importer-set-upload-progress',
START_IMPORT: 'importer-start-import',
START_UPLOAD: 'importer-start-upload',

MAP_AUTHORS: 'importer-map-authors',
START_MAPPING_AUTHORS: 'importer-start-mapping-authors',

START_IMPORTING: 'importer-start-importing',

DEV_SET_STATE: 'dev-set-state',
RESET_STORE: 'dev-reset-store'
} );
68 changes: 38 additions & 30 deletions client/lib/importer/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ import partial from 'lodash/partial';
/**
* Internal dependencies
*/
import { actionTypes, appStates } from './constants';
import {
IMPORTS_AUTHORS_SET_MAPPING,
IMPORTS_AUTHORS_START_MAPPING,
IMPORTS_FETCH,
IMPORTS_FETCH_FAILED,
IMPORTS_FETCH_COMPLETED,
IMPORTS_IMPORT_CANCEL,
IMPORTS_IMPORT_LOCK,
IMPORTS_IMPORT_RESET,
IMPORTS_IMPORT_START,
IMPORTS_IMPORT_UNLOCK,
IMPORTS_START_IMPORTING,
IMPORTS_UPLOAD_FAILED,
IMPORTS_UPLOAD_COMPLETED,
IMPORTS_UPLOAD_SET_PROGRESS,
IMPORTS_UPLOAD_START,
} from 'state/action-types';
import { appStates } from './constants';
import { createReducerStore } from 'lib/store';

/**
Expand All @@ -32,10 +49,10 @@ const shouldRemove = importer => removableStates.some( partial( equals, importer

const adjustImporterLock = ( state, { action } ) => {
switch ( action.type ) {
case actionTypes.LOCK_IMPORT:
case IMPORTS_IMPORT_LOCK:
return state.setIn( [ 'importerLocks', action.importerId ], true );

case actionTypes.UNLOCK_IMPORT:
case IMPORTS_IMPORT_UNLOCK:
return state.setIn( [ 'importerLocks', action.importerId ], false );

default:
Expand All @@ -48,60 +65,48 @@ const ImporterStore = createReducerStore( function( state, payload ) {
newState;

switch ( action.type ) {
case actionTypes.RESET_STORE:
return initialState;

case actionTypes.DEV_SET_STATE:
// Convert the importer list into an object
action.newState.importers = action.newState.importers
.reduce( ( total, importer ) => Object.assign( total, { [ importer.id ]: importer } ), {} );

newState = Immutable.fromJS( action.newState );
newState = Immutable.is( state, newState ) ? state : newState;
break;

case actionTypes.API_REQUEST:
case IMPORTS_FETCH:
newState = state.setIn( [ 'api', 'isFetching' ], true );
break;

case actionTypes.API_FAILURE:
case IMPORTS_FETCH_FAILED:
newState = state
.setIn( [ 'api', 'isFetching' ], false )
.updateIn( [ 'api', 'retryCount' ], increment );
break;

case actionTypes.API_SUCCESS:
case IMPORTS_FETCH_COMPLETED:
newState = state
.setIn( [ 'api', 'isFetching' ], false )
.setIn( [ 'api', 'isHydrated' ], true )
.setIn( [ 'api', 'retryCount' ], 0 );
break;

case actionTypes.CANCEL_IMPORT:
case actionTypes.RESET_IMPORT:
case IMPORTS_IMPORT_CANCEL:
case IMPORTS_IMPORT_RESET:
// Remove the specified importer from the list of current importers
newState = state.update( 'importers', importers => {
return importers.filterNot( importer => importer.get( 'importerId' ) === action.importerId );
} );
break;

case actionTypes.FAIL_UPLOAD:
case IMPORTS_UPLOAD_FAILED:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.UPLOAD_FAILURE )
.setIn( [ 'importers', action.importerId, 'errorData' ], { type: 'uploadError', description: action.error } );
break;

case actionTypes.FINISH_UPLOAD:
case IMPORTS_UPLOAD_COMPLETED:
newState = state
.deleteIn( [ 'importers' ], action.importerId )
.setIn( [ 'importers', action.importerStatus.importerId ], Immutable.fromJS( action.importerStatus ) );
break;

case actionTypes.START_MAPPING_AUTHORS:
case IMPORTS_AUTHORS_START_MAPPING:
newState = state.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.MAP_AUTHORS );
break;

case actionTypes.MAP_AUTHORS:
case IMPORTS_AUTHORS_SET_MAPPING:
newState = state.updateIn( [ 'importers', action.importerId, 'customData', 'sourceAuthors' ], authors => (
authors.map( author => {
if ( action.sourceAuthor.id !== author.get( 'id' ) ) {
Expand All @@ -113,8 +118,11 @@ const ImporterStore = createReducerStore( function( state, payload ) {
) );
break;

case actionTypes.RECEIVE_IMPORT_STATUS:
newState = state.setIn( [ 'api', 'isHydrated' ], true );
case IMPORTS_FETCH_COMPLETED:
newState = state
.setIn( [ 'api', 'isFetching' ], false )
.setIn( [ 'api', 'isHydrated' ], true )
.setIn( [ 'api', 'retryCount' ], 0 );

if ( newState.getIn( [ 'importerLocks', action.importerStatus.importerId ], false ) ) {
break;
Expand All @@ -131,13 +139,13 @@ const ImporterStore = createReducerStore( function( state, payload ) {
.update( 'importers', importers => importers.filterNot( shouldRemove ) );
break;

case actionTypes.SET_UPLOAD_PROGRESS:
case IMPORTS_UPLOAD_SET_PROGRESS:
newState = state.setIn( [ 'importers', action.importerId, 'percentComplete' ],
action.uploadLoaded / ( action.uploadTotal + Number.EPSILON ) * 100
);
break;

case actionTypes.START_IMPORT:
case IMPORTS_IMPORT_START:
const newImporter = Immutable.fromJS( {
importerId: action.importerId,
type: action.importerType,
Expand All @@ -150,12 +158,12 @@ const ImporterStore = createReducerStore( function( state, payload ) {
.setIn( [ 'importers', action.importerId ], newImporter );
break;

case actionTypes.START_IMPORTING:
case IMPORTS_START_IMPORTING:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.IMPORTING );
break;

case actionTypes.START_UPLOAD:
case IMPORTS_UPLOAD_START:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.UPLOADING )
.setIn( [ 'importers', action.importerId, 'filename' ], action.filename );
Expand Down
15 changes: 15 additions & 0 deletions client/state/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ export const FETCH_WPORG_PLUGIN_DATA = 'FETCH_WPORG_PLUGIN_DATA';
export const GOOGLE_APPS_USERS_FETCH = 'GOOGLE_APPS_USERS_FETCH';
export const GOOGLE_APPS_USERS_FETCH_COMPLETED = 'GOOGLE_APPS_USERS_FETCH_COMPLETED';
export const GOOGLE_APPS_USERS_FETCH_FAILED = 'GOOGLE_APPS_USERS_FETCH_FAILED';
export const IMPORTS_AUTHORS_SET_MAPPING = 'IMPORTS_AUTHORS_SET_MAPPING';
export const IMPORTS_AUTHORS_START_MAPPING = 'IMPORTS_AUTHORS_START_MAPPING';
export const IMPORTS_FETCH = 'IMPORTS_FETCH';
export const IMPORTS_FETCH_FAILED = 'IMPORTS_FETCH_FAILED';
export const IMPORTS_FETCH_COMPLETED = 'IMPORTS_FETCH_COMPLETED';
export const IMPORTS_IMPORT_CANCEL = 'IMPORTS_IMPORT_CANCEL';
export const IMPORTS_IMPORT_LOCK = 'IMPORTS_IMPORT_LOCK';
export const IMPORTS_IMPORT_RESET = 'IMPORTS_IMPORT_RESET';
export const IMPORTS_IMPORT_START = 'IMPORTS_IMPORT_START';
export const IMPORTS_IMPORT_UNLOCK = 'IMPORTS_IMPORT_UNLOCK';
export const IMPORTS_START_IMPORTING = 'IMPORTS_START_IMPORTING';
export const IMPORTS_UPLOAD_FAILED = 'IMPORTS_UPDLOAD_FAILED';
export const IMPORTS_UPLOAD_COMPLETED = 'IMPORTS_UPLOAD_COMPLETED';
export const IMPORTS_UPLOAD_SET_PROGRESS = 'IMPORTS_UPLOAD_SET_PROGRESS';
export const IMPORTS_UPLOAD_START = 'IMPORTS_UPLOAD_START';
export const JETPACK_CONNECT_CHECK_URL = 'JETPACK_CONNECT_CHECK_URL';
export const JETPACK_CONNECT_CHECK_URL_RECEIVE = 'JETPACK_CONNECT_CHECK_URL_RECEIVE';
export const JETPACK_CONNECT_DISMISS_URL_STATUS = 'JETPACK_CONNECT_DISMISS_URL_STATUS';
Expand Down

0 comments on commit c0bbc0a

Please sign in to comment.