Skip to content

Instantly share code, notes, and snippets.

@yayobyte
Created November 27, 2019 11:37
Show Gist options
  • Save yayobyte/d3b9dd4c0931bdbe08a10658ed45de69 to your computer and use it in GitHub Desktop.
Save yayobyte/d3b9dd4c0931bdbe08a10658ed45de69 to your computer and use it in GitHub Desktop.
//@flow
import { connect } from "react-redux";
import {
entitiesOptionsSelector
} from "src/crud";
import { entitySelectTemplate } from "src/containerTemplates";
import basicActions, {
fetchAccountSuggestions
} from "./actions";
import AccountsAutosuggestC from "./AccountsAutosuggest";
import AccountsSelectC from "./AccountsSelect";
const { filter } = basicActions;
export const AccountsAutosuggest = connect(
({
entities: {
customers: { byId: customersById }
}
}) => ({
customersById
}),
{
fetchAccountSuggestions,
}
)(AccountsAutosuggestC);
const accountOptionsSelector = entitiesOptionsSelector({
getLabel: ({ number, currency }) => `${number}.${currency}`
});
export const AccountsSelect = entitySelectTemplate("accounts", filter, {
mapState: ({ entities: { accounts: { byId } } }, { error, fetching }, { customer }) => {
const { accounts: customerAccounts } = customer || { accounts: { ids: [] } };
const { ids, valid } = customerAccounts || { ids: [], valid: false };
const items = accountOptionsSelector({ ids, byId, error });
return {
accounts: byId,
error,
items,
fetching,
valid
};
},
dispatches: (dispatch) => ({
fetch: ({ customer }) => customer && customer.id
&& dispatch(filter({ customerId: customer.id }))
})
})(AccountsSelectC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment