Skip to content

Commit

Permalink
Refactor Redux things
Browse files Browse the repository at this point in the history
  • Loading branch information
umurkontaci committed Mar 4, 2016
1 parent 32ea8c0 commit 55dcd51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const GoogleAppsUsers = React.createClass( {
},

canAddUsers() {
let domainsInContext = this.props.selectedDomainName ? [ getSelectedDomain( this.props ) ] : this.props.domains.list;
return domainsInContext.some( domain => domain.googleAppsSubscription.ownedByUserId === this.props.user.ID );
const domainsInContext = this.props.selectedDomainName
? [ getSelectedDomain( this.props ) ]
: this.props.domains.list;
return domainsInContext.some( domain =>
domain.googleAppsSubscription.ownedByUserId === this.props.user.ID
);
},

generateClickHandler( user ) {
Expand All @@ -51,7 +55,9 @@ const GoogleAppsUsers = React.createClass( {
label={ this.translate( 'Google Apps Users' ) }>
{ this.canAddUsers() && (
<a
href={ paths.domainManagementAddGoogleApps( this.props.selectedSite.slug, this.props.selectedDomainName ) }
href={ paths.domainManagementAddGoogleApps(
this.props.selectedSite.slug, this.props.selectedDomainName
) }
className="button is-compact is-primary"
onClick={ this.goToAddGoogleApps }>
{ this.translate( 'Add Google Apps User' ) }
Expand Down
11 changes: 9 additions & 2 deletions client/state/google-apps-users/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,28 @@ import property from 'lodash/property';
import {
GOOGLE_APPS_USERS_FETCH,
GOOGLE_APPS_USERS_FETCH_COMPLETED,
GOOGLE_APPS_USERS_FETCH_FAILED
GOOGLE_APPS_USERS_FETCH_FAILED,
SERIALIZE,
DESERIALIZE
} from 'state/action-types';

export function items( state = [], action ) {
switch ( action.type ) {
case GOOGLE_APPS_USERS_FETCH_COMPLETED:
return uniqBy( state.concat( action.items ), property( 'email' ) );
break;
case SERIALIZE:
return [];
case DESERIALIZE:
return [];
}
return state;
}

export function loaded( state = false, action ) {
switch ( action.type ) {
case GOOGLE_APPS_USERS_FETCH:
case SERIALIZE:
case DESERIALIZE:
return false;
case GOOGLE_APPS_USERS_FETCH_FAILED:
case GOOGLE_APPS_USERS_FETCH_COMPLETED:
Expand Down
16 changes: 11 additions & 5 deletions client/state/google-apps-users/selectors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import createSelector from 'lib/create-selector';

function getGoogleAppsUsersState( state ) {
return state.googleAppsUsers;
}

export function getByDomain( state, domain ) {
return getGoogleAppsUsersState( state ).items.filter( item => item.domain === domain );
function createGoogleAppsUsersSelector( fn ) {
return createSelector( fn, ( state ) => [ getGoogleAppsUsersState( state ) ] );
}

export function getBySite( state, siteId ) {
return getGoogleAppsUsersState( state ).items.filter( item => item.site_id === siteId );
}
export const getByDomain = createGoogleAppsUsersSelector(
( state, domainName ) => getGoogleAppsUsersState( state ).items.filter( item => item.domain === domainName )
);

export const getBySite = createGoogleAppsUsersSelector(
( state, siteId ) => getGoogleAppsUsersState( state ).items.filter( item => item.site_id === siteId )
);

export function getAll( state ) {
return getGoogleAppsUsersState( state );
Expand Down

0 comments on commit 55dcd51

Please sign in to comment.