Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve performance issue by using selector factories #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/companies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ const getCompaniesDict = createSelector(

const pickOfficePhone = phones => phones.find(phone => phone.charAt(0) === 1) || phones[0] || null;

export const getCompanyById = createSelector(
export const makeGetCompanyById = () => createSelector(
[getCompaniesDict, getCompanyId],
(companies, id) => ({
...companies[id],
phone: pickOfficePhone(companies[id].phones)
})
);

export const getCompanyEmployees = createSelector(
export const makeGetCompanyEmployees = () => createSelector(
[getContacts, getCompanyId],
(contacts, companyId) => contacts.filter(c => c.employer === companyId)
);

export const getCompanyCustomers = createSelector(
export const makeGetCompanyCustomers = () => createSelector(
[getContacts, getCompanyId],
(contacts, companyId) => contacts.filter(c => c.sellers.includes(companyId))
);
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { connect } from 'react-redux';
import { createSelector } from "reselect";

import { getCompanyEmployees, getCompanyCustomers } from "../../../companies";
import { makeGetCompanyEmployees, makeGetCompanyCustomers } from "../../../companies";
import CompanyContacts from './CompanyContacts.component';

const selectorCounts = createSelector(
[getCompanyCustomers, getCompanyEmployees],
export default connect(() => createSelector(
[makeGetCompanyCustomers(), makeGetCompanyEmployees()],
(customers, employees) => ({
nCustomers: customers.length,
nEmployees: employees.length
})
)

export default connect(selectorCounts)(CompanyContacts);
))(CompanyContacts);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { getCompanyById } from "../../../companies";
import { makeGetCompanyById } from "../../../companies";
import CompanyDetails from './CompanyDetails.component';

export default connect(createStructuredSelector({
company: getCompanyById
export default connect(() => createStructuredSelector({
company: makeGetCompanyById()
}))(CompanyDetails);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { getCompanyById } from "../../../companies";
import { makeGetCompanyById } from "../../../companies";
import CompanyMessengers from './CompanyMessengers.component';

export default connect(createStructuredSelector({
company: getCompanyById
export default connect(() => createStructuredSelector({
company: makeGetCompanyById()
}))(CompanyMessengers);