Skip to content

Commit

Permalink
refactor: Reducer and action cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed Apr 4, 2018
1 parent 87164b3 commit 91c9f9f
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 160 deletions.
66 changes: 35 additions & 31 deletions electron/renderer/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import uuid from 'uuid/v4';
import verifyObjectProperties from '../lib/verifyObjectProperties';

export const ADD_ACCOUNT = 'ADD_ACCOUNT';
export const DELETE_ACCOUNT = 'DELETE_ACCOUNT';
export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT';
export const UPDATE_ACCOUNT = 'UPDATE_ACCOUNT';
export const UPDATE_ACCOUNT_BADGE = 'UPDATE_ACCOUNT_BADGE';
export const DELETE_ACCOUNT = 'DELETE_ACCOUNT';
export const UPDATE_ACCOUNT_LIFECYCLE = 'UPDATE_ACCOUNT_LIFECYCLE';

export const addAccount = (withSession = true) => {
Expand All @@ -16,54 +16,58 @@ export const addAccount = (withSession = true) => {
};
};

export const updateAccount = (id, data) => {
export const deleteAccount = id => {
return {
data,
id,
type: UPDATE_ACCOUNT,
type: DELETE_ACCOUNT,
};
};

export const updateAccountLifecycle = (id, data) => {
export const switchAccount = id => {
return {
data,
id,
type: UPDATE_ACCOUNT_LIFECYCLE,
type: SWITCH_ACCOUNT,
};
};

export const switchAccount = id => {
export const updateAccount = (id, data) => {
return {
data,
id,
type: SWITCH_ACCOUNT,
type: UPDATE_ACCOUNT,
};
};

export const updateAccountBadge = (id, count) => {
export const updateAccountLifecycle = (id, data) => {
return {
count,
data,
id,
type: UPDATE_ACCOUNT_BADGE,
type: UPDATE_ACCOUNT_LIFECYCLE,
};
};

export const deleteAccount = id => {
export const updateAccountBadge = (id, count) => {
return {
count,
id,
type: DELETE_ACCOUNT,
type: UPDATE_ACCOUNT_BADGE,
};
};

export const HIDE_CONTEXT_MENUS = 'HIDE_CONTEXT_MENUS';
export const TOGGLE_ADD_ACCOUNT_VISIBILITY = 'TOGGLE_ADD_ACCOUNT_VISIBILITY';
export const TOGGLE_EDIT_ACCOUNT_VISIBILITY = 'TOGGLE_EDIT_ACCOUNT_VISIBILITY';

export const setAccountContextHidden = () => {
return {
type: 'HIDE_CONTEXT_MENUS',
type: HIDE_CONTEXT_MENUS,
};
};

export const toggleAddAccountMenuVisibility = (x, y) => {
return {
payload: { position: { x, y } },
type: 'TOGGLE_ADD_ACCOUNT_VISIBILITY',
payload: {position: {x, y}},
type: TOGGLE_ADD_ACCOUNT_VISIBILITY,
};
};

Expand All @@ -80,10 +84,10 @@ export const toggleEditAccountMenuVisibility = (
accountId,
isAtLeastAdmin,
lifecycle,
position: { x, y },
position: {x, y},
sessionId,
},
type: 'TOGGLE_EDIT_ACCOUNT_VISIBILITY',
type: TOGGLE_EDIT_ACCOUNT_VISIBILITY,
};
};

Expand All @@ -102,6 +106,18 @@ export const abortAccountCreation = id => {
};
};

export const addAccountWithSession = () => {
return (dispatch, getState) => {
const hasReachedAccountLimit = getState().accounts.length >= 3;

if (hasReachedAccountLimit) {
console.warn('Reached number of maximum accounts');
} else {
dispatch(addAccount());
}
};
};

export const updateAccountData = (id, data) => {
return (dispatch, getState) => {
const validatedAccountData = verifyObjectProperties(data, {
Expand Down Expand Up @@ -135,15 +151,3 @@ export const updateAccountBadgeCount = (id, count) => {
}
};
};

export const addAccountWithSession = () => {
return (dispatch, getState) => {
const hasReachedAccountLimit = getState().accounts.length >= 3;

if (hasReachedAccountLimit) {
console.warn('Reached number of maximum accounts');
} else {
dispatch(addAccount());
}
};
};
12 changes: 6 additions & 6 deletions electron/renderer/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ const App = props => (
<IsOnline>
<div
className="App"
onKeyDown={e => {
const modKeyPressed = (window.isMac && e.metaKey) || e.ctrlKey;
const isValidKey = ['1', '2', '3'].includes(e.key);
if (modKeyPressed && isValidKey && props.accountIds[e.key - 1]) {
props.switchAccount(props.accountIds[e.key - 1]);
onKeyDown={event => {
const modKeyPressed = (window.isMac && event.metaKey) || event.ctrlKey;
const isValidKey = ['1', '2', '3'].includes(event.key);
if (modKeyPressed && isValidKey && props.accountIds[event.key - 1]) {
props.switchAccount(props.accountIds[event.key - 1]);
}
}}
>
Expand All @@ -44,4 +44,4 @@ const App = props => (
</IsOnline>
);

export default connect(({ accounts }) => ({ accountIds: accounts.map(account => account.id) }), { switchAccount })(App);
export default connect(({accounts}) => ({accountIds: accounts.map(account => account.id)}), {switchAccount})(App);
6 changes: 3 additions & 3 deletions electron/renderer/src/components/IsOnline.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class IsOnline extends Component {

componentDidMount() {
if (this.state.isOnline === false) {
window.addEventListener('online', (event) => {
this.setState({ isOnline: true });
}, { once: true });
window.addEventListener('online', event => {
this.setState({isOnline: true});
}, {once: true});
}
}

Expand Down
2 changes: 1 addition & 1 deletion electron/renderer/src/components/PersonalIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { colorFromId } from '../lib/accentColor';

import './PersonalIcon.css';

const PersonalIcon = ({ account, accentID, onClick }) =>
const PersonalIcon = ({account, accentID, onClick}) =>
<div className="PersonalIcon" title={account.name} onClick={onClick} data-uie-name="item-team" data-uie-value={account.name}>
{account.visible &&
<div className="PersonalIcon-border" style={{borderColor: colorFromId(accentID)}}></div>
Expand Down
6 changes: 3 additions & 3 deletions electron/renderer/src/components/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*
*/

import { colorFromId } from '../lib/accentColor';
import { connect } from 'react-redux';
import { preventFocus } from '../lib/util';
import {colorFromId} from '../lib/accentColor';
import {connect} from 'react-redux';
import {preventFocus} from '../lib/util';
import AddAccountMenuTrigger from './context/AddAccountMenuTrigger';
import AddAccountMenu from './context/AddAccountMenu';
import EditAccountMenu from './context/EditAccountMenu';
Expand Down
4 changes: 2 additions & 2 deletions electron/renderer/src/components/TeamIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { colorFromId } from '../lib/accentColor';

import './TeamIcon.css';

const TeamIcon = ({ account, accentID }) =>
const TeamIcon = ({account, accentID}) =>
<div className="TeamIcon" title={account.name} data-uie-name="item-team" data-uie-value={account.name}>
{account.visible &&
<svg style={{fill: colorFromId(accentID)}} width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg">
Expand All @@ -34,7 +34,7 @@ const TeamIcon = ({ account, accentID }) =>
<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg">
<path d="M17.26 5.303c.96-.543 2.514-.545 3.48 0l9.52 5.375c.96.542 1.74 1.878 1.74 2.977v10.69c0 1.102-.774 2.432-1.74 2.977l-9.52 5.375c-.96.543-2.514.545-3.48 0l-9.52-5.375C6.78 26.78 6 25.444 6 24.345v-10.69c0-1.102.774-2.432 1.74-2.977l9.52-5.375z" fillRule="evenodd"></path>
</svg>
<span>{ [...account.name][0] }</span>
<span>{[...account.name][0]}</span>
</div>;

TeamIcon.propTypes = {
Expand Down
6 changes: 3 additions & 3 deletions electron/renderer/src/components/Webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class Webview extends Component {
}

_focusWebview() {
const is_visible = this.props.visible === true;
if (is_visible) {
const isVisible = this.props.visible === true;
if (isVisible) {
this.webview.focus();
}
}

render() {
const {visible, partition, src, onPageTitleUpdated, onIpcMessage, ...validProps} = this.props; // eslint-disable-line no-unused-vars
return <webview {...validProps} ref={(webview) => this.webview = webview } />;
return <webview {...validProps} ref={webview => this.webview = webview} />;
}
}

Expand Down
6 changes: 3 additions & 3 deletions electron/renderer/src/components/Webviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

import React, { Component } from 'react';
import React, {Component} from 'react';

import Webview from './Webview';

Expand Down Expand Up @@ -82,7 +82,7 @@ class Webviews extends Component {
window.sendBadgeCount(accumulatedCount);
}

_onIpcMessage(account, { channel, args }) {
_onIpcMessage(account, {channel, args}) {
switch (channel) {
case 'notification-click': {
this.props.switchAccount(account.id);
Expand Down Expand Up @@ -111,7 +111,7 @@ class Webviews extends Component {
}
}

this.setState({ canDelete: { ...this.state.canDelete, [account.id]: this._canDeleteWebview(account) } });
this.setState({canDelete: {...this.state.canDelete, [account.id]: this._canDeleteWebview(account)}});
}

_onWebviewClose(account) {
Expand Down
4 changes: 2 additions & 2 deletions electron/renderer/src/components/context/AddAccountMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ContextMenu from './ContextMenu';
import ContextMenuItem from './ContextMenuItem';
import { addAccountWithSession } from '../../actions/';

function AddAccountMenu({ ...connected }) {
function AddAccountMenu({...connected}) {
return (
<ContextMenu>
<ContextMenuItem onClick={() => window.open('https://wire.com/create-team/?pk_campaign=client&pk_kwd=desktop')}>
Expand All @@ -35,4 +35,4 @@ function AddAccountMenu({ ...connected }) {
);
}

export default connect(null, { addAccountWithSession })(AddAccountMenu);
export default connect(null, {addAccountWithSession})(AddAccountMenu);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import React from 'react';

const AddAccountMenuTrigger = ({ onClick, forceVisible }) => (
const AddAccountMenuTrigger = ({onClick, forceVisible}) => (
<div
className={`Sidebar-cell${forceVisible ? '' : ' ContextMenuTrigger'}`}
onClick={onClick}
Expand Down
23 changes: 11 additions & 12 deletions electron/renderer/src/components/context/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ class ContextMenu extends Component {
}

_handleRef(menu) {
if (!menu) {
return;
}
this.menu = menu;
const { x, y } = this.props.position;
if (menu) {
this.menu = menu;
const { x, y } = this.props.position;

const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;

const menuWidth = menu.offsetWidth;
const menuHeight = menu.offsetHeight;
const menuWidth = menu.offsetWidth;
const menuHeight = menu.offsetHeight;

menu.style.left = `${windowWidth - x < menuWidth ? x - menuWidth : x}px`;
menu.style.top = `${windowHeight - y < menuHeight ? y - menuHeight : y}px`;
menu.style.left = `${windowWidth - x < menuWidth ? x - menuWidth : x}px`;
menu.style.top = `${windowHeight - y < menuHeight ? y - menuHeight : y}px`;
}
}

render() {
Expand All @@ -106,5 +105,5 @@ export default connect(
state => ({
position: state.contextMenuState.position,
}),
{ setAccountContextHidden }
{setAccountContextHidden}
)(ContextMenu);
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function EditAccountMenu({
}

export default connect(
({ contextMenuState }) => ({
({contextMenuState}) => ({
accountId: contextMenuState.accountId,
isAtLeastAdmin: contextMenuState.isAtLeastAdmin,
lifecycle: contextMenuState.lifecycle,
Expand Down
2 changes: 1 addition & 1 deletion electron/renderer/src/containers/WebviewsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../actions';
import Webviews from '../components/Webviews';

const WebviewsContainer = connect(state => ({ accounts: state.accounts }), {
const WebviewsContainer = connect(state => ({accounts: state.accounts}), {
abortAccountCreation,
switchAccount,
updateAccountBadgeCount,
Expand Down
4 changes: 2 additions & 2 deletions electron/renderer/src/lib/accentColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const ACCENT_COLORS = [
VIOLET,
];

export function colorFromId(id) {
export const colorFromId = id => {
const accentColor = ACCENT_COLORS.find((color) => color.id === id);
return accentColor && accentColor.color;
}
};
11 changes: 5 additions & 6 deletions electron/renderer/src/lib/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
*
*/

const STATE_NAME = 'state';

export const loadState = () => {
try {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState);
const serializedState = localStorage.getItem(STATE_NAME);
return !!serializedState ? JSON.parse(serializedState) : undefined;
} catch (error) {
console.error('ERROR: Failed to load state ', error.message);
return undefined;
Expand All @@ -33,7 +32,7 @@ export const loadState = () => {
export const saveState = (state) => {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem('state', serializedState);
localStorage.setItem(STATE_NAME, serializedState);
} catch (error) {
console.error('ERROR: Failed to save state ', error.message);
}
Expand Down
4 changes: 1 addition & 3 deletions electron/renderer/src/lib/locale.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
window.locStrings = window.locStrings || {};
window.locStringsDefault = window.locStringsDefault || {};

export function getText(id) {
return locStrings[id] || locStringsDefault[id] || id;
}
export const getText = id => locStrings[id] || locStringsDefault[id] || id;
8 changes: 4 additions & 4 deletions electron/renderer/src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

export const noop = () => {};

export function preventFocus(func = noop) {
return function(event) {
export const preventFocus = (fn = noop) => {
return event => {
event.stopPropagation();
event.preventDefault();
func(event);
fn(event);
};
}
};
Loading

0 comments on commit 91c9f9f

Please sign in to comment.