Browse Source

Merge pull request #59 from loeck/master

Fix SelectAccounts components, clean stuffs
master
Loëck Vézien 7 years ago
committed by GitHub
parent
commit
ade69a2ea7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/components/IsUnlocked/index.js
  2. 12
      src/components/SelectAccount.js
  3. 10
      src/components/base/Select/index.js
  4. 2
      src/constants.js
  5. 9
      src/internals/accounts/sync.js
  6. 17
      src/renderer/events.js

6
src/components/IsUnlocked/index.js

@ -57,6 +57,12 @@ class IsUnlocked extends PureComponent<Props, State> {
...defaultState, ...defaultState,
} }
componentWillMount() {
if (this.props.isLocked) {
stopSyncAccounts()
}
}
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (this.props.isLocked && !nextProps.isLocked) { if (this.props.isLocked && !nextProps.isLocked) {
startSyncAccounts(nextProps.accounts) startSyncAccounts(nextProps.accounts)

12
src/components/SelectAccount.js

@ -12,6 +12,10 @@ import Text from 'components/base/Text'
import type { Account } from 'types/common' import type { Account } from 'types/common'
function renderItem(accounts) {
return item => <span>{(accounts.find(a => a.id === item) || {}).name}</span>
}
const mapStateToProps: MapStateToProps<*, *, *> = state => ({ const mapStateToProps: MapStateToProps<*, *, *> = state => ({
accounts: values(getAccounts(state)), accounts: values(getAccounts(state)),
}) })
@ -24,12 +28,13 @@ type Props = {
const SelectAccount = ({ accounts, value, onChange }: Props) => ( const SelectAccount = ({ accounts, value, onChange }: Props) => (
<Select <Select
itemToString={item => item}
value={value} value={value}
placeholder="Choose an account" placeholder="Choose an account"
items={accounts} items={accounts.map(a => a.id)}
keyProp="id"
onChange={onChange} onChange={onChange}
itemToString={item => (item ? item.name : '')} renderItem={renderItem(accounts)}
renderSelected={renderItem(accounts)}
renderHighlight={(text, key) => ( renderHighlight={(text, key) => (
<Text key={key} fontWeight="bold"> <Text key={key} fontWeight="bold">
{text} {text}
@ -37,5 +42,4 @@ const SelectAccount = ({ accounts, value, onChange }: Props) => (
)} )}
/> />
) )
export default connect(mapStateToProps)(SelectAccount) export default connect(mapStateToProps)(SelectAccount)

10
src/components/base/Select/index.js

@ -15,7 +15,7 @@ import Search from 'components/base/Search'
import Triangles from './Triangles' import Triangles from './Triangles'
type Props = { type Props = {
items: Array<Object>, items: Array<any>,
value?: Object | null, value?: Object | null,
itemToString?: Function, itemToString?: Function,
onChange?: Function, onChange?: Function,
@ -24,7 +24,7 @@ type Props = {
searchable?: boolean, searchable?: boolean,
placeholder?: string, placeholder?: string,
renderHighlight?: string => Element<*>, renderHighlight?: string => Element<*>,
renderSelected?: Object => Element<*>, renderSelected?: any => Element<*>,
renderItem?: (*) => Element<*>, renderItem?: (*) => Element<*>,
keyProp?: string, keyProp?: string,
} }
@ -98,11 +98,15 @@ class Select extends PureComponent<Props> {
renderItems = (items: Array<Object>, downshiftProps: Object) => { renderItems = (items: Array<Object>, downshiftProps: Object) => {
const { renderItem, keyProp } = this.props const { renderItem, keyProp } = this.props
const { getItemProps, highlightedIndex } = downshiftProps const { getItemProps, highlightedIndex } = downshiftProps
return ( return (
<Dropdown> <Dropdown>
{items.length ? ( {items.length ? (
items.map((item, i) => ( items.map((item, i) => (
<ItemWrapper key={keyProp ? item[keyProp] : item.key} {...getItemProps({ item })}> <ItemWrapper
key={keyProp ? item[keyProp] : item.key || item}
{...getItemProps({ item })}
>
<Item highlighted={i === highlightedIndex}> <Item highlighted={i === highlightedIndex}>
{renderItem ? renderItem(item) : <span>{item.name_highlight || item.name}</span>} {renderItem ? renderItem(item) : <span>{item.name_highlight || item.name}</span>}
</Item> </Item>

2
src/constants.js

@ -1,2 +1,2 @@
export const CHECK_UPDATE_TIMEOUT = 5e3 export const CHECK_UPDATE_TIMEOUT = 5e3
export const SYNC_ACCOUNT_TIMEOUT = 2e3 export const SYNC_ACCOUNT_TIMEOUT = 3e3

9
src/internals/accounts/sync.js

@ -17,14 +17,7 @@ export default (send: Function) => ({
send('accounts.sync.progress', null, { kill: false }) send('accounts.sync.progress', null, { kill: false })
try { try {
const result = await accounts.reduce( const result = await Promise.all(accounts.map(syncAccount))
(promise, account) =>
promise.then(async results => {
const result = await syncAccount(account)
return [...results, result]
}),
Promise.resolve([]),
)
send('accounts.sync.success', result) send('accounts.sync.success', result)
} catch (err) { } catch (err) {

17
src/renderer/events.js

@ -3,6 +3,7 @@
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import objectPath from 'object-path' import objectPath from 'object-path'
import get from 'lodash/get' import get from 'lodash/get'
import uniqBy from 'lodash/uniqBy'
import type { Accounts } from 'types/common' import type { Accounts } from 'types/common'
@ -11,7 +12,7 @@ import { CHECK_UPDATE_TIMEOUT, SYNC_ACCOUNT_TIMEOUT } from 'constants'
import { updateDevices, addDevice, removeDevice } from 'actions/devices' import { updateDevices, addDevice, removeDevice } from 'actions/devices'
import { syncAccount } from 'actions/accounts' import { syncAccount } from 'actions/accounts'
import { setUpdateStatus } from 'reducers/update' import { setUpdateStatus } from 'reducers/update'
import { getAccounts } from 'reducers/accounts' import { getAccountData, getAccounts } from 'reducers/accounts'
type MsgPayload = { type MsgPayload = {
type: string, type: string,
@ -66,9 +67,17 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
sync: { sync: {
success: accounts => { success: accounts => {
if (syncAccounts) { if (syncAccounts) {
accounts.forEach( const currentState = store.getState()
account => account.transactions.length > 0 && store.dispatch(syncAccount(account)), accounts.forEach(account => {
) const currentAccountData = getAccountData(currentState, account.id) || {}
const transactions = uniqBy(
[...currentAccountData.transactions, ...account.transactions],
tx => tx.hash,
)
if (currentAccountData.transactions.length !== transactions.length) {
store.dispatch(syncAccount(account))
}
})
syncTimeout = setTimeout(() => { syncTimeout = setTimeout(() => {
const newAccounts = getAccounts(store.getState()) const newAccounts = getAccounts(store.getState())
startSyncAccounts(newAccounts) startSyncAccounts(newAccounts)

Loading…
Cancel
Save