Skip to content

Commit

Permalink
NotificationSettings: Update toggleState to be slimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
spen committed Nov 23, 2018
1 parent 0398bc4 commit 6f2db3e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 107 deletions.
9 changes: 8 additions & 1 deletion client/lib/notification-settings-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ const NotificationSettingsStore = createReducerStore( ( state, payload ) => {
}
case actionTypes.TOGGLE_SETTING: {
return {
...toggleSetting( state, source, stream, setting ),
...state,
settings: {
...state.settings,
dirty: {
...state.settings.dirty,
...toggleSetting( state, source, stream, setting ),
},
},
status: null,
};
}
Expand Down
148 changes: 42 additions & 106 deletions client/lib/notification-settings-store/toggle-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,129 +4,65 @@
*/
import { find, get, without } from 'lodash';

const toggleWpcom = ( state, source, stream, setting ) => ( {
...state,
settings: {
...get( state, 'settings' ),
dirty: {
...get( state, 'settings.dirty' ),
wpcom: {
...get( state, 'settings.dirty.wpcom' ),
[ setting ]: ! get( state, [ 'settings', 'dirty', 'wpcom', setting ] ),
},
},
const toggleInStream = ( streamName, stream, setting ) => ( {
[ streamName ]: {
...stream,
[ setting ]: ! get( stream, setting ),
},
} );

const toggleOther = ( state, source, stream, setting ) => {
if ( isNaN( stream ) ) {
return {
...state,
settings: {
...get( state, 'settings' ),
dirty: {
...get( state, 'settings.dirty' ),
other: {
...get( state, 'settings.dirty.other' ),
[ stream ]: {
...get( state, [ 'settings', 'dirty', 'other', stream ] ),
[ setting ]: ! get( state, [ 'settings', 'dirty', 'other', stream, setting ] ),
},
},
},
},
};
}

const devices = get( state, 'settings.dirty.other.devices' );
const device = find( devices, ( { device_id } ) => device_id === parseInt( stream, 10 ) );
const deviceSetting = get( device, setting );

return {
...state,
settings: {
...get( state, 'settings' ),
dirty: {
...get( state, 'settings.dirty' ),
other: {
...get( state, 'settings.dirty.other' ),
devices: [
...without( devices, device ),
{
...device,
[ setting ]: ! deviceSetting,
},
],
},
},
},
};
};

const toggleBlog = ( state, source, stream, setting ) => {
const blogs = get( state, 'settings.dirty.blogs' );
const blog = find( blogs, ( { blog_id } ) => blog_id === parseInt( source, 10 ) );

if ( isNaN( stream ) ) {
return {
...state,
settings: {
...state.settings,
dirty: {
...get( state, 'settings.dirty' ),
blogs: [
...without( blogs, blog ),
{
...blog,
[ stream ]: {
...get( blog, stream ),
[ setting ]: ! get( blog, [ stream, setting ] ),
},
},
],
},
},
};
}

const devices = get( blog, 'devices', [] );
const device = find( devices, ( { device_id } ) => device_id === parseInt( stream, 10 ) );
const toggleInDevice = ( devices, deviceId, setting ) => {
const device = find( devices, ( { device_id } ) => device_id === parseInt( deviceId, 10 ) );
const deviceSetting = get( device, setting );

return {
...state,
settings: {
...state.settings,
dirty: {
...state.settings.dirty,
blogs: [
...without( blogs, blog ),
{
...blog,
devices: [
...without( devices, device ),
{
...device,
[ setting ]: ! deviceSetting,
},
],
},
],
devices: [
...without( devices, device ),
{
...device,
[ setting ]: ! deviceSetting,
},
},
],
};
};

export default {
wpcom( state, source, stream, setting ) {
return toggleWpcom( state, source, stream, setting );
return toggleInStream( 'wpcom', get( state, 'settings.dirty.wpcom' ), setting );
},

other( state, source, stream, setting ) {
return toggleOther( state, source, stream, setting );
const devices = get( state, 'settings.dirty.other.devices' );

return {
other: {
...get( state, 'settings.dirty.other' ),
...( isNaN( stream )
? toggleInStream(
stream,
get( state, [ 'settings', 'dirty', 'other', stream ] ),
setting
)
: toggleInDevice( devices, stream, setting ) ),
},
};
},

blog( state, source, stream, setting ) {
return toggleBlog( state, source, stream, setting );
const blogs = get( state, 'settings.dirty.blogs' );
const blog = find( blogs, ( { blog_id } ) => blog_id === parseInt( source, 10 ) );
const devices = get( blog, 'devices', [] );

return {
blogs: [
...without( blogs, blog ),
{
...blog,
...( isNaN( stream )
? toggleInStream( stream, get( blog, stream ), setting )
: toggleInDevice( devices, stream, setting ) ),
},
],
};
},
};

0 comments on commit 6f2db3e

Please sign in to comment.