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

UISACQCOMP-166: add unassignDonor to default formatters #728

Merged
merged 5 commits into from
Nov 10, 2023
Merged
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
24 changes: 7 additions & 17 deletions lib/Donors/DonorsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { useIntl } from 'react-intl';

import { useStripes } from '@folio/stripes/core';

import { defaultVisibleColumns } from './constants';
import { defaultContainerVisibleColumns } from './constants';
import { DonorsList } from './DonorsList';
import { DonorsLookup } from './DonorsLookup';
import {
getDonorsListFormatter,
getUnAssignDonorFormatter,
} from './utils';
import { getDonorsFormatter } from './utils';

export function DonorsContainer({
columnMapping,
Expand All @@ -24,6 +21,7 @@ export function DonorsContainer({
searchLabel,
showTriggerButton,
visibleColumns,
...rest
}) {
const stripes = useStripes();
const intl = useIntl();
Expand All @@ -50,17 +48,8 @@ export function DonorsContainer({
const contentData = useMemo(() => sortBy(listOfDonors, [({ lastName }) => lastName?.toLowerCase()]), [listOfDonors]);

const resultsFormatter = useMemo(() => {
const defaultFormatter = formatter || getDonorsListFormatter({ intl, fields, canViewOrganizations });

if (visibleColumns.includes('unassignDonor')) {
return {
...getUnAssignDonorFormatter({ intl, fields }),
...defaultFormatter,
};
}

return defaultFormatter;
}, [canViewOrganizations, fields, formatter, intl, visibleColumns]);
return formatter || getDonorsFormatter({ intl, fields, canViewOrganizations });
}, [canViewOrganizations, fields, formatter, intl]);

const onAddDonors = (values = []) => {
const addedDonorIds = new Set(fields.value);
Expand All @@ -81,6 +70,7 @@ export function DonorsContainer({
formatter={resultsFormatter}
columnMapping={columnMapping}
columnWidths={columnWidths}
{...rest}
/>
<br />
{
Expand Down Expand Up @@ -112,5 +102,5 @@ DonorsContainer.propTypes = {

DonorsContainer.defaultProps = {
showTriggerButton: true,
visibleColumns: defaultVisibleColumns,
visibleColumns: defaultContainerVisibleColumns,
};
4 changes: 4 additions & 0 deletions lib/Donors/DonorsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useMemo } from 'react';
import { MultiColumnList } from '@folio/stripes/components';
import { useStripes } from '@folio/stripes/core';

import { acqRowFormatter } from '../utils';
import {
alignRowProps,
defaultColumnMapping,
Expand All @@ -18,6 +19,7 @@ export const DonorsList = ({
formatter: formatterProp,
id,
visibleColumns,
...rest
}) => {
const stripes = useStripes();
const canViewOrganizations = stripes.hasPerm('ui-organizations.view');
Expand All @@ -31,9 +33,11 @@ export const DonorsList = ({
columnMapping={columnMapping}
contentData={contentData}
formatter={formatter}
rowFormatter={acqRowFormatter}
rowProps={alignRowProps}
visibleColumns={visibleColumns}
columnWidths={columnWidths}
{...rest}
/>
);
};
Expand Down
26 changes: 26 additions & 0 deletions lib/Donors/DonorsListContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import PropTypes from 'prop-types';

import { Loading } from '@folio/stripes/components';

import { DonorsList } from './DonorsList';
import { useFetchDonors } from './hooks';

export const DonorsListContainer = ({ donorOrganizationIds, ...rest }) => {
const { donors, isLoading } = useFetchDonors(donorOrganizationIds);

if (isLoading) {
return <Loading />;
}

return (
<DonorsList
rowProps={{ alignLastColToEnd: false }}
contentData={donors}
{...rest}
/>
);
};

DonorsListContainer.propTypes = {
donorOrganizationIds: PropTypes.arrayOf(PropTypes.string).isRequired,
};
58 changes: 58 additions & 0 deletions lib/Donors/DonorsListContainer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { MemoryRouter } from 'react-router-dom';
import { render, screen } from '@testing-library/react';

import { DonorsListContainer } from './DonorsListContainer';
import { useFetchDonors } from './hooks';

jest.mock('@folio/stripes/components', () => ({
...jest.requireActual('@folio/stripes/components'),
Loading: jest.fn(() => 'Loading'),
}));

jest.mock('./DonorsList', () => ({
DonorsList: jest.fn(() => 'DonorsList'),
}));

jest.mock('./hooks', () => ({
useFetchDonors: jest.fn().mockReturnValue({
donors: [],
isLoading: false,
}),
}));

const defaultProps = {
donorOrganizationIds: [],
};

const wrapper = ({ children }) => (
<MemoryRouter>
{children}
</MemoryRouter>
);

const renderComponent = (props = {}) => (render(
<DonorsListContainer
{...defaultProps}
{...props}
/>,
{ wrapper },
));

describe('DonorsListContainer', () => {
it('should render component', () => {
renderComponent();

expect(screen.getByText('DonorsList')).toBeDefined();
});

it('should render loading component', () => {
useFetchDonors.mockClear().mockReturnValue({
donors: [],
isLoading: true,
});

renderComponent();

expect(screen.getByText('Loading')).toBeDefined();
});
});
5 changes: 5 additions & 0 deletions lib/Donors/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const defaultVisibleColumns = [
'code',
];

export const defaultContainerVisibleColumns = [
...defaultVisibleColumns,
'unassignDonor',
];

export const alignRowProps = { alignLastColToEnd: true };

export const modalLabel = <FormattedMessage id="stripes-acq-components.donors.modal.title" />;
Expand Down
1 change: 1 addition & 0 deletions lib/Donors/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Donors } from './Donors';
export { DonorsList } from './DonorsList';
export { DonorsListContainer } from './DonorsListContainer';
export { useFetchDonors } from './hooks/useFetchDonors';
4 changes: 2 additions & 2 deletions lib/Donors/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export const getDonorsListFormatter = ({ canViewOrganizations }) => ({
code: donor => donor.code,
});

export const getUnAssignDonorFormatter = ({ fields, intl }) => ({
export const getDonorsFormatter = ({ canViewOrganizations, fields, intl }) => ({
...getDonorsListFormatter({ canViewOrganizations }),
unassignDonor: donor => (
<Button
align="end"
aria-label={intl.formatMessage({ id: 'stripes-acq-components.donors.button.unassign' })}
buttonStyle="fieldControl"
type="button"
Expand Down
6 changes: 3 additions & 3 deletions lib/Donors/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
getDonorsListFormatter,
getUnAssignDonorFormatter,
getDonorsFormatter,
} from './utils';

const defaultProps = {
Expand All @@ -24,9 +24,9 @@ describe('getDonorsListFormatter', () => {
});
});

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

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