Skip to content

Instantly share code, notes, and snippets.

@kandros
Forked from ryanflorence/foo.js
Created January 29, 2017 21:41
Show Gist options
  • Save kandros/03ab0ce08ef27d23132b186c3084177e to your computer and use it in GitHub Desktop.
Save kandros/03ab0ce08ef27d23132b186c3084177e to your computer and use it in GitHub Desktop.
const addAccountToTransactions = (txs, accounts) => (
txs.map(tx => (
// add the relationship
{
...tx,
account: accounts[tx.accountKey]
}
))
)
const TransactionsWithAccounts = () => (
// these fetch in parallel
<Ref path="/accounts">
{(accounts) => (
<Ref path="/transactions" list={true}>
{(txs) => (
// txs will be an array
accounts.error || txs.error ? (
<ErrorMessages errors={[ accounts.error, txs.error ]}/>
) : accounts.loaded && txs.loaded ? (
children(addAccountToTransactions(transactions.value, accounts.value))
) : (
<Loading/>
)
)}
</Ref>
)}
</Ref>
)
// now it's super easy to just grab that data from anywhere, even w/ the foreign relationship!
const TransactionsDashboard = () => (
<Block>
<Header>Transactions</Header>
<TransactionsWithAccounts>
{(txs) => (
<Table txs={formatTransactions(txs)}/>
)}
</TransactionsWithAccounts>
</Block>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment