-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into UIORGS-398
- Loading branch information
Showing
48 changed files
with
1,143 additions
and
52 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
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
60 changes: 60 additions & 0 deletions
60
src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.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,60 @@ | ||
import React from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { getControlledVocabTranslations } from '@folio/stripes-acq-components'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
import { ControlledVocab } from '@folio/stripes/smart-components'; | ||
|
||
import { BANKING_ACCOUNT_TYPES_API } from '../constants'; | ||
|
||
const setUniqValidation = (value, index, items) => { | ||
const errors = {}; | ||
|
||
const isBankingAccountTypeExist = items.some(({ id, name }) => { | ||
return name?.toLowerCase() === value?.name?.toLowerCase() && id !== value?.id; | ||
}); | ||
|
||
if (isBankingAccountTypeExist) { | ||
errors.name = <FormattedMessage id="ui-organizations.settings.accountTypes.save.error.accountTypeMustBeUnique" />; | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
const BankingAccountTypeSettings = () => { | ||
const stripes = useStripes(); | ||
const ConnectedComponent = stripes.connect(ControlledVocab); | ||
|
||
const columnMapping = { | ||
name: <FormattedMessage id="ui-organizations.settings.accountTypes.name" />, | ||
action: <FormattedMessage id="ui-organizations.settings.accountTypes.action" />, | ||
}; | ||
|
||
const hasEditPerms = stripes.hasPerm('ui-organizations.settings'); | ||
const actionSuppressor = { | ||
edit: () => !hasEditPerms, | ||
delete: () => !hasEditPerms, | ||
}; | ||
|
||
return ( | ||
<ConnectedComponent | ||
actionSuppressor={actionSuppressor} | ||
canCreate={hasEditPerms} | ||
stripes={stripes} | ||
baseUrl={BANKING_ACCOUNT_TYPES_API} | ||
records="bankingAccountTypes" | ||
label={<FormattedMessage id="ui-organizations.settings.bankingAccountTypes" />} | ||
translations={getControlledVocabTranslations('ui-organizations.settings.bankingAccountTypes')} | ||
objectLabel="BankingAccountTypes" | ||
visibleFields={['name']} | ||
columnMapping={columnMapping} | ||
hiddenFields={['lastUpdated', 'numberOfObjects']} | ||
nameKey="bankingAccountTypes" | ||
id="bankingAccountTypes" | ||
validate={setUniqValidation} | ||
sortby="name" | ||
/> | ||
); | ||
}; | ||
|
||
export default BankingAccountTypeSettings; |
66 changes: 66 additions & 0 deletions
66
src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.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,66 @@ | ||
import { | ||
render, | ||
screen, | ||
} from '@folio/jest-config-stripes/testing-library/react'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
import { ControlledVocab } from '@folio/stripes/smart-components'; | ||
|
||
import BankingAccountTypeSettings from './BankingAccountTypeSettings'; | ||
|
||
jest.mock('@folio/stripes/core', () => ({ | ||
...jest.requireActual('@folio/stripes/core'), | ||
useStripes: jest.fn(), | ||
})); | ||
|
||
jest.mock('@folio/stripes-smart-components/lib/ControlledVocab', () => jest.fn(({ | ||
rowFilter, | ||
label, | ||
rowFilterFunction, | ||
preCreateHook, | ||
listSuppressor, | ||
}) => ( | ||
<> | ||
{label} | ||
<div onChange={rowFilterFunction}>{rowFilter}</div> | ||
<button | ||
data-testid="button-new" | ||
type="button" | ||
onClick={() => { | ||
preCreateHook(); | ||
listSuppressor(); | ||
}} | ||
> | ||
New | ||
</button> | ||
</> | ||
))); | ||
|
||
const stripesMock = { | ||
connect: component => component, | ||
hasPerm: jest.fn(() => true), | ||
clone: jest.fn(), | ||
}; | ||
|
||
const renderCategorySettings = () => render(<BankingAccountTypeSettings />); | ||
|
||
describe('BankingAccountTypeSettings', () => { | ||
beforeEach(() => { | ||
useStripes.mockReturnValue(stripesMock); | ||
}); | ||
|
||
it('should render component', () => { | ||
renderCategorySettings(); | ||
|
||
expect(screen.getByText('New')); | ||
expect(screen.getByText('ui-organizations.settings.bankingAccountTypes')); | ||
}); | ||
|
||
it('should check action suppression', () => { | ||
renderCategorySettings(); | ||
|
||
const { actionSuppressor } = ControlledVocab.mock.calls[0][0]; | ||
|
||
expect(actionSuppressor.edit()).toBeFalsy(); | ||
expect(actionSuppressor.delete()).toBeFalsy(); | ||
}); | ||
}); |
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 as BankingAccountTypeSettings } from './BankingAccountTypeSettings'; |
53 changes: 53 additions & 0 deletions
53
src/Settings/BankingInformationSettings/BankingInformationSettings.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,53 @@ | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { useShowCallout } from '@folio/stripes-acq-components'; | ||
import { Loading } from '@folio/stripes/components'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { SETTINGS_API } from '../constants'; | ||
import { useBankingInformation } from '../hooks'; | ||
import BankingInformationSettingsForm from './BankingInformationSettingsForm'; | ||
|
||
const BankingInformationSettings = () => { | ||
const { | ||
enabled, | ||
key, | ||
id: bankingInformationId, | ||
version, | ||
isLoading, | ||
refetch, | ||
} = useBankingInformation(); | ||
const ky = useOkapiKy(); | ||
const sendCallout = useShowCallout(); | ||
|
||
const onSubmit = async ({ value }) => { | ||
try { | ||
await ky.put(`${SETTINGS_API}/${bankingInformationId}`, { | ||
json: { value, key, _version: version }, | ||
}); | ||
|
||
refetch(); | ||
sendCallout({ | ||
message: <FormattedMessage id="ui-organizations.settings.accountTypes.save.success.message" />, | ||
}); | ||
} catch (error) { | ||
sendCallout({ | ||
type: 'error', | ||
message: <FormattedMessage id="ui-organizations.settings.accountTypes.save.error.generic.message" />, | ||
}); | ||
} | ||
}; | ||
|
||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<BankingInformationSettingsForm | ||
onSubmit={onSubmit} | ||
initialValues={{ value: enabled }} | ||
/> | ||
); | ||
}; | ||
|
||
export default BankingInformationSettings; |
84 changes: 84 additions & 0 deletions
84
src/Settings/BankingInformationSettings/BankingInformationSettings.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,84 @@ | ||
import { MemoryRouter } from 'react-router-dom'; | ||
|
||
import { | ||
act, | ||
render, | ||
screen, | ||
} from '@folio/jest-config-stripes/testing-library/react'; | ||
import user from '@folio/jest-config-stripes/testing-library/user-event'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { useBankingInformation } from '../hooks'; | ||
import BankingInformationSettings from './BankingInformationSettings'; | ||
|
||
const mockRefetch = jest.fn(); | ||
|
||
jest.mock('@folio/stripes/components', () => ({ | ||
...jest.requireActual('@folio/stripes/components'), | ||
Loading: () => <div>Loading</div>, | ||
})); | ||
|
||
jest.mock('../hooks', () => ({ | ||
useBankingInformation: jest.fn(() => ({ | ||
isLoading: false, | ||
enabled: false, | ||
refetch: mockRefetch, | ||
})), | ||
})); | ||
|
||
const renderBankingInformationSettings = () => render( | ||
<BankingInformationSettings />, | ||
{ wrapper: MemoryRouter }, | ||
); | ||
|
||
describe('BankingInformationSettings component', () => { | ||
it('should display pane headings', () => { | ||
renderBankingInformationSettings(); | ||
|
||
const paneTitle = screen.getByText('ui-organizations.settings.bankingInformation'); | ||
const checkboxLabel = screen.getByText('ui-organizations.settings.bankingInformation.enable'); | ||
|
||
expect(paneTitle).toBeInTheDocument(); | ||
expect(checkboxLabel).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render Loading component', () => { | ||
useBankingInformation.mockReturnValue({ | ||
isLoading: true, | ||
enabled: false, | ||
}); | ||
|
||
renderBankingInformationSettings(); | ||
|
||
expect(screen.getByText('Loading')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should save banking options', async () => { | ||
useBankingInformation.mockClear().mockReturnValue({ | ||
isLoading: false, | ||
enabled: true, | ||
refetch: mockRefetch, | ||
}); | ||
const mockPutMethod = jest.fn(() => ({ | ||
json: () => Promise.resolve('ok'), | ||
})); | ||
|
||
useOkapiKy | ||
.mockClear() | ||
.mockReturnValue({ | ||
put: mockPutMethod, | ||
}); | ||
|
||
renderBankingInformationSettings(); | ||
|
||
const checkbox = screen.getByRole('checkbox', { name: 'ui-organizations.settings.bankingInformation.enable' }); | ||
const saveButton = screen.getByText('ui-organizations.settings.accountTypes.save.button'); | ||
|
||
await act(async () => { | ||
await user.click(checkbox); | ||
await user.click(saveButton); | ||
}); | ||
|
||
expect(mockPutMethod).toHaveBeenCalled(); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
src/Settings/BankingInformationSettings/BankingInformationSettingsForm.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,73 @@ | ||
import { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Field } from 'react-final-form'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { | ||
Button, | ||
Checkbox, | ||
Col, | ||
Pane, | ||
PaneFooter, | ||
PaneHeader, | ||
Row, | ||
} from '@folio/stripes/components'; | ||
import stripesForm from '@folio/stripes/final-form'; | ||
|
||
const BankingInformationSettingsForm = ({ | ||
handleSubmit, | ||
pristine, | ||
submitting, | ||
}) => { | ||
const paneFooter = useMemo(() => { | ||
const end = ( | ||
<Button | ||
id="clickable-save-contact-person-footer" | ||
type="submit" | ||
buttonStyle="primary mega" | ||
disabled={pristine || submitting} | ||
onClick={handleSubmit} | ||
> | ||
<FormattedMessage id="ui-organizations.settings.accountTypes.save.button" /> | ||
</Button> | ||
); | ||
|
||
return <PaneFooter renderEnd={end} />; | ||
}, [handleSubmit, pristine, submitting]); | ||
|
||
const paneTitle = <FormattedMessage id="ui-organizations.settings.bankingInformation" />; | ||
|
||
return ( | ||
<Pane | ||
defaultWidth="fill" | ||
id="banking-information" | ||
renderHeader={renderProps => <PaneHeader {...renderProps} paneTitle={paneTitle} />} | ||
footer={paneFooter} | ||
> | ||
<Row> | ||
<Col xs={12}> | ||
<Field | ||
component={Checkbox} | ||
label={<FormattedMessage id="ui-organizations.settings.bankingInformation.enable" />} | ||
name="value" | ||
type="checkbox" | ||
/> | ||
</Col> | ||
</Row> | ||
</Pane> | ||
); | ||
}; | ||
|
||
BankingInformationSettingsForm.propTypes = { | ||
handleSubmit: PropTypes.func.isRequired, | ||
pristine: PropTypes.bool.isRequired, | ||
submitting: PropTypes.bool.isRequired, | ||
renderHeader: PropTypes.func, | ||
}; | ||
|
||
export default stripesForm({ | ||
enableReinitialize: true, | ||
keepDirtyOnReinitialize: true, | ||
navigationCheck: true, | ||
subscription: { values: true }, | ||
})(BankingInformationSettingsForm); |
Oops, something went wrong.