diff --git a/package.json b/package.json index c1ce21e0..7f108ad2 100644 --- a/package.json +++ b/package.json @@ -305,6 +305,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-intl": "^6.4.4", + "react-query": "^3.6.0", "react-router-dom": "^5.2.0" } } diff --git a/src/components/Period/Period.js b/src/components/Period/Period.js index 9f999028..65d6b441 100644 --- a/src/components/Period/Period.js +++ b/src/components/Period/Period.js @@ -1,31 +1,24 @@ -import React from 'react'; +import React, { useRef } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from 'react-intl'; - -import { - Field, -} from 'react-final-form'; - -import { - get, - isEmpty, - isNumber, -} from 'lodash'; +import { Field } from 'react-final-form'; +import { get, isEmpty, isNumber } from 'lodash'; import { Col, Row, Select, TextField, - Label, + Label } from '@folio/stripes/components'; -import css from './Period.css'; import { shortTermExpiryPeriod, shortTermClosedDateManagementMenu, longTermClosedDateManagementMenu } from '../../settings/ServicePoints/constants'; +import css from './Period.css'; + const validateDuration = value => { if (typeof value !== 'number') { @@ -39,32 +32,19 @@ const validateDuration = value => { return undefined; }; -class Period extends React.Component { - static propTypes = { - fieldLabel: PropTypes.string.isRequired, - selectPlaceholder: PropTypes.string.isRequired, - dependentValuePath: PropTypes.string.isRequired, - inputValuePath: PropTypes.string.isRequired, - selectValuePath: PropTypes.string.isRequired, - entity: PropTypes.object.isRequired, - intervalPeriods: PropTypes.arrayOf(PropTypes.object), - changeFormValue: PropTypes.func.isRequired, - }; - - constructor(props) { - super(props); - - this.inputRef = React.createRef(); - } - - onInputBlur = () => { - const { - inputValuePath, - selectValuePath, - entity, - changeFormValue, - } = this.props; - +const Period = ({ + fieldLabel, + selectPlaceholder, + dependentValuePath, + inputValuePath, + selectValuePath, + entity, + intervalPeriods, + changeFormValue +}) => { + const inputRef = useRef(null); + + const onInputBlur = () => { const inputValue = get(entity, inputValuePath); if (isNumber(inputValue)) { @@ -74,22 +54,11 @@ class Period extends React.Component { changeFormValue(selectValuePath, ''); }; - onInputClear = () => { - const { - inputValuePath, - changeFormValue, - } = this.props; - + const onInputClear = () => { changeFormValue(inputValuePath, ''); }; - onSelectChange = (e) => { - const { - selectValuePath, - changeFormValue, - dependentValuePath, - } = this.props; - + const onSelectChange = (e) => { changeFormValue(selectValuePath, e.target.value); const holdShelfClosedLibraryDateManagementValue = shortTermExpiryPeriod.findIndex(item => item === e.target.value) > -1 @@ -97,10 +66,10 @@ class Period extends React.Component { : longTermClosedDateManagementMenu[0].value; changeFormValue(dependentValuePath, holdShelfClosedLibraryDateManagementValue); - this.inputRef.current.focus(); + inputRef.current.focus(); }; - transformInputValue = (value) => { + const transformInputValue = (value) => { if (isEmpty(value)) { return ''; } @@ -108,12 +77,7 @@ class Period extends React.Component { return Number(value); }; - generateOptions = () => { - const { - intervalPeriods, - selectValuePath, - } = this.props; - + const generateOptions = () => { return intervalPeriods.map(({ value, label }) => (