/* eslint-disable max-len, react/forbid-prop-types */
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import {
FormGroup,
Select, SelectOption, SelectVariant,
} from '@patternfly/react-core';
import LabelIcon from 'foremanReact/components/common/LabelIcon';
import { sprintf, translate as __ } from 'foremanReact/common/I18n';
import { validateAKField, akHasValidValue } from '../RegistrationCommandsPageHelpers';
const ActivationKeys = ({
activationKeys,
selectedKeys,
hostGroupActivationKeys,
hostGroupId,
pluginValues,
onChange,
isLoading,
handleInvalidField,
}) => {
const [isOpen, setIsOpen] = useState(false);
const updatePluginValues = (keys) => {
onChange({ activationKeys: keys });
handleInvalidField('Activation Keys', akHasValidValue(hostGroupId, pluginValues?.activationKeys, hostGroupActivationKeys));
};
const onSelect = (_e, value) => {
if (selectedKeys.find((key => key === value))) {
updatePluginValues(selectedKeys.filter(sk => sk !== value));
} else {
updatePluginValues([...selectedKeys, value]);
}
};
// Validate field when hostgroup is changed (host group may have some keys)
useEffect(() => {
handleInvalidField('Activation Keys', akHasValidValue(hostGroupId, pluginValues?.activationKeys, hostGroupActivationKeys));
}, [handleInvalidField, hostGroupId, hostGroupActivationKeys, pluginValues]);
return (
}
isRequired
>
);
};
ActivationKeys.propTypes = {
activationKeys: PropTypes.array,
selectedKeys: PropTypes.array,
hostGroupActivationKeys: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
hostGroupId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pluginValues: PropTypes.objectOf(PropTypes.shape({})),
onChange: PropTypes.func.isRequired,
handleInvalidField: PropTypes.func.isRequired,
isLoading: PropTypes.bool,
};
ActivationKeys.defaultProps = {
activationKeys: undefined,
selectedKeys: [],
hostGroupActivationKeys: undefined,
hostGroupId: undefined,
pluginValues: {},
isLoading: false,
};
export default ActivationKeys;