Created
November 26, 2019 13:48
-
-
Save yayobyte/0bd9e2a01fa094024e42b52070034d8f 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
//@flow | |
import React from "react"; | |
import { connect } from "react-redux"; | |
import { push } from "connected-react-router"; | |
import { | |
selectedSelector, | |
createLocationSelector | |
} from "src/crud"; | |
import { selectCustomer } from "src/customers/actions"; | |
import { selectAccount } from "src/accounts/actions"; | |
import customerScopedActions from "src/customers/accounts/actions"; | |
import Transactions from "src/transactions"; | |
import CustomerTransactions from "./CustomerTransactions"; | |
const { | |
fetchOne: fetchAccount, | |
} = customerScopedActions; | |
const customerSelector = selectedSelector(); | |
const accountSelector = selectedSelector(); | |
const getLocationParams = createLocationSelector(); | |
const renderTransactions = (props) => <Transactions {...props} />; | |
export default connect( | |
({ | |
entities: { | |
customers: { byId: byIdA, selected: selectedCustomer }, | |
accounts: { byId: byIdB, fetching: fetchingAccount, selected: selectedAccount } | |
}, | |
router: { location: { pathname } } | |
}) => { | |
const { uid } = getLocationParams({ | |
pathname, | |
resource: "/customer_transactions/:uid?" | |
}); | |
const { accId } = getLocationParams({ | |
pathname, | |
resource: "/customer_transactions/:uid/accounts/:accId?" | |
}); | |
const customerId = selectedCustomer || uid; | |
const accountId = selectedAccount || accId; | |
console.log("AccountId: ", accountId); | |
const customer = customerSelector({ byId: byIdA, selectedId: customerId }); | |
const account = accountSelector({ byId: byIdB, selectedId: accountId }); | |
const transactionFilterParams = { customerId, accountId }; | |
return { | |
uid, | |
customer, | |
customerId, | |
account, | |
accountId, | |
renderTransactions, | |
transactionFilterParams, | |
fetchingAccount | |
}; | |
}, | |
(dispatch) => ({ | |
accountLoader: ({ accountId, customerId }) => { | |
if (accountId && customerId) { | |
dispatch(fetchAccount(accountId, { customer: { id: customerId } })); | |
} | |
}, | |
refresh: ({ id }) => { | |
if (id) { | |
// dispatch(invalidateCache(["transactions"])); | |
dispatch(selectCustomer(id)); | |
dispatch(push(`/customer_transactions/${id}`)); | |
} | |
}, | |
refreshWithAccount: ({ id, customerId }) => { | |
if (id && customerId) { | |
// dispatch(invalidateCache(["transactions"])); | |
dispatch(selectCustomer(customerId)); | |
dispatch(selectAccount(id)); | |
dispatch(push(`/customer_transactions/${customerId}/accounts/${id}`)); | |
} | |
} | |
}) | |
)(CustomerTransactions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment