Skip to content

Commit

Permalink
Importer: Use globally-defined action types (#5304)
Browse files Browse the repository at this point in the history
* Resolves #5299

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 authored and roundhill committed May 17, 2016
1 parent 1bfc49b commit b6ef128
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 81 deletions.
60 changes: 35 additions & 25 deletions client/lib/importer/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@ 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_RECEIVE,
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 +49,21 @@ 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 apiStart = () => Dispatcher.handleViewAction( { type: IMPORTS_FETCH } );
const apiSuccess = data => {
Dispatcher.handleViewAction( { type: actionTypes.API_SUCCESS } );
Dispatcher.handleViewAction( { type: IMPORTS_FETCH_COMPLETED } );

return data;
};
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 +74,7 @@ const asArray = a => [].concat( a );

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

Dispatcher.handleViewAction( {
type: actionTypes.CANCEL_IMPORT,
type: IMPORTS_IMPORT_CANCEL,
importerId,
siteId
} );
Expand All @@ -87,7 +105,7 @@ export function cancelImport( siteId, importerId ) {

export function failUpload( importerId, { message: error } ) {
Dispatcher.handleViewAction( {
type: actionTypes.FAIL_UPLOAD,
type: IMPORTS_UPLOAD_FAILED,
importerId,
error
} );
Expand All @@ -106,14 +124,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,7 +143,7 @@ export function resetImport( siteId, importerId ) {
lockImport( importerId );

Dispatcher.handleViewAction( {
type: actionTypes.RESET_IMPORT,
type: IMPORTS_IMPORT_RESET,
importerId,
siteId
} );
Expand All @@ -139,26 +157,18 @@ export function resetImport( siteId, importerId ) {
.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 +180,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 +193,7 @@ export function startImporting( importerStatus ) {
unlockImport( importerId );

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

Expand Down Expand Up @@ -213,7 +223,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'
} );
67 changes: 40 additions & 27 deletions client/lib/importer/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ 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_RECEIVE,
IMPORTS_IMPORT_RESET,
IMPORTS_IMPORT_START,
IMPORTS_IMPORT_UNLOCK,
IMPORTS_START_IMPORTING,
IMPORTS_STORE_RESET,
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 +51,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 +67,54 @@ 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;
case IMPORTS_STORE_RESET:
// this is here to enable
// unit-testing the store
newState = initialState;
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,7 +126,7 @@ const ImporterStore = createReducerStore( function( state, payload ) {
) );
break;

case actionTypes.RECEIVE_IMPORT_STATUS:
case IMPORTS_IMPORT_RECEIVE:
newState = state.setIn( [ 'api', 'isHydrated' ], true );

if ( newState.getIn( [ 'importerLocks', action.importerStatus.importerId ], false ) ) {
Expand All @@ -131,13 +144,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 +163,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
4 changes: 2 additions & 2 deletions client/lib/importer/test/api-interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import partial from 'lodash/partial';
import { nock, useNock } from 'test/helpers/use-nock';

import { fetchState } from '../actions';
import { actionTypes } from '../constants';
import { IMPORTS_STORE_RESET } from 'state/action-types';
import store from '../store';

const testSiteId = 'en.blog.wordpress.com';
const fetchTestState = partial( fetchState, testSiteId );
const hydratedState = () => store.get().getIn( [ 'api', 'isHydrated' ] );
const resetStore = () => Dispatcher.handleViewAction( { type: actionTypes.RESET_STORE } );
const resetStore = () => Dispatcher.handleViewAction( { type: IMPORTS_STORE_RESET } );

const queuePayload = payload =>
nock( 'https://public-api.wordpress.com:443' )
Expand Down
17 changes: 17 additions & 0 deletions client/state/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ export const IMAGE_EDITOR_ROTATE_COUNTERCLOCKWISE = 'IMAGE_EDITOR_ROTATE_COUNTER
export const IMAGE_EDITOR_FLIP = 'IMAGE_EDITOR_FLIP';
export const IMAGE_EDITOR_SET_FILE_INFO = 'IMAGE_EDITOR_SET_FILE_INFO';
export const IMAGE_EDITOR_STATE_RESET = 'IMAGE_EDITOR_STATE_RESET';
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_RECEIVE = 'IMPORTS_IMPORT_RECEIVE';
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_STORE_RESET = 'IMPORTS_STORE_RESET';
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 b6ef128

Please sign in to comment.