-
Notifications
You must be signed in to change notification settings - Fork 3
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: view list of donors #724
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
44033b8
UISACQCOMP-166: view list of donors
alisher-epam ccae7e8
Merge branch 'master' into UISACQCOMP-166
alisher-epam cb5be47
tests: add test coverages
alisher-epam 407e3a1
tests: add test coverage and Changelog.md
alisher-epam 9afb133
refactor: change the order of import files
alisher-epam acb730f
add column width for better table view
alisher-epam 5474b1a
refactor: add config properties for plugin to implement donors
alisher-epam f12b1bc
fix: add missing IS_DONOR filter prop
alisher-epam e54369b
improve code quality regarding to the comments
alisher-epam a4b13ff
refactor: upgrade mutation to query for better caching
alisher-epam b946cca
fix: failing tests
alisher-epam 1b6cb98
update callback names for better readability
alisher-epam 98ef11b
improve and optimize the code
alisher-epam c96feae
update state initializer
alisher-epam 11bb6c9
tests: add test cases for the button
alisher-epam 32ec435
remove unused packages and imports
alisher-epam a989e49
tests: add test cases
alisher-epam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { map } from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { Pluggable } from '@folio/stripes/core'; | ||
|
||
import { | ||
initialFilters, | ||
modalLabel, | ||
pluginVisibleColumns, | ||
resultsPaneTitle, | ||
searchableIndexes, | ||
visibleFilters, | ||
} from './constants'; | ||
|
||
const AddDonorButton = ({ onAddDonors, fields, stripes, name }) => { | ||
const addDonors = (donors = []) => { | ||
const addedDonorIds = new Set(fields.value); | ||
const newDonorsIds = map(donors.filter(({ id }) => !addedDonorIds.has(id)), 'id'); | ||
|
||
if (newDonorsIds.length) { | ||
onAddDonors([...addedDonorIds, ...newDonorsIds]); | ||
newDonorsIds.forEach(contactId => fields.push(contactId)); | ||
} | ||
}; | ||
|
||
return ( | ||
<Pluggable | ||
id={`${name}-plugin`} | ||
aria-haspopup="true" | ||
type="find-organization" | ||
dataKey="organization" | ||
searchLabel={<FormattedMessage id="stripes-acq-components.donors.button.addDonor" />} | ||
searchButtonStyle="default" | ||
disableRecordCreation | ||
stripes={stripes} | ||
selectVendor={addDonors} | ||
modalLabel={modalLabel} | ||
resultsPaneTitle={resultsPaneTitle} | ||
visibleColumns={pluginVisibleColumns} | ||
initialFilters={initialFilters} | ||
searchableIndexes={searchableIndexes} | ||
visibleFilters={visibleFilters} | ||
isMultiSelect | ||
> | ||
<span data-test-add-donor> | ||
<FormattedMessage id="stripes-acq-components.donors.noFindOrganizationPlugin" /> | ||
</span> | ||
</Pluggable> | ||
); | ||
}; | ||
|
||
AddDonorButton.propTypes = { | ||
onAddDonors: PropTypes.func.isRequired, | ||
fields: PropTypes.object, | ||
stripes: PropTypes.object, | ||
name: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default AddDonorButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
|
||
import AddDonorButton from './AddDonorButton'; | ||
|
||
const mockVendorData = { id: '1', name: 'Amazon' }; | ||
|
||
jest.mock('@folio/stripes/core', () => ({ | ||
...jest.requireActual('@folio/stripes/core'), | ||
Pluggable: jest.fn(({ children, ...rest }) => { | ||
return ( | ||
<div> | ||
{children} | ||
<button | ||
type="button" | ||
id={rest?.name} | ||
onClick={() => rest?.selectVendor([mockVendorData])} | ||
> | ||
Add donor | ||
</button> | ||
</div> | ||
); | ||
}), | ||
})); | ||
|
||
const mockOnAddDonors = jest.fn(); | ||
|
||
const defaultProps = { | ||
onAddDonors: mockOnAddDonors, | ||
fields: { | ||
name: 'donors', | ||
}, | ||
name: 'donors', | ||
}; | ||
|
||
const renderComponent = (props = defaultProps) => (render( | ||
<AddDonorButton {...props} />, | ||
)); | ||
|
||
describe('AddDonorButton', () => { | ||
it('should render component', async () => { | ||
renderComponent({ | ||
fields: { | ||
name: 'donors', | ||
push: jest.fn(), | ||
}, | ||
name: 'donors', | ||
onAddDonors: mockOnAddDonors, | ||
}); | ||
|
||
const addDonorsButton = screen.getByText('Add donor'); | ||
|
||
expect(addDonorsButton).toBeDefined(); | ||
|
||
await user.click(addDonorsButton); | ||
|
||
expect(mockOnAddDonors).toHaveBeenCalledWith([mockVendorData.id]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { FieldArray } from 'react-final-form-arrays'; | ||
|
||
import { | ||
Col, | ||
Loading, | ||
Row, | ||
} from '@folio/stripes/components'; | ||
|
||
import DonorsList from './DonorsList'; | ||
import { useFetchDonors } from './hooks'; | ||
|
||
function DonorsContainer({ name, donorOrganizationIds }) { | ||
const [donorIds, setDonorIds] = useState(donorOrganizationIds); | ||
const { donors, isLoading } = useFetchDonors(donorIds); | ||
|
||
const donorsMap = donors.reduce((acc, contact) => { | ||
acc[contact.id] = contact; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<Row> | ||
<Col xs={12}> | ||
<FieldArray | ||
name={name} | ||
id={name} | ||
component={DonorsList} | ||
setDonorIds={setDonorIds} | ||
donorsMap={donorsMap} | ||
/> | ||
</Col> | ||
</Row> | ||
); | ||
} | ||
|
||
DonorsContainer.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
donorOrganizationIds: PropTypes.arrayOf(PropTypes.string), | ||
}; | ||
|
||
DonorsContainer.defaultProps = { | ||
donorOrganizationIds: [], | ||
}; | ||
|
||
export default DonorsContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import stripesFinalForm from '@folio/stripes/final-form'; | ||
|
||
import DonorsContainer from './DonorsContainer'; | ||
import { useFetchDonors } from './hooks'; | ||
|
||
jest.mock('@folio/stripes/components', () => ({ | ||
...jest.requireActual('@folio/stripes/components'), | ||
Loading: jest.fn(() => 'Loading'), | ||
})); | ||
|
||
jest.mock('./DonorsList', () => jest.fn(({ donorsMap }) => { | ||
if (!Object.values(donorsMap).length) { | ||
return 'stripes-components.tableEmpty'; | ||
} | ||
|
||
return Object.values(donorsMap).map(({ name }) => <div key={name}>{name}</div>); | ||
})); | ||
|
||
jest.mock('./hooks', () => ({ | ||
useFetchDonors: jest.fn().mockReturnValue({ | ||
donors: [], | ||
isLoading: false, | ||
}), | ||
})); | ||
|
||
const defaultProps = { | ||
name: 'donors', | ||
donorOrganizationIds: [], | ||
}; | ||
|
||
const renderForm = (props = {}) => ( | ||
<form> | ||
<DonorsContainer | ||
{...defaultProps} | ||
{...props} | ||
/> | ||
<button type="submit">Submit</button> | ||
</form> | ||
); | ||
|
||
const FormCmpt = stripesFinalForm({})(renderForm); | ||
|
||
const renderComponent = (props = {}) => (render( | ||
<MemoryRouter> | ||
<FormCmpt onSubmit={() => { }} {...props} /> | ||
</MemoryRouter>, | ||
)); | ||
|
||
describe('DonorsContainer', () => { | ||
beforeEach(() => { | ||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: [], | ||
isLoading: false, | ||
}); | ||
}); | ||
|
||
it('should render component', () => { | ||
renderComponent(); | ||
|
||
expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined(); | ||
}); | ||
|
||
it('should render Loading component', () => { | ||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: [], | ||
isLoading: true, | ||
}); | ||
|
||
renderComponent(); | ||
|
||
expect(screen.getByText('Loading')).toBeDefined(); | ||
}); | ||
|
||
it('should call `useFetchDonors` with `donorOrganizationIds`', () => { | ||
const mockData = [{ name: 'Amazon', code: 'AMAZ', id: '1' }]; | ||
|
||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: mockData, | ||
isLoading: false, | ||
}); | ||
|
||
renderComponent({ donorOrganizationIds: ['1'] }); | ||
|
||
expect(screen.getByText(mockData[0].name)).toBeDefined(); | ||
}); | ||
}); |
usavkov-epam marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, { useMemo } from 'react'; | ||
import { sortBy } from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { | ||
Button, | ||
Icon, | ||
MultiColumnList, | ||
TextLink, | ||
} from '@folio/stripes/components'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
|
||
import AddDonorButton from './AddDonorButton'; | ||
import { | ||
alignRowProps, | ||
columnMapping, | ||
columnWidths, | ||
visibleColumns, | ||
} from './constants'; | ||
|
||
const getDonorUrl = (orgId) => { | ||
if (orgId) { | ||
return `/organizations/view/${orgId}`; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
const getResultsFormatter = ({ | ||
canViewOrganizations, | ||
fields, | ||
intl, | ||
}) => ({ | ||
name: donor => <TextLink to={getDonorUrl(canViewOrganizations && donor.id)}>{donor.name}</TextLink>, | ||
code: donor => donor.code, | ||
unassignDonor: donor => ( | ||
<Button | ||
align="end" | ||
aria-label={intl.formatMessage({ id: 'stripes-acq-components.donors.button.unassign' })} | ||
buttonStyle="fieldControl" | ||
type="button" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
fields.remove(donor._index); | ||
}} | ||
> | ||
<Icon icon="times-circle" /> | ||
</Button> | ||
), | ||
}); | ||
|
||
const DonorsList = ({ setDonorIds, fields, donorsMap, id }) => { | ||
const intl = useIntl(); | ||
const stripes = useStripes(); | ||
const canViewOrganizations = stripes.hasPerm('ui-organizations.view'); | ||
|
||
const donors = useMemo(() => (fields.value || []) | ||
.map((contactId, _index) => { | ||
const contact = donorsMap?.[contactId]; | ||
|
||
return { | ||
...(contact || { isDeleted: true }), | ||
_index, | ||
}; | ||
}), [donorsMap, fields.value]); | ||
|
||
const contentData = useMemo(() => sortBy(donors, [({ lastName }) => lastName?.toLowerCase()]), [donors]); | ||
usavkov-epam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const resultsFormatter = useMemo(() => { | ||
return getResultsFormatter({ intl, fields, canViewOrganizations }); | ||
}, [canViewOrganizations, fields, intl]); | ||
|
||
return ( | ||
<> | ||
<MultiColumnList | ||
alisher-epam marked this conversation as resolved.
Show resolved
Hide resolved
usavkov-epam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id={id} | ||
columnMapping={columnMapping} | ||
contentData={contentData} | ||
formatter={resultsFormatter} | ||
rowProps={alignRowProps} | ||
visibleColumns={visibleColumns} | ||
columnWidths={columnWidths} | ||
/> | ||
<br /> | ||
<AddDonorButton | ||
onAddDonors={setDonorIds} | ||
fields={fields} | ||
stripes={stripes} | ||
name={id} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
DonorsList.propTypes = { | ||
setDonorIds: PropTypes.func.isRequired, | ||
fields: PropTypes.object, | ||
donorsMap: PropTypes.object, | ||
id: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default DonorsList; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test file for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added