You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

// @flow
import { createSelector } from 'reselect'
import { handleActions } from 'redux-actions'
import type { State } from 'reducers'
import { accountsSelector } from './accounts'
export type AsyncState = {
pending: boolean,
error: ?Error,
}
export type BridgeSyncState = {
syncs: { [accountId: string]: AsyncState },
}
const initialState: BridgeSyncState = {
syncs: {},
}
const handlers: Object = {
SET_ACCOUNT_SYNC_STATE: (
state: BridgeSyncState,
action: {
accountId: string,
state: AsyncState,
},
) => ({
syncs: {
...state.syncs,
[action.accountId]: action.state,
},
}),
}
// Selectors
export const bridgeSyncSelector = (state: State) => state.bridgeSync
const nothingState = { pending: false, error: null }
export const syncStateLocalSelector = (
bridgeSync: BridgeSyncState,
{ accountId }: { accountId: string },
) => bridgeSync.syncs[accountId] || nothingState
export const globalSyncStateSelector = createSelector(
accountsSelector,
bridgeSyncSelector,
(accounts, bridgeSync) => {
const globalSyncState: AsyncState = {
pending: false,
error: null,
}
for (const account of accounts) {
const syncState = syncStateLocalSelector(bridgeSync, { accountId: account.id })
if (syncState.error) globalSyncState.error = syncState.error
if (syncState.pending) globalSyncState.pending = true
}
return globalSyncState
},
)
export default handleActions(handlers, initialState)