Skip to content

Commit

Permalink
Support User: Changes based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jordwest committed Jan 25, 2016
1 parent ac1d2ac commit 4d2530e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
7 changes: 5 additions & 2 deletions client/lib/user/dev-support-user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a temporary file to assist development of the support user feature.
*/
import { compose } from 'lodash';

import config from 'config';

Expand All @@ -13,9 +14,11 @@ import { supportUserFetchToken, supportUserRestore } from 'state/support/actions
*/
export default function( user, reduxStore ) {
if ( config.isEnabled( 'support-user' ) ) {
const dispatch = reduxStore.dispatch.bind( reduxStore );

window.supportUser = {
login: ( ...args ) => reduxStore.dispatch( supportUserFetchToken( ...args ) ),
logout: ( ...args ) => reduxStore.dispatch( supportUserRestore( ...args ) )
login: compose( dispatch, supportUserFetchToken ),
logout: compose( dispatch, supportUserRestore )
};
}
}
14 changes: 7 additions & 7 deletions client/lib/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ User.prototype.set = function( attributes ) {

User.prototype.changeUser = function( username, password, callback ) {
if ( config.isEnabled( 'support-user' ) ) {
wpcom.changeUser( username, password, function( error ) {
if ( ! error ) {
this.once( 'change', () => callback() );
this.clear();
this.fetch();
} else {
callback( error );
return wpcom.changeUser( username, password, function( error ) {
if ( error ) {
return callback( error );
}

this.once( 'change', () => callback() );
this.clear();
this.fetch();
}.bind( this ) );
}
};
Expand Down
34 changes: 16 additions & 18 deletions client/state/support/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,28 @@ import userSettings from 'lib/user-settings';
*/
export function supportUserFetchToken( supportUser, supportPassword ) {
return ( dispatch ) => {
if ( !supportUser || !supportPassword ) {
return;
}

dispatch( {
type: SUPPORT_USER_FETCH_TOKEN,
supportUser
} );

if ( supportUser && supportPassword ) {
let user = new User();
const user = new User();

user.changeUser(
supportUser,
supportPassword,
( error ) => {
if ( error ) {
dispatch( supportUserDeactivated( error.message ) );
} else {
let userData = Object.assign( {}, user.data );
dispatch( supportUserActivated( userData ) );
}
},
( tokenError ) => {
dispatch( supportUserDeactivated( tokenError.message ) );
}
);
}
const activateSupportUser = ( userData ) =>
dispatch( supportUserActivated( Object.assign( {}, user.data ) ) );

const tokenErrorCallback = ( error ) =>
dispatch( supportUserDeactivated( error.message ) );

const changeUserCallback = error => error
? tokenErrorCallback( error )
: activateSupportUser();

user.changeUser( supportUser, supportPassword, changeUserCallback, tokenErrorCallback );
}
}
/**
Expand Down
22 changes: 11 additions & 11 deletions client/state/support/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
SUPPORT_USER_FETCH_TOKEN
} from 'state/action-types';

function isSupportUser( state = false, action ) {
switch ( action.type ) {
function isSupportUser( state = false, { type } ) {
switch ( type ) {
case SUPPORT_USER_ACTIVATED:
return true;
case SUPPORT_USER_DEACTIVATED:
Expand All @@ -19,26 +19,26 @@ function isSupportUser( state = false, action ) {
return state;
}

function userData( state = {}, action ) {
switch ( action.type ) {
function userData( state = {}, { type, userData } ) {
switch ( type ) {
case SUPPORT_USER_ACTIVATED:
return action.userData;
return userData;
case SUPPORT_USER_DEACTIVATED:
return null;
}

return state;
}

function errorMessage( state = null, action ) {
switch ( action.type ) {
function errorMessage( state = null, { type, error } ) {
switch ( type ) {
case SUPPORT_USER_FETCH_TOKEN:
return null;
case SUPPORT_USER_ACTIVATED:
return null;
case SUPPORT_USER_DEACTIVATED:
if ( action.error ) {
return action.error;
if ( error ) {
return error;
}
return null;
}
Expand All @@ -52,8 +52,8 @@ function errorMessage( state = null, action ) {
* @param {object} action
* @return {Boolean}
*/
function isTransitioning( state = false, action ) {
switch ( action.type ) {
function isTransitioning( state = false, { type } ) {
switch ( type ) {
case SUPPORT_USER_FETCH_TOKEN:
return true;
case SUPPORT_USER_ACTIVATED:
Expand Down

0 comments on commit 4d2530e

Please sign in to comment.