diff --git a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js index 25462f17..705e4ae5 100644 --- a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js +++ b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js @@ -1,69 +1,62 @@ -import React, { Component } from 'react'; import { FormattedMessage } from 'react-intl'; -import { stripesShape } from '@folio/stripes/core'; +import { stripesConnect, stripesShape } from '@folio/stripes/core'; import { ControlledVocab } from '@folio/stripes/smart-components'; import { getControlledVocabTranslations } from '@folio/stripes-acq-components'; -class BankingAccountTypeSettings extends Component { - constructor(props) { - super(props); - this.connectedControlledVocab = props.stripes.connect(ControlledVocab); - } - - render() { - const { stripes } = this.props; - const columnMapping = { - value: , - action: , - }; - - const hasEditPerms = stripes.hasPerm('ui-organizations.settings'); - const actionSuppressor = { - edit: () => !hasEditPerms, - delete: () => !hasEditPerms, - }; - - const ConnectedComponent = this.connectedControlledVocab; - - const setUniqValidation = (value, index, items) => { - const errors = {}; - - const isAccountTypeExist = items.some(({ id, name }) => { - return name?.toLowerCase() === value?.name?.toLowerCase() && id !== value?.id; - }); - - if (isAccountTypeExist) { - errors.name = ; - } - - return errors; - }; - - return ( - } - translations={getControlledVocabTranslations('ui-organizations.settings.accountTypes')} - objectLabel="BankingAccountTypes" - visibleFields={['name']} - columnMapping={columnMapping} - hiddenFields={['lastUpdated', 'numberOfObjects']} - nameKey="bankingAccountTypes" - id="bankingAccountTypes" - validate={setUniqValidation} - sortby="name" - /> - ); - } -} +import { BANKING_ACCOUNT_TYPES_API } from '../constants'; + +const BankingAccountTypeSettings = ({ stripes }) => { + const ConnectedComponent = stripes.connect(ControlledVocab); + + const columnMapping = { + value: , + action: , + }; + + const hasEditPerms = stripes.hasPerm('ui-organizations.settings'); + const actionSuppressor = { + edit: () => !hasEditPerms, + delete: () => !hasEditPerms, + }; + + 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 = ; + } + + return errors; + }; + + return ( + } + translations={getControlledVocabTranslations('ui-organizations.settings.bankingAccountTypes')} + objectLabel="BankingAccountTypes" + visibleFields={['name']} + columnMapping={columnMapping} + hiddenFields={['lastUpdated', 'numberOfObjects']} + nameKey="bankingAccountTypes" + id="bankingAccountTypes" + validate={setUniqValidation} + sortby="name" + /> + ); +}; BankingAccountTypeSettings.propTypes = { stripes: stripesShape.isRequired, }; -export default BankingAccountTypeSettings; +export default stripesConnect(BankingAccountTypeSettings); diff --git a/src/Settings/BankingInformationSettings/BankingInformationSettings.js b/src/Settings/BankingInformationSettings/BankingInformationSettings.js index 47c23eab..fb29d547 100644 --- a/src/Settings/BankingInformationSettings/BankingInformationSettings.js +++ b/src/Settings/BankingInformationSettings/BankingInformationSettings.js @@ -1,25 +1,46 @@ import { FormattedMessage } from 'react-intl'; import { Field, Form } from 'react-final-form'; +import { useQueryClient } from 'react-query'; import { Button, Col, Headline, + Loading, Pane, RadioButton, RadioButtonGroup, Row, } from '@folio/stripes/components'; +import { + useOkapiKy, + useNamespace, +} from '@folio/stripes/core'; + +import { useBankingInformation } from '../hooks/useBankingInformation'; +import { SETTINGS_API } from '../constants'; const BankingInformationSettings = () => { - const onSubmit = (values) => { - console.log(values); + const { enabled, key, id: bankingInformationId, isLoading } = useBankingInformation(); + const ky = useOkapiKy(); + const queryClient = useQueryClient(); + const [namespace] = useNamespace({ key: 'banking-information-settings' }); + + const onSubmit = ({ value }) => { + ky.put(`${SETTINGS_API}/${bankingInformationId}`, { + json: { value, key }, + }).then(() => { + queryClient.invalidateQueries([namespace]); + }); }; + if (isLoading) { + return ; + } + return ( } > @@ -32,11 +53,11 @@ const BankingInformationSettings = () => {
( , - perm: 'settings.organizations.enabled', - route: 'account-types', - }, ]; -const SettingsPage = (props) => ( - } - /> -); +const bankingAccountTypesPage = { + component: BankingAccountTypeSettings, + label: , + perm: 'settings.organizations.enabled', + route: 'banking-account-types', +}; + +const SettingsPage = (props) => { + const { enabled, isLoading } = useBankingInformation(); + + const settingsPages = useMemo(() => (enabled ? pages.concat(bankingAccountTypesPage) : pages), [enabled]); + + if (isLoading) { + return ; + } + + return ( + } + /> + ); +}; export default SettingsPage; diff --git a/src/Settings/constants.js b/src/Settings/constants.js new file mode 100644 index 00000000..33902d7f --- /dev/null +++ b/src/Settings/constants.js @@ -0,0 +1,4 @@ +export const SETTINGS_API = 'organizations-storage/settings'; +export const BANKING_ACCOUNT_TYPES_API = 'organizations-storage/banking-account-types'; + +export const MAX_LIMIT = 1; diff --git a/src/Settings/hooks/useBankingInformation.js b/src/Settings/hooks/useBankingInformation.js new file mode 100644 index 00000000..4407056f --- /dev/null +++ b/src/Settings/hooks/useBankingInformation.js @@ -0,0 +1,33 @@ +import { useQuery } from 'react-query'; +import { get } from 'lodash'; + +import { + useNamespace, + useOkapiKy, +} from '@folio/stripes/core'; + +import { MAX_LIMIT, SETTINGS_API } from '../constants'; + +export const useBankingInformation = () => { + const ky = useOkapiKy(); + const [namespace] = useNamespace({ key: 'banking-information-settings' }); + + const searchParams = { + limit: MAX_LIMIT, + query: 'key=BANKING_INFORMATION_ENABLED', + }; + + const { isLoading, data } = useQuery( + [namespace], + () => ky.get(SETTINGS_API, { searchParams }).json().catch(() => null), + ); + + const bankingInformation = get(data, 'settings[0]', {}); + + return ({ + id: bankingInformation.id, + enabled: bankingInformation.value === 'true', + key: bankingInformation.key, + isLoading, + }); +}; diff --git a/translations/ui-organizations/en.json b/translations/ui-organizations/en.json index e943b434..7fa0aa18 100644 --- a/translations/ui-organizations/en.json +++ b/translations/ui-organizations/en.json @@ -445,8 +445,13 @@ "settings.bankingInformation": "Banking information", "settings.bankingInformation.enabled": "Enable", "settings.bankingInformation.disabled": "Disable", - "settings.accountTypes": "Account types", - "settings.accountTypes.save.error.accountTypeMustBeUnique": "Account type must be uniq", + "settings.bankingAccountTypes": "Account types", + "settings.bankingAccountTypes.cannotDeleteTermHeader": "Cannot delete banking account type", + "settings.bankingAccountTypes.cannotDeleteTermMessage": "This banking account type cannot be deleted, as it is in use by one or more records.", + "settings.bankingAccountTypes.deleteEntry": "Delete banking account type", + "settings.bankingAccountTypes.termDeleted": "The banking account type {term} was successfully deleted", + "settings.bankingAccountTypes.termWillBeDeleted": "The banking account type {term} will be deleted.", + "settings.accountTypes.save.error.accountTypeMustBeUnique": "Banking account type must be unique.", "permission.view": "Organizations: View", "permission.edit": "Organizations: View, edit",