Skip to content

Commit

Permalink
UITEN-299 Rewrite all class components to functional ones (ui-tenant-…
Browse files Browse the repository at this point in the history
…settings module)
  • Loading branch information
vashjs committed Aug 5, 2024
1 parent c539bdd commit 446fc74
Show file tree
Hide file tree
Showing 64 changed files with 2,842 additions and 3,417 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
190 changes: 78 additions & 112 deletions src/components/Period/Period.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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)) {
Expand All @@ -74,104 +54,90 @@ 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
? shortTermClosedDateManagementMenu[0].value
: longTermClosedDateManagementMenu[0].value;
changeFormValue(dependentValuePath, holdShelfClosedLibraryDateManagementValue);

this.inputRef.current.focus();
inputRef.current.focus();
};

transformInputValue = (value) => {
const transformInputValue = (value) => {
if (isEmpty(value)) {
return '';
}

return Number(value);
};

generateOptions = () => {
const {
intervalPeriods,
selectValuePath,
} = this.props;

const generateOptions = () => {
return intervalPeriods.map(({ value, label }) => (
<option value={value} key={`${selectValuePath}-${value}`}>
{label}
</option>
));
};

render() {
const {
fieldLabel,
selectPlaceholder,
inputValuePath,
selectValuePath,
} = this.props;

return (
<>
<Row className={css.labelRow}>
<Col xs={12}>
<Label className={css.label} required>
<FormattedMessage id={fieldLabel} />
</Label>
</Col>
</Row>
<Row>
<Col xs={2}>
<Field
data-test-period-duration
type="number"
name={inputValuePath}
component={TextField}
forwardRef
inputRef={this.inputRef}
onBlur={this.onInputBlur}
onClearField={this.onInputClear}
parse={this.transformInputValue}
validate={validateDuration}
/>
</Col>
<Col xs={2}>
<FormattedMessage id={selectPlaceholder}>
{placeholder => (
<Field
data-test-period-interval
name={selectValuePath}
component={Select}
placeholder={placeholder}
onChange={this.onSelectChange}
>
{this.generateOptions()}
</Field>
)}
</FormattedMessage>
</Col>
</Row>
</>
);
}
}
return (
<>
<Row className={css.labelRow}>
<Col xs={12}>
<Label className={css.label} required>
<FormattedMessage id={fieldLabel} />
</Label>
</Col>
</Row>
<Row>
<Col xs={2}>
<Field
data-test-period-duration
type="number"
name={inputValuePath}
component={TextField}
forwardRef
inputRef={inputRef}
onBlur={onInputBlur}
onClearField={onInputClear}
parse={transformInputValue}
validate={validateDuration}
/>
</Col>
<Col xs={2}>
<FormattedMessage id={selectPlaceholder}>
{placeholder => (
<Field
data-test-period-interval
name={selectValuePath}
component={Select}
placeholder={placeholder}
onChange={onSelectChange}
>
{generateOptions()}
</Field>
)}
</FormattedMessage>
</Col>
</Row>
</>
);
};

Period.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,
};

export default Period;
20 changes: 20 additions & 0 deletions src/hooks/useCampusDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useQuery } from 'react-query';

export const CAMPUS_DETAILS = 'CAMPUS_DETAILS';

export const useCampusDetails = ({ id, searchParams }) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: CAMPUS_DETAILS });

const { data, isLoading: isCampusLoading } = useQuery({
queryKey: [CAMPUS_DETAILS, namespaceKey, id, searchParams],
queryFn: () => ky.get(`location-units/campuses/${id}`, { searchParams }).json(),
enabled: !!id,
});

return {
campus: data,
isCampusLoading,
};
};
19 changes: 19 additions & 0 deletions src/hooks/useCampuses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useQuery } from 'react-query';

export const CAMPUSES = 'CAMPUSES';

export const useCampuses = ({ searchParams }) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: CAMPUSES });

const { data, isLoading: isCampusesLoading } = useQuery({
queryKey: [CAMPUSES, namespaceKey, searchParams],
queryFn: () => ky.get('location-units/campuses', { searchParams }).json(),
});

return {
campuses: data?.loccamps || [],
isCampusesLoading,
};
};
19 changes: 19 additions & 0 deletions src/hooks/useConfigurations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useQuery } from 'react-query';

export const CONFIGURATIONS = 'CONFIGURATIONS';

export const useConfigurations = ({ searchParams }) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: CONFIGURATIONS });

const { data, isLoading: isConfigsLoading } = useQuery({
queryKey: [CONFIGURATIONS, namespaceKey, searchParams],
queryFn: () => ky.get('configurations/entries', { searchParams }).json(),
});

return {
configs: data?.configs || [],
isConfigsLoading,
};
};
16 changes: 16 additions & 0 deletions src/hooks/useConfigurationsCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMutation } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';

export const useConfigurationsCreate = (options) => {
const ky = useOkapiKy();

const { mutateAsync: createConfiguration, isLoading: isCreatingConfiguration } = useMutation({
mutationFn: ({ data }) => ky.post('configurations/entries', { json: data }).json(),
...options,
});

return {
createConfiguration,
isCreatingConfiguration,
};
};
16 changes: 16 additions & 0 deletions src/hooks/useConfigurationsUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMutation } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';

export const useConfigurationsUpdate = (options) => {
const ky = useOkapiKy();

const { mutateAsync: updateConfiguration, isLoading: isUpdatingConfiguration } = useMutation({
mutationFn: ({ id, data }) => ky.put(`configurations/entries/${id}`, { json: data }).json(),
...options,
});

return {
updateConfiguration,
isUpdatingConfiguration,
};
};
20 changes: 20 additions & 0 deletions src/hooks/useInstitutionDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useQuery } from 'react-query';

export const INSTITUTION_DETAILS = 'INSTITUTION_DETAILS';

export const useInstitutionDetails = ({ id, searchParams }) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: INSTITUTION_DETAILS });

const { data, isLoading: isInstitutionLoading } = useQuery({
queryKey: [INSTITUTION_DETAILS, namespaceKey, id, searchParams],
queryFn: () => ky.get(`location-units/institutions/${id}`, { searchParams }).json(),
enabled: !!id,
});

return {
institution: data,
isInstitutionLoading,
};
};
19 changes: 19 additions & 0 deletions src/hooks/useInstitutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useQuery } from 'react-query';

export const INSTITUTIONS = 'INSTITUTIONS';

export const useInstitutions = ({ searchParams }) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: INSTITUTIONS });

const { data, isLoading: isInstitutionsLoading } = useQuery({
queryKey: [INSTITUTIONS, namespaceKey, searchParams],
queryFn: () => ky.get('location-units/institutions', { searchParams }).json(),
});

return {
institutions: data?.locinsts || [],
isInstitutionsLoading,
};
};
Loading

0 comments on commit 446fc74

Please sign in to comment.