-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIU-3115 - reading room acecss acoordion on user edit view
- Loading branch information
1 parent
ac8d11e
commit 56277f9
Showing
13 changed files
with
353 additions
and
2 deletions.
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
67 changes: 67 additions & 0 deletions
67
src/components/EditSections/EditReadingRoomAccess/EditReadingRoomAccess.js
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,67 @@ | ||
import React, { useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { noop } from 'lodash'; | ||
|
||
import { | ||
Accordion, | ||
Headline, | ||
MultiColumnList, | ||
} from '@folio/stripes/components'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
|
||
import { rraColumns } from './constants'; | ||
import { getFormatter } from './getFormatter'; | ||
|
||
const EditReadingRoomAccess = ({ | ||
expanded, | ||
onToggle, | ||
accordionId, | ||
form, | ||
formData, | ||
}) => { | ||
const stripes = useStripes(); | ||
const columnMapping = { | ||
[rraColumns.ACCESS]: <FormattedMessage id="ui-users.readingRoom.access" />, | ||
[rraColumns.READING_ROOM_NAME]: <FormattedMessage id="ui-users.readingRoom.name" />, | ||
[rraColumns.NOTES]: <FormattedMessage id="ui-users.readingRoom.note" />, | ||
}; | ||
const visibleColumns = Object.keys(columnMapping); | ||
const readOnly = !stripes.hasPerm('reading-room-patron-permission.item.put'); | ||
|
||
useEffect(() => { | ||
const unregisterReadingRoomAccessList = form.registerField('readingRoomsAccessList', noop, { initialValue: [] }); | ||
return () => { | ||
unregisterReadingRoomAccessList(); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return ( | ||
<Accordion | ||
open={expanded} | ||
id={accordionId} | ||
onToggle={onToggle} | ||
label={<Headline size="large" tag="h3"><FormattedMessage id="ui-users.readingRoom.readingRoomAccess" /></Headline>} | ||
displayWhenClosed={formData.length} | ||
> | ||
<MultiColumnList | ||
striped | ||
contentData={formData} | ||
columnMapping={columnMapping} | ||
visibleColumns={visibleColumns} | ||
formatter={getFormatter(form, readOnly)} | ||
/> | ||
</Accordion> | ||
); | ||
}; | ||
|
||
EditReadingRoomAccess.propTypes = { | ||
expanded: PropTypes.bool, | ||
onToggle: PropTypes.func, | ||
accordionId: PropTypes.string.isRequired, | ||
formData: PropTypes.object, | ||
form: PropTypes.object, | ||
}; | ||
|
||
export default EditReadingRoomAccess; |
116 changes: 116 additions & 0 deletions
116
src/components/EditSections/EditReadingRoomAccess/EditReadingRoomAccess.test.js
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,116 @@ | ||
import { screen, waitFor } from '@folio/jest-config-stripes/testing-library/react'; | ||
import userEvent from '@folio/jest-config-stripes/testing-library/user-event'; | ||
import { within } from '@folio/jest-config-stripes/testing-library/dom'; | ||
import { Form } from 'react-final-form'; | ||
|
||
import renderWithRouter from 'helpers/renderWithRouter'; | ||
import '../../../../test/jest/__mock__/matchMedia.mock'; | ||
|
||
import EditReadingRoomAccess from './EditReadingRoomAccess'; | ||
|
||
jest.unmock('@folio/stripes/components'); | ||
|
||
const unregisterFieldMock = jest.fn(); | ||
|
||
const onSubmit = jest.fn(); | ||
const arrayMutators = { | ||
concat: jest.fn(), | ||
move: jest.fn(), | ||
pop: jest.fn(), | ||
push: jest.fn(), | ||
remove: jest.fn(), | ||
removeBatch: jest.fn(), | ||
shift: jest.fn(), | ||
swap: jest.fn(), | ||
unshift: jest.fn(), | ||
update: jest.fn() | ||
}; | ||
const renderEditReadingRoomAccess = (props, initialValues) => { | ||
const component = () => ( | ||
<> | ||
<EditReadingRoomAccess {...props} /> | ||
</> | ||
); | ||
renderWithRouter( | ||
<Form | ||
id="form-user" | ||
mutators={{ | ||
...arrayMutators | ||
}} | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
render={component} | ||
/> | ||
); | ||
}; | ||
|
||
const props = { | ||
expanded: true, | ||
onToggle: jest.fn(), | ||
accordionId: 'readingRoomAccess', | ||
form: { | ||
change: jest.fn(), | ||
registerField: jest.fn().mockReturnValue(unregisterFieldMock), | ||
}, | ||
formData: [ | ||
{ | ||
'id': '2205004b-ca51-4a14-87fd-938eefa8f5df', | ||
'userId': '2205005b-ca51-4a04-87fd-938eefa8f6de', | ||
'readingRoomId': 'ea7ac988-ede1-466b-968c-46a770333b14', | ||
'readingRoomName': 'rr-4', | ||
'access': 'ALLOWED', | ||
'notes': 'Allowed for this reading room...', | ||
'metadata': { | ||
'createdDate': '2024-05-15 18:39:31', | ||
'createdByUserId': '21457ab5-4635-4e56-906a-908f05e9233b', | ||
'updatedDate': '2024-05-15 18:40:27', | ||
'updatedByUserId': '21457ab5-4635-4e56-906a-908f05e9233b' | ||
} | ||
}, | ||
{ | ||
'id': 'fe1d83dc-e3f9-4e57-aa2c-0b245ae7eb19', | ||
'userId': '2205005b-ca51-4a04-87fd-938eefa8f6de', | ||
'readingRoomId': '754c6287-892c-4484-941a-23e050fc8888', | ||
'readingRoomName': 'abc', | ||
'access': 'NOT_ALLOWED', | ||
'notes': '', | ||
'metadata': { | ||
'createdDate': '2024-05-21 07:05:17', | ||
'createdByUserId': '21457ab5-4635-4e56-906a-908f05e9233b', | ||
'updatedDate': '2024-05-21 07:13:11', | ||
'updatedByUserId': '21457ab5-4635-4e56-906a-908f05e9233b' | ||
} | ||
} | ||
], | ||
}; | ||
describe('EditReadingRoomAccess', () => { | ||
it('should render component', () => { | ||
renderEditReadingRoomAccess(props); | ||
expect(screen.getByText('ui-users.readingRoom.readingRoomAccess')).toBeDefined(); | ||
}); | ||
|
||
it('should display columns - access, name and note', () => { | ||
renderEditReadingRoomAccess(props); | ||
[ | ||
'ui-users.readingRoom.access', | ||
'ui-users.readingRoom.name', | ||
'ui-users.readingRoom.note', | ||
].forEach(col => expect(screen.getByText(col)).toBeDefined()); | ||
}); | ||
|
||
it('should update the notes', async () => { | ||
renderEditReadingRoomAccess(props); | ||
const noteField1 = document.querySelectorAll('[id^=textarea]')[0]; | ||
await userEvent.type(noteField1, 'note1'); | ||
await waitFor(() => expect(props.form.change).toHaveBeenCalled()); | ||
}); | ||
|
||
it('should update access', async () => { | ||
renderEditReadingRoomAccess(props); | ||
const accessSelectField = document.querySelectorAll('[id=reading-room-access-select]')[0]; | ||
await userEvent.click(accessSelectField); | ||
const list = screen.getByRole('listbox'); | ||
await userEvent.click(within(list).getByText('Not allowed', { exact: false })); | ||
await waitFor(() => expect(props.form.change).toHaveBeenCalled()); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
src/components/EditSections/EditReadingRoomAccess/constants.js
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,17 @@ | ||
export const rraColumns = { | ||
ACCESS: 'access', | ||
READING_ROOM_NAME: 'readingRoomName', | ||
NOTES: 'notes', | ||
}; | ||
|
||
// TODO: Translations to be applied to the options | ||
export const READING_ROOM_ACCESS_OPTIONS = [ | ||
{ | ||
label: 'Allowed', | ||
value: 'ALLOWED' | ||
}, | ||
{ | ||
label: 'Not allowed', | ||
value: 'NOT_ALLOWED' | ||
} | ||
]; |
69 changes: 69 additions & 0 deletions
69
src/components/EditSections/EditReadingRoomAccess/getFormatter.js
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,69 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import PropTypes from 'prop-types'; | ||
import { Field } from 'react-final-form'; | ||
import { cloneDeep } from 'lodash'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { Selection, TextArea } from '@folio/stripes/components'; | ||
|
||
import { rraColumns, READING_ROOM_ACCESS_OPTIONS } from './constants'; | ||
|
||
export const getFormatter = (form, readOnly) => { | ||
const updateRecord = (record, val, name, rowIndex) => { | ||
const clonedRecord = cloneDeep(record); | ||
clonedRecord[name] = val; | ||
if (!clonedRecord.id) { | ||
clonedRecord.id = uuidv4(); | ||
} | ||
form.change(`readingRoomsAccessList[${rowIndex}]`, clonedRecord); | ||
}; | ||
|
||
return ({ | ||
[rraColumns.ACCESS] : Object.assign( | ||
({ rowIndex, ...record }) => ( | ||
<Field | ||
name={`${rraColumns.ACCESS}`} | ||
id={`${rraColumns.ACCESS}-${rowIndex}`} | ||
aria-label={`${rraColumns.ACCESS}-${rowIndex}`} | ||
render={({ input }) => { | ||
return ( | ||
<Selection | ||
ariaLabel="reading-room access" | ||
dataOptions={READING_ROOM_ACCESS_OPTIONS} | ||
id="reading-room-access-select" | ||
disabled={readOnly} | ||
value={record.access} | ||
onChange={(val) => { | ||
updateRecord(record, val, input.name, rowIndex); | ||
}} | ||
/> | ||
); | ||
}} | ||
/> | ||
), | ||
{ rowIndex: PropTypes.number, record: PropTypes.object } | ||
), | ||
[rraColumns.READING_ROOM_NAME] : ({ readingRoomName }) => readingRoomName, | ||
[rraColumns.NOTES] : Object.assign( | ||
({ rowIndex, ...record }) => ( | ||
<Field | ||
name={`${rraColumns.NOTES}`} | ||
ariaLabel={`${rraColumns.NOTES}-${rowIndex}`} | ||
id={`${rraColumns.NOTES}-${rowIndex}`} | ||
render={({ input }) => ( | ||
<TextArea | ||
{...input} | ||
fullWidth | ||
value={record?.notes} | ||
disabled={readOnly} | ||
onChange={(e) => { | ||
updateRecord(record, e.target.value, input.name, rowIndex); | ||
}} | ||
/> | ||
)} | ||
/> | ||
), | ||
{ rowIndex: PropTypes.number, record: PropTypes.object } | ||
) | ||
}); | ||
}; |
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 @@ | ||
export { default } from './EditReadingRoomAccess'; |
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
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
Oops, something went wrong.