Created
January 25, 2017 00:57
-
-
Save ryanflorence/492da1cc39ef69e2c77a3683ac6a1de4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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