Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Aug 6, 2024
1 parent 548cd3f commit b3e77c8
Showing 1 changed file with 65 additions and 53 deletions.
118 changes: 65 additions & 53 deletions src/settings/LocationLocations/LocationForm/ServicePointsFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import { Field } from 'react-final-form';
import { FieldArray } from 'react-final-form-arrays';
import PropTypes from 'prop-types';
import { sortBy, cloneDeep, findIndex } from 'lodash';

import {
Icon,
Layout,
RadioButton,
RepeatableField,
Select,
} from '@folio/stripes/components';

import css from './ServicePointsFields.css';


const omitUsedOptions = (list, usedValues, key, id) => {
const unUsedValues = cloneDeep(list);
if (usedValues) {
Expand All @@ -33,33 +30,50 @@ const omitUsedOptions = (list, usedValues, key, id) => {
return unUsedValues;
};

const ServicePointsFields = ({ servicePoints, changePrimary, formValues }) => {
const list = omitUsedOptions(servicePoints, formValues.servicePointIds, 'selectSP');
class ServicePointsFields extends React.Component {
static propTypes = {
servicePoints: PropTypes.arrayOf(PropTypes.object),
changePrimary: PropTypes.func.isRequired,
formValues: PropTypes.object.isRequired,
};

constructor(props) {
super(props);
this.singlePrimary = this.singlePrimary.bind(this);
this.renderFields = this.renderFields.bind(this);
this.radioButtonComp = this.radioButtonComp.bind(this);
this.list = {};
}

singlePrimary(id) {
const { changePrimary, formValues } = this.props;

const singlePrimary = (id) => {
formValues.servicePointIds.forEach((a, i) => {
if (i === id) {
changePrimary(i, true);
} else {
changePrimary(i, false);
}
});
};
}

const radioButtonComp = ({ input, fieldIndex }) => {
radioButtonComp({ input, ...props }) {
return (
<RadioButton
onChange={() => { singlePrimary(fieldIndex); }}
onChange={() => { this.singlePrimary(props.fieldIndex); }}
checked={input.value}
name={input.name}
aria-label={`servicePoint use as primary ${fieldIndex}`}
aria-label={`servicePoint use as primary ${props.fieldIndex}`}
/>
);
};
}

renderFields(field, index) {
const { formValues } = this.props;

const renderFields = (field, index) => {
const availableOptions = omitUsedOptions(servicePoints, formValues.servicePointIds, 'selectSP', index);
const sortedList = sortBy(availableOptions, ['label']);
this.list = omitUsedOptions(this.props.servicePoints, formValues.servicePointIds, 'selectSP', index);

const sortedList = sortBy(this.list, ['label']);
const options = [{ label: 'Select service point', value: '' }, ...sortedList];

return (
Expand All @@ -81,54 +95,52 @@ const ServicePointsFields = ({ servicePoints, changePrimary, formValues }) => {
</Layout>
<Layout className={`display-flex ${css.radioButtonLayout}`}>
<Field
component={radioButtonComp}
component={this.radioButtonComp}
fieldIndex={index}
name={`${field}.primary`}
/>
</Layout>
</Layout>
);
};

// Make the last existing service point to be the primary one
if (formValues.servicePointIds && formValues.servicePointIds.length === 1 && !formValues.servicePointIds[0].primary) {
singlePrimary(0);
}

const legend = (
<Layout className="display-flex">
<Layout className={`${css.label} ${css.servicePointsLabel}`}>
<FormattedMessage id="ui-tenant-settings.settings.location.locations.servicePoints" />
<span className={css.asterisk}>*</span>
</Layout>
<Layout className={`${css.label} ${css.primaryLabel}`}>
<FormattedMessage id="ui-tenant-settings.settings.location.locations.primary" />
</Layout>
</Layout>
);
render() {
const { formValues } = this.props;

return (
<>
<FieldArray
addLabel={
Object.keys(list).length > 1 ?
<Icon icon="plus-sign">Add service point</Icon> :
''
}
legend={legend}
emptyMessage={<span className={css.emptyMessage}>Location must have at least one service point</span>}
component={RepeatableField}
name="servicePointIds"
renderField={renderFields}
/>
</>
);
};
// make the last existing service point to be the primary one
if (formValues.servicePointIds && formValues.servicePointIds.length === 1 && !formValues.servicePointIds[0].primary) {
this.singlePrimary(0);
}

ServicePointsFields.propTypes = {
servicePoints: PropTypes.arrayOf(PropTypes.object),
changePrimary: PropTypes.func.isRequired,
formValues: PropTypes.object.isRequired,
};
const legend = (
<Layout className="display-flex">
<Layout className={`${css.label} ${css.servicePointsLabel}`}>
<FormattedMessage id="ui-tenant-settings.settings.location.locations.servicePoints" />
<span className={css.asterisk}>*</span>
</Layout>
<Layout className={`${css.label} ${css.primaryLabel}`}>
<FormattedMessage id="ui-tenant-settings.settings.location.locations.primary" />
</Layout>
</Layout>
);

return (
<>
<FieldArray
addLabel={
Object.keys(this.list).length > 1 ?
<Icon icon="plus-sign">Add service point</Icon> :
''
}
legend={legend}
emptyMessage={<span className={css.emptyMessage}>Location must have at least one service point</span>}
component={RepeatableField}
name="servicePointIds"
renderField={this.renderFields}
/>
</>
);
}
}

export default ServicePointsFields;

0 comments on commit b3e77c8

Please sign in to comment.