Skip to content

Commit

Permalink
use event to initialize selected records on datwhen data loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Dec 8, 2023
1 parent f7c8e3d commit 15e7cc5
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 169 deletions.
208 changes: 54 additions & 154 deletions lib/FindLocation/FindLocation.js
Original file line number Diff line number Diff line change
@@ -1,170 +1,70 @@
import keyBy from 'lodash/keyBy';
import PropTypes from 'prop-types';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { useCallback, useRef, useState } from 'react';

import { ColumnManager } from '@folio/stripes/smart-components';
import { Button } from '@folio/stripes/components';

import { EVENT_EMMITER_EVENTS } from '../constants';4
import { FindRecords } from '../FindRecords';
import {
useCampuses,
useEventEmitter,
useInstitutions,
useLibraries,
} from '../hooks';
import {
COLUMN_MAPPING,
COLUMN_NAMES,
NON_TOGGLEABLE_COLUMNS,
SORTABLE_COLUMNS,
} from './configs'
import { FindLocationFilters } from './FindLocationFilters';
import { useLocationsRecords } from './useLocationsRecords';
import { FindLocationLookup } from './FindLocationLookup';

export const FindLocation = (props) => {
const {
idPrefix,
isMultiSelect,
modalLabel,
onRecordsSelect,
resultsPaneTitle,
sortableColumns,
id,

Check failure on line 9 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'id' is missing in props validation
disabled,

Check failure on line 10 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'disabled' is missing in props validation
marginBottom0,

Check failure on line 11 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'marginBottom0' is missing in props validation
onClose,

Check failure on line 12 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'onClose' is missing in props validation
renderTrigger,

Check failure on line 13 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'renderTrigger' is missing in props validation
searchButtonStyle,

Check failure on line 14 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'searchButtonStyle' is missing in props validation
searchLabel,

Check failure on line 15 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'searchLabel' is missing in props validation
trigerless,

Check failure on line 16 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'trigerless' is missing in props validation
...rest
} = props;

const eventEmitter = useEventEmitter();

const [selectedLocations, setSelectedLocations] = useState({});
const [filters, setFilters] = useState({});

const { institutions, isLoading: isInstitutionsLoading } = useInstitutions();
const { campuses, isLoading: isCampusesLoading } = useCampuses();
const { libraries, isLoading: isLibrariesLoading } = useLibraries();

const institutionsMap = useMemo(() => keyBy(institutions, 'id'), [institutions]);
const campusessMap = useMemo(() => keyBy(campuses, 'id'), [campuses]);
const librariesMap = useMemo(() => keyBy(libraries, 'id'), [libraries]);

const { locations, isLoading: isLocationsLoading } = useLocationsRecords({
filters,
selectedLocations,
institutionsMap,
campusessMap,
librariesMap,
});

useEffect(() => {
const eventType = EVENT_EMMITER_EVENTS.FIND_RECORDS_SELECTED_CHANGED;
const callback = ({ detail: assignment }) => setSelectedLocations(assignment);

eventEmitter.on(eventType, callback);

return () => {
eventEmitter.off(eventType, callback);
};
}, [eventEmitter]);

const columnMapping = useMemo(() => ({
...COLUMN_MAPPING,
...(
isMultiSelect
? { [COLUMN_NAMES.isAssigned]: <FormattedMessage id="stripes-acq-components.find-location.results.column.assignment" /> }
: {}
)
}), [isMultiSelect]);

const resultsFormatter = useMemo(() => ({
[COLUMN_NAMES.name]: ({ name }) => name,
[COLUMN_NAMES.code]: ({ code }) => code,
[COLUMN_NAMES.institution]: ({ institutionId }) => institutionsMap[institutionId]?.name,
[COLUMN_NAMES.campus]: ({ campusId }) => campusessMap[campusId]?.name,
[COLUMN_NAMES.library]: ({ libraryId }) => librariesMap[libraryId]?.name,
[COLUMN_NAMES.isActive]: ({ isActive }) => (
<FormattedMessage id={`stripes-acq-components.find-location.results.column.status.${ isActive ? 'active': 'inactive'}`} />
),
[COLUMN_NAMES.isAssigned]: ({ id }) => (
<FormattedMessage id={`stripes-acq-components.filter.assignment.${ selectedLocations[id] ? 'assigned': 'unassigned'}`} />
),
}), [
campusessMap,
institutionsMap,
librariesMap,
selectedLocations,
const modalTriggerRef = useRef();
const [openModal, setOpenModal] = useState(false);

const onModalClose = useCallback((data) => {
setOpenModal(false);
onClose?.(data);
}, [onClose]);

const renderDefaultTrigger = useCallback(() => {
return (
<Button
id={id}
buttonRef={modalTriggerRef}
buttonStyle={searchButtonStyle}
disabled={disabled}
key="searchButton"
marginBottom0={marginBottom0}
onClick={() => setOpenModal(true)}
>
{searchLabel}
</Button>
);
}, [

Check warning on line 42 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has a missing dependency: 'searchLabel'. Either include it or remove the dependency array
disabled,
id,
marginBottom0,
searchButtonStyle,
]);

const refreshRecords = useCallback((filters) => setFilters(filters), []);

const isLoading = (
isLocationsLoading
|| isInstitutionsLoading
|| isCampusesLoading
|| isLibrariesLoading
);

const renderFilters = useCallback((activeFilters, applyFilters) => (
<FindLocationFilters
activeFilters={activeFilters}
applyFilters={applyFilters}
disabled={isLoading}
isMultiSelect={isMultiSelect}
institutions={institutions}
campuses={campuses}
libraries={libraries}
/>
), [
campuses,
institutions,
isMultiSelect,
isLoading,
libraries,
]);
const renderTriggerButton = useCallback(() => {
return renderTrigger
? renderTrigger({
buttonRef: modalTriggerRef,
onClick: () => setOpenModal(true),
})
: renderDefaultTrigger();
}, []);

Check warning on line 56 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has missing dependencies: 'renderDefaultTrigger' and 'renderTrigger'. Either include them or remove the dependency array

return (
<ColumnManager
id="find-locations"
columnMapping={columnMapping}
excludeKeys={NON_TOGGLEABLE_COLUMNS}
>
{({ renderColumnsMenu, visibleColumns }) => (
<FindRecords
modalLabel={modalLabel}
resultsPaneTitle={resultsPaneTitle}
idPrefix={idPrefix}
visibleColumns={visibleColumns}
sortableColumns={sortableColumns}
columnMapping={columnMapping}
resultsFormatter={resultsFormatter}
records={locations}
totalCount={locations.length}
refreshRecords={refreshRecords}
isMultiSelect={isMultiSelect}
isLoading={isLoading}
selectRecords={onRecordsSelect}
stickyFirstColumn={isMultiSelect}
renderActionMenu={() => renderColumnsMenu}
renderFilters={renderFilters}
<>
{!trigerless && renderTriggerButton()}
{(openModal || trigerless) && (
<FindLocationLookup
onClose={onModalClose}
trigerless
{...rest}
/>
)}
</ColumnManager>
);
};

FindLocation.propTypes = {
idPrefix: PropTypes.string,
isMultiSelect: PropTypes.bool,
modalLabel: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
onRecordsSelect: PropTypes.func.isRequired,
resultsPaneTitle: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
sortableColumns: PropTypes.arrayOf(PropTypes.string),
visibleColumns: PropTypes.arrayOf(PropTypes.string),
};

FindLocation.defaultProps = {
idPrefix: 'FindLocation-',
isMultiSelect: false,
modalLabel: <FormattedMessage id="stripes-acq-components.find-location.modal.label" />,
resultsPaneTitle: <FormattedMessage id="stripes-acq-components.find-location.results.heading" />,
sortableColumns: SORTABLE_COLUMNS,
</>
)

Check failure on line 69 in lib/FindLocation/FindLocation.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Missing semicolon
};
Loading

0 comments on commit 15e7cc5

Please sign in to comment.