|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { sortBy } from 'lodash'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import { useIntl } from 'react-intl'; |
| 5 | + |
| 6 | +import { |
| 7 | + Button, |
| 8 | + Icon, |
| 9 | + MultiColumnList, |
| 10 | + TextLink, |
| 11 | +} from '@folio/stripes/components'; |
| 12 | +import { useStripes } from '@folio/stripes/core'; |
| 13 | + |
| 14 | +import AddDonorButton from './AddDonorButton'; |
| 15 | +import { |
| 16 | + alignRowProps, |
| 17 | + columnMapping, |
| 18 | + columnWidths, |
| 19 | + visibleColumns, |
| 20 | +} from './constants'; |
| 21 | + |
| 22 | +const getDonorUrl = (orgId) => { |
| 23 | + if (orgId) { |
| 24 | + return `/organizations/view/${orgId}`; |
| 25 | + } |
| 26 | + |
| 27 | + return undefined; |
| 28 | +}; |
| 29 | + |
| 30 | +const getResultsFormatter = ({ |
| 31 | + canViewOrganizations, |
| 32 | + fields, |
| 33 | + intl, |
| 34 | +}) => ({ |
| 35 | + name: donor => <TextLink to={getDonorUrl(canViewOrganizations && donor.id)}>{donor.name}</TextLink>, |
| 36 | + code: donor => donor.code, |
| 37 | + unassignDonor: donor => ( |
| 38 | + <Button |
| 39 | + align="end" |
| 40 | + aria-label={intl.formatMessage({ id: 'stripes-acq-components.donors.button.unassign' })} |
| 41 | + buttonStyle="fieldControl" |
| 42 | + type="button" |
| 43 | + onClick={(e) => { |
| 44 | + e.preventDefault(); |
| 45 | + fields.remove(donor._index); |
| 46 | + }} |
| 47 | + > |
| 48 | + <Icon icon="times-circle" /> |
| 49 | + </Button> |
| 50 | + ), |
| 51 | +}); |
| 52 | + |
| 53 | +const DonorsList = ({ setDonorIds, fields, donorsMap, id }) => { |
| 54 | + const intl = useIntl(); |
| 55 | + const stripes = useStripes(); |
| 56 | + const canViewOrganizations = stripes.hasPerm('ui-organizations.view'); |
| 57 | + |
| 58 | + const donors = useMemo(() => (fields.value || []) |
| 59 | + .map((contactId, _index) => { |
| 60 | + const contact = donorsMap?.[contactId]; |
| 61 | + |
| 62 | + return { |
| 63 | + ...(contact || { isDeleted: true }), |
| 64 | + _index, |
| 65 | + }; |
| 66 | + }), [donorsMap, fields.value]); |
| 67 | + |
| 68 | + const contentData = useMemo(() => sortBy(donors, [({ lastName }) => lastName?.toLowerCase()]), [donors]); |
| 69 | + |
| 70 | + const resultsFormatter = useMemo(() => { |
| 71 | + return getResultsFormatter({ intl, fields, canViewOrganizations }); |
| 72 | + }, [canViewOrganizations, fields, intl]); |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + <MultiColumnList |
| 77 | + id={id} |
| 78 | + columnMapping={columnMapping} |
| 79 | + contentData={contentData} |
| 80 | + formatter={resultsFormatter} |
| 81 | + rowProps={alignRowProps} |
| 82 | + visibleColumns={visibleColumns} |
| 83 | + columnWidths={columnWidths} |
| 84 | + /> |
| 85 | + <br /> |
| 86 | + <AddDonorButton |
| 87 | + onAddDonors={setDonorIds} |
| 88 | + fields={fields} |
| 89 | + stripes={stripes} |
| 90 | + name={id} |
| 91 | + /> |
| 92 | + </> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +DonorsList.propTypes = { |
| 97 | + setDonorIds: PropTypes.func.isRequired, |
| 98 | + fields: PropTypes.object, |
| 99 | + donorsMap: PropTypes.object, |
| 100 | + id: PropTypes.string.isRequired, |
| 101 | +}; |
| 102 | + |
| 103 | +export default DonorsList; |
0 commit comments