Skip to content

Commit

Permalink
refactor: default props
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Nov 8, 2023
1 parent 8f882d9 commit 861fd1f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 28 deletions.
15 changes: 8 additions & 7 deletions lib/Donors/DonorsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useStripes } from '@folio/stripes/core';
import { defaultVisibleColumns } from './constants';
import DonorsList from './DonorsList';
import DonorsLookup from './DonorsLookup';
import { getResultsFormatter } from './utils';
import { getDonorsListFormatter } from './utils';

function DonorsContainer({
columnMapping,
Expand All @@ -26,11 +26,13 @@ function DonorsContainer({
const intl = useIntl();
const canViewOrganizations = stripes.hasPerm('ui-organizations.view');

const donorsMap = donors.reduce((acc, contact) => {
acc[contact.id] = contact;
const donorsMap = useMemo(() => {
return donors.reduce((acc, contact) => {
acc[contact.id] = contact;

return acc;
}, {});
return acc;
}, {});
}, [donors]);

const listOfDonors = useMemo(() => (fields.value || [])
.map((contactId, _index) => {
Expand All @@ -45,7 +47,7 @@ function DonorsContainer({
const contentData = useMemo(() => sortBy(listOfDonors, [({ lastName }) => lastName?.toLowerCase()]), [listOfDonors]);

const resultsFormatter = useMemo(() => {
return formatter || getResultsFormatter({ intl, fields, canViewOrganizations });
return formatter || getDonorsListFormatter({ intl, fields, canViewOrganizations });
}, [canViewOrganizations, fields, formatter, intl]);

const onAddDonors = (values = []) => {
Expand Down Expand Up @@ -75,7 +77,6 @@ function DonorsContainer({
onAddDonors={onAddDonors}
name={id}
searchLabel={searchLabel}
showTriggerButton={showTriggerButton}
visibleColumns={visibleColumns}
/>
)
Expand Down
8 changes: 6 additions & 2 deletions lib/Donors/DonorsContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ describe('DonorsContainer', () => {
const addDonorsButton = screen.getByText('Add donor');

expect(addDonorsButton).toBeDefined();

await user.click(addDonorsButton);

expect(setDonorIds).toHaveBeenCalled();
});

it('should not render `DonorsLookup` when `showTriggerButton` is false', () => {
renderComponent({ showTriggerButton: false });

expect(screen.queryByText('Add donor')).toBeNull();
});
});
2 changes: 2 additions & 0 deletions lib/Donors/DonorsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MultiColumnList } from '@folio/stripes/components';
import {
alignRowProps,
defaultColumnMapping,
defaultFormatter,
defaultVisibleColumns,
} from './constants';

Expand Down Expand Up @@ -40,6 +41,7 @@ DonorsList.propTypes = {

DonorsList.defaultProps = {
columnMapping: defaultColumnMapping,
formatter: defaultFormatter,
visibleColumns: defaultVisibleColumns,
};

Expand Down
7 changes: 0 additions & 7 deletions lib/Donors/DonorsLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ const DonorsLookup = ({
name,
onAddDonors,
searchLabel,
showTriggerButton,
visibleColumns,
}) => {
const stripes = useStripes();

if (!showTriggerButton) {
return null;
}

return (
<Pluggable
id={`${name}-plugin`}
Expand Down Expand Up @@ -57,13 +52,11 @@ const DonorsLookup = ({
DonorsLookup.propTypes = {
onAddDonors: PropTypes.func.isRequired,
name: PropTypes.string,
showTriggerButton: PropTypes.bool,
searchLabel: PropTypes.node,
visibleColumns: PropTypes.arrayOf(PropTypes.string),
};

DonorsLookup.defaultProps = {
showTriggerButton: true,
searchLabel: <FormattedMessage id="stripes-acq-components.donors.button.addDonor" />,
visibleColumns: pluginVisibleColumns,
};
Expand Down
8 changes: 0 additions & 8 deletions lib/Donors/DonorsLookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ describe('DonorsLookup', () => {
const addDonorsButton = screen.getByText('Add donor');

expect(addDonorsButton).toBeDefined();

await user.click(addDonorsButton);

expect(mockOnAddDonors).toHaveBeenCalledWith([mockVendorData]);
});

it('should not render component', () => {
renderComponent({ showTriggerButton: false });

expect(screen.queryByText('Add donor')).toBeNull();
});
});
6 changes: 6 additions & 0 deletions lib/Donors/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TextLink } from '@folio/stripes/components';
import { FormattedMessage } from 'react-intl';

export const defaultColumnMapping = {
Expand All @@ -11,6 +12,11 @@ export const defaultVisibleColumns = [
'code',
];

export const defaultFormatter = {
name: donor => <TextLink to={donor.id}>{donor.name}</TextLink>,
code: donor => donor.code,
};

export const alignRowProps = { alignLastColToEnd: true };

export const modalLabel = <FormattedMessage id="stripes-acq-components.donors.modal.title" />;
Expand Down
2 changes: 1 addition & 1 deletion lib/Donors/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getDonorUrl = (orgId) => {
return undefined;
};

export const getResultsFormatter = ({
export const getDonorsListFormatter = ({
canViewOrganizations,
fields,
intl,
Expand Down
6 changes: 3 additions & 3 deletions lib/Donors/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getResultsFormatter } from './utils';
import { getDonorsListFormatter } from './utils';

const defaultProps = {
canViewOrganizations: true,
Expand All @@ -10,9 +10,9 @@ const defaultProps = {
},
};

describe('getResultsFormatter', () => {
describe('getDonorsListFormatter', () => {
it('should return object with name, code and unassignDonor functions', () => {
const result = getResultsFormatter(defaultProps);
const result = getDonorsListFormatter(defaultProps);

expect(result).toEqual(expect.objectContaining({
name: expect.any(Function),
Expand Down

0 comments on commit 861fd1f

Please sign in to comment.