Skip to content

Commit

Permalink
Remove redundant connectDispatcher HoC
Browse files Browse the repository at this point in the history
  • Loading branch information
spen committed Oct 16, 2018
1 parent 0c922b8 commit 2c69291
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 90 deletions.
60 changes: 25 additions & 35 deletions client/lib/importer/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Dispatcher from 'dispatcher';
import { flowRight, includes, partial } from 'lodash';
import { includes, partial } from 'lodash';
import wpLib from 'lib/wp';
const wpcom = wpLib.undocumented();

Expand Down Expand Up @@ -111,12 +111,6 @@ export function cancelImport( siteId, importerId ) {
.catch( apiFailure );
}

export const failUpload = importerId => ( { message: error } ) => ( {
type: IMPORTS_UPLOAD_FAILED,
importerId,
error,
} );

export function fetchState( siteId ) {
apiStart();

Expand All @@ -129,18 +123,19 @@ export function fetchState( siteId ) {
.catch( apiFailure );
}

export const finishUpload = importerId => importerStatus => ( {
export const createFinishUploadAction = ( importerId, importerStatus ) => ( {
type: IMPORTS_UPLOAD_COMPLETED,
importerId,
importerStatus,
} );

export const mapAuthor = ( importerId, sourceAuthor, targetAuthor ) => ( {
type: IMPORTS_AUTHORS_SET_MAPPING,
importerId,
sourceAuthor,
targetAuthor,
} );
export const mapAuthor = ( importerId, sourceAuthor, targetAuthor ) =>
Dispatcher.handleViewAction( {
type: IMPORTS_AUTHORS_SET_MAPPING,
importerId,
sourceAuthor,
targetAuthor,
} );

export function resetImport( siteId, importerId ) {
// We are done with this import session, so lock it away
Expand Down Expand Up @@ -196,17 +191,14 @@ export const setUploadProgress = ( importerId, data ) => ( {
importerId,
} );

export const startImport = ( siteId, importerType ) => {
// Use a fake ID until the server returns the real one
const importerId = `${ ID_GENERATOR_PREFIX }${ Math.round( Math.random() * 10000 ) }`;

return {
export const startImport = ( siteId, importerType ) =>
Dispatcher.handleViewAction( {
type: IMPORTS_IMPORT_START,
importerId,
// Use a fake ID until the server returns the real one
importerId: `${ ID_GENERATOR_PREFIX }${ Math.round( Math.random() * 10000 ) }`,
importerType,
siteId,
};
};
} );

export function startImporting( importerStatus ) {
const {
Expand All @@ -224,7 +216,7 @@ export function startImporting( importerStatus ) {
wpcom.updateImporter( siteId, importOrder( importerStatus ) );
}

export const startUpload = ( importerStatus, file ) => dispatch => {
export const startUpload = ( importerStatus, file ) => {
const {
importerId,
site: { ID: siteId },
Expand All @@ -236,7 +228,7 @@ export const startUpload = ( importerStatus, file ) => dispatch => {
file,

onprogress: event =>
dispatch(
Dispatcher.handleViewAction(
setUploadProgress( importerId, {
uploadLoaded: event.loaded,
uploadTotal: event.total,
Expand All @@ -247,20 +239,18 @@ export const startUpload = ( importerStatus, file ) => dispatch => {
} )
.then( data => Object.assign( data, { siteId } ) )
.then( fromApi )
.then(
flowRight(
dispatch,
finishUpload( importerId )
)
.then( importerData =>
Dispatcher.handleViewAction( createFinishUploadAction( importerId, importerData ) )
)
.catch(
flowRight(
dispatch,
failUpload( importerId )
)
.catch( error =>
Dispatcher.handleViewAction( {
type: IMPORTS_UPLOAD_FAILED,
importerId,
error: error.message,
} )
);

dispatch( {
Dispatcher.handleViewAction( {
type: IMPORTS_UPLOAD_START,
filename: file.name,
importerId,
Expand Down
14 changes: 2 additions & 12 deletions client/my-sites/importer/importer-header/start-button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import PropTypes from 'prop-types';
import { localize } from 'i18n-calypso';
import React from 'react';
import { flow, flowRight } from 'lodash';
import { flow } from 'lodash';
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import Button from 'components/forms/form-button';
import { startImport } from 'lib/importer/actions';
import { connectDispatcher } from 'my-sites/importer/dispatcher-converter';
import { recordTracksEvent } from 'state/analytics/actions';

class StartButton extends React.PureComponent {
Expand All @@ -32,11 +31,10 @@ class StartButton extends React.PureComponent {
const {
importerStatus: { type },
site: { ID: siteId },
startImportFn,
} = this.props;
const tracksType = type.endsWith( 'site-importer' ) ? type + '-wix' : type;

startImportFn( siteId, type );
startImport( siteId, type );

this.props.recordTracksEvent( 'calypso_importer_main_start_clicked', {
blog_id: siteId,
Expand All @@ -55,18 +53,10 @@ class StartButton extends React.PureComponent {
}
}

const mapDispatchToProps = dispatch => ( {
startImportFn: flowRight(
dispatch,
startImport
),
} );

export default flow(
connect(
null,
{ recordTracksEvent }
),
connectDispatcher( null, mapDispatchToProps ),
localize
)( StartButton );
20 changes: 7 additions & 13 deletions client/my-sites/importer/importing-pane.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import React from 'react';
import { connect } from 'react-redux';
import classNames from 'classnames';
import { numberFormat, translate, localize } from 'i18n-calypso';
import { has, omit } from 'lodash';
import { get, has, omit } from 'lodash';

/**
* Internal dependencies
*/
import { mapAuthor, startImporting } from 'lib/importer/actions';
import { appStates } from 'state/imports/constants';
import { connectDispatcher } from './dispatcher-converter';
import ProgressBar from 'components/progress-bar';
import AuthorMappingPane from './author-mapping-pane';
import Spinner from 'components/spinner';
Expand Down Expand Up @@ -221,10 +220,12 @@ class ImportingPane extends React.PureComponent {
this.maybeLoadHotJar();
}

handleOnMap = ( source, target ) =>
mapAuthor( get( this.props, 'importerStatus.importerId' ), source, target );

render() {
const {
importerStatus: { importerId, customData },
mapAuthorFor,
importerStatus: { customData },
site: { ID: siteId, name: siteName, single_user_site: hasSingleAuthor },
sourceType,
} = this.props;
Expand Down Expand Up @@ -262,7 +263,7 @@ class ImportingPane extends React.PureComponent {
{ this.isMapping() && (
<AuthorMappingPane
hasSingleAuthor={ hasSingleAuthor }
onMap={ mapAuthorFor( importerId ) }
onMap={ this.handleOnMap }
onStartImport={ () => startImporting( this.props.importerStatus ) }
siteId={ siteId }
sourceType={ sourceType }
Expand All @@ -289,14 +290,7 @@ class ImportingPane extends React.PureComponent {
}
}

const mapFluxDispatchToProps = dispatch => ( {
mapAuthorFor: importerId => ( source, target ) =>
setTimeout( () => {
dispatch( mapAuthor( importerId, source, target ) );
}, 0 ),
} );

export default connect(
null,
{ loadTrackingTool }
)( connectDispatcher( null, mapFluxDispatchToProps )( localize( ImportingPane ) ) );
)( localize( ImportingPane ) );
35 changes: 17 additions & 18 deletions client/my-sites/importer/site-importer/site-importer-input-pane.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { connect } from 'react-redux';
import Dispatcher from 'dispatcher';
import PropTypes from 'prop-types';
import { localize } from 'i18n-calypso';
import { noop, every, has, defer, get, trim, sortBy, reverse } from 'lodash';
import { noop, every, flow, has, defer, get, trim, sortBy, reverse } from 'lodash';
import url from 'url';
import moment from 'moment';

Expand All @@ -22,7 +22,12 @@ const wpcom = wpLib.undocumented();

import { toApi, fromApi } from 'lib/importer/common';

import { startMappingAuthors, startImporting, mapAuthor, finishUpload } from 'lib/importer/actions';
import {
mapAuthor,
startMappingAuthors,
startImporting,
createFinishUploadAction,
} from 'lib/importer/actions';
import user from 'lib/user';

import { appStates } from 'state/imports/constants';
Expand All @@ -32,7 +37,6 @@ import TextInput from 'components/forms/form-text-input';
import FormSelect from 'components/forms/form-select';

import SiteImporterSitePreview from './site-importer-site-preview';
import { connectDispatcher } from '../dispatcher-converter';

import { loadmShotsPreview } from './site-preview-actions';

Expand Down Expand Up @@ -93,11 +97,10 @@ class SiteImporterInputPane extends React.Component {
name: currentUserData.display_name,
};

const mappingFunction = this.props.mapAuthorFor( props.importerStatus.importerId );

// map all the authors to the current user
// TODO: when converting to redux, allow for multiple mappings in a single action
props.importerStatus.customData.sourceAuthors.forEach( author => {
mappingFunction( author, currentUser );
mapAuthor( props.importerStatus.importerId, author, currentUser );
} );
}, nextProps );
} else {
Expand Down Expand Up @@ -329,7 +332,7 @@ class SiteImporterInputPane extends React.Component {
} );

const data = fromApi( resp );
const action = finishUpload( this.props.importerStatus.importerId )( data );
const action = createFinishUploadAction( this.props.importerStatus.importerId, data );
defer( () => {
Dispatcher.handleViewAction( action );
} );
Expand Down Expand Up @@ -454,14 +457,10 @@ class SiteImporterInputPane extends React.Component {
}
}

const mapDispatchToProps = dispatch => ( {
mapAuthorFor: importerId => ( source, target ) =>
defer( () => {
dispatch( mapAuthor( importerId, source, target ) );
} ),
} );

export default connect(
null,
{ recordTracksEvent }
)( connectDispatcher( null, mapDispatchToProps )( localize( SiteImporterInputPane ) ) );
export default flow(
connect(
null,
{ recordTracksEvent }
),
localize
)( SiteImporterInputPane );
14 changes: 2 additions & 12 deletions client/my-sites/importer/uploading-pane.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PropTypes from 'prop-types';
import { localize } from 'i18n-calypso';
import React from 'react';
import classNames from 'classnames';
import { flowRight, includes, noop } from 'lodash';
import { includes, noop } from 'lodash';
import Gridicon from 'gridicons';

/**
Expand All @@ -19,7 +19,6 @@ import { appStates } from 'state/imports/constants';
import Button from 'components/forms/form-button';
import DropZone from 'components/drop-zone';
import ProgressBar from 'components/progress-bar';
import { connectDispatcher } from './dispatcher-converter';

class UploadingPane extends React.PureComponent {
static displayName = 'SiteSettingsUploadingPane';
Expand Down Expand Up @@ -111,8 +110,6 @@ class UploadingPane extends React.PureComponent {
};

startUpload = file => {
const { startUpload } = this.props;

if ( window.chrome && window.chrome.webstore ) {
/**
* This is a workaround for a Chrome issue that prevents file uploads from `calypso.localhost` through
Expand Down Expand Up @@ -158,11 +155,4 @@ class UploadingPane extends React.PureComponent {
}
}

const mapDispatchToProps = dispatch => ( {
startUpload: flowRight(
dispatch,
startUpload
),
} );

export default connectDispatcher( null, mapDispatchToProps )( localize( UploadingPane ) );
export default localize( UploadingPane );

0 comments on commit 2c69291

Please sign in to comment.