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,
}
componentWillMount() {
if (this.props.isLocked) {
stopSyncAccounts()
}
}
componentWillReceiveProps(nextProps) {
if (this.props.isLocked && !nextProps.isLocked) {
startSyncAccounts(nextProps.accounts)

12
src/components/SelectAccount.js

@ -12,6 +12,10 @@ import Text from 'components/base/Text'
import type { Account } from 'types/common'
function renderItem(accounts) {
return item => <span>{(accounts.find(a => a.id === item) || {}).name}</span>
}
const mapStateToProps: MapStateToProps<*, *, *> = state => ({
accounts: values(getAccounts(state)),
})
@ -24,12 +28,13 @@ type Props = {
const SelectAccount = ({ accounts, value, onChange }: Props) => (
<Select
itemToString={item => item}
value={value}
placeholder="Choose an account"
items={accounts}
keyProp="id"
items={accounts.map(a => a.id)}
onChange={onChange}
itemToString={item => (item ? item.name : '')}
renderItem={renderItem(accounts)}
renderSelected={renderItem(accounts)}
renderHighlight={(text, key) => (
<Text key={key} fontWeight="bold">
{text}
@ -37,5 +42,4 @@ const SelectAccount = ({ accounts, value, onChange }: Props) => (
)}
/>
)
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'
type Props = {
items: Array<Object>,
items: Array<any>,
value?: Object | null,
itemToString?: Function,
onChange?: Function,
@ -24,7 +24,7 @@ type Props = {
searchable?: boolean,
placeholder?: string,
renderHighlight?: string => Element<*>,
renderSelected?: Object => Element<*>,
renderSelected?: any => Element<*>,
renderItem?: (*) => Element<*>,
keyProp?: string,
}
@ -98,11 +98,15 @@ class Select extends PureComponent<Props> {
renderItems = (items: Array<Object>, downshiftProps: Object) => {
const { renderItem, keyProp } = this.props
const { getItemProps, highlightedIndex } = downshiftProps
return (
<Dropdown>
{items.length ? (
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}>
{renderItem ? renderItem(item) : <span>{item.name_highlight || item.name}</span>}
</Item>

2
src/constants.js

@ -1,2 +1,2 @@
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 })
try {
const result = await accounts.reduce(
(promise, account) =>
promise.then(async results => {
const result = await syncAccount(account)
return [...results, result]
}),
Promise.resolve([]),
)
const result = await Promise.all(accounts.map(syncAccount))
send('accounts.sync.success', result)
} catch (err) {

17
src/renderer/events.js

@ -3,6 +3,7 @@
import { ipcRenderer } from 'electron'
import objectPath from 'object-path'
import get from 'lodash/get'
import uniqBy from 'lodash/uniqBy'
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 { syncAccount } from 'actions/accounts'
import { setUpdateStatus } from 'reducers/update'
import { getAccounts } from 'reducers/accounts'
import { getAccountData, getAccounts } from 'reducers/accounts'
type MsgPayload = {
type: string,
@ -66,9 +67,17 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
sync: {
success: accounts => {
if (syncAccounts) {
accounts.forEach(
account => account.transactions.length > 0 && store.dispatch(syncAccount(account)),
)
const currentState = store.getState()
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(() => {
const newAccounts = getAccounts(store.getState())
startSyncAccounts(newAccounts)

Loading…
Cancel
Save