Skip to content

Instantly share code, notes, and snippets.

@yayobyte
Created November 26, 2019 12:08
Show Gist options
  • Save yayobyte/7914ff97773b953819f6f161134a0baa to your computer and use it in GitHub Desktop.
Save yayobyte/7914ff97773b953819f6f161134a0baa to your computer and use it in GitHub Desktop.
//@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;
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