Skip to content

Commit

Permalink
Fixes #36295 - Auto activation key selection if there's only 1
Browse files Browse the repository at this point in the history
  • Loading branch information
girijaasoni committed May 24, 2023
1 parent f3cd18a commit ecc90cb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
// Activation Keys helpers

export const validateAKField = (hostGroupId, userKeys, hgKeys) => {
export const validateAKField = (
hasInteraction,
hostGroupId,
userKeys,
hgKeys,
) => {
if (hostGroupId === '') {
return (userKeys?.length > 0 ? 'success' : 'error');
return userKeys?.length > 0 ? 'success' : 'error';
}

if (userKeys === undefined && hgKeys === undefined) {
return ('default');
if (!hasInteraction) {
return 'default';
}

return ((userKeys?.length > 0 || hgKeys?.length > 0) ? 'success' : 'error');
return userKeys?.length > 0 || hgKeys?.length > 0 ? 'success' : 'error';
};

export const akHasValidValue = (hostGroupId, userKeys, hgKeys) => {
if (hostGroupId === '') {
return (userKeys?.length > 0);
return userKeys?.length > 0;
}

return (hgKeys?.length > 0 || userKeys?.length > 0);
return hgKeys?.length > 0 || userKeys?.length > 0;
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@

/* eslint-disable max-len, react/forbid-prop-types */
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import {
FormGroup,
Select, SelectOption, SelectVariant,
Select,
SelectOption,
SelectVariant,
} from '@patternfly/react-core';

import LabelIcon from 'foremanReact/components/common/LabelIcon';
import { ExclamationCircleIcon } from '@patternfly/react-icons';
import { sprintf, translate as __ } from 'foremanReact/common/I18n';

import { validateAKField, akHasValidValue } from '../RegistrationCommandsPageHelpers';
import {
validateAKField,
akHasValidValue,
} from '../RegistrationCommandsPageHelpers';

const ActivationKeys = ({
activationKeys,
Expand All @@ -23,14 +28,22 @@ const ActivationKeys = ({
handleInvalidField,
}) => {
const [isOpen, setIsOpen] = useState(false);
const [hasInteraction, setHasInteraction] = useState(false);

const updatePluginValues = (keys) => {
onChange({ activationKeys: keys });
handleInvalidField('Activation Keys', akHasValidValue(hostGroupId, pluginValues?.activationKeys, hostGroupActivationKeys));
handleInvalidField(
'Activation Keys',
akHasValidValue(
hostGroupId,
pluginValues?.activationKeys,
hostGroupActivationKeys,
),
);
};

const onSelect = (_e, value) => {
if (selectedKeys.find((key => key === value))) {
if (selectedKeys.find(key => key === value)) {
updatePluginValues(selectedKeys.filter(sk => sk !== value));
} else {
updatePluginValues([...selectedKeys, value]);
Expand All @@ -39,47 +52,95 @@ const ActivationKeys = ({

// Validate field when hostgroup is changed (host group may have some keys)
useEffect(() => {
handleInvalidField('Activation Keys', akHasValidValue(hostGroupId, pluginValues?.activationKeys, hostGroupActivationKeys));
handleInvalidField(
'Activation Keys',
akHasValidValue(
hostGroupId,
pluginValues?.activationKeys,
hostGroupActivationKeys,
),
);
}, [handleInvalidField, hostGroupId, hostGroupActivationKeys, pluginValues]);

useEffect(() => {
if (activationKeys?.length === 1) {
updatePluginValues([activationKeys[0].name]);
}
if (activationKeys?.length === 0) {
setHasInteraction(true);
}
}, [activationKeys?.length]);

return (
<FormGroup
label={__('Activation Keys')}
fieldId="reg_katello_ak"
helperText={hostGroupActivationKeys && sprintf('From host group: %s', hostGroupActivationKeys)}
helperTextInvalid={activationKeys?.length === 0 ? <a href="/activation_keys/new">{__('Create new activation key')}</a> : __('No Activation Keys selected')}
validated={validateAKField(hostGroupId, pluginValues?.activationKeys, hostGroupActivationKeys)}
labelIcon={<LabelIcon text={__('Activation key(s) for Subscription Manager.')} />}
helperText={
hostGroupActivationKeys &&
sprintf('From host group: %s', hostGroupActivationKeys)
}
helperTextInvalid={
activationKeys?.length === 0 ? (
<a href="/activation_keys/new">{__('Create new activation key')}</a>
) : (
__('No Activation Keys selected')
)
}
helperTextInvalidIcon={<ExclamationCircleIcon />}
labelIcon={
<LabelIcon text={__('Activation key(s) for Subscription Manager.')} />
}
validated={validateAKField(
hasInteraction,
hostGroupId,
pluginValues?.activationKeys,
hostGroupActivationKeys,
)}
isRequired
>
<Select
selections={selectedKeys}
variant={SelectVariant.typeaheadMulti}
onToggle={() => setIsOpen(!isOpen)}
onToggle={() => {
setHasInteraction(true);
setIsOpen(!isOpen);
}}
onSelect={onSelect}
onClear={() => updatePluginValues([])}
isOpen={isOpen}
validated={validateAKField(
hasInteraction,
hostGroupId,
pluginValues?.activationKeys,
hostGroupActivationKeys,
)}
id="reg_katello_ak"
className="without_select2"
isDisabled={isLoading || activationKeys?.length === 0}
placeholderText={activationKeys?.length === 0 ? __('No Activation keys to select') : ''}
placeholderText={
activationKeys?.length === 0 ? __('No Activation keys to select') : ''
}
>
{activationKeys && activationKeys.map(ack => (
<SelectOption
key={ack.name}
value={ack.name}
description={(ack?.lce ? ack.lce : __('No environment'))}
/>
))}
{activationKeys &&
activationKeys.map(ack => (
<SelectOption
key={ack.name}
value={ack.name}
description={ack?.lce ? ack.lce : __('No environment')}
/>
))}
</Select>
</FormGroup>);
</FormGroup>
);
};


ActivationKeys.propTypes = {
activationKeys: PropTypes.array,
selectedKeys: PropTypes.array,
hostGroupActivationKeys: PropTypes.oneOfType([PropTypes.string, 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const RegistrationActivationKeys = ({
<ActivationKeys
activationKeys={pluginData?.activationKeys}
organizationId={organizationId}
selectedKeys={(pluginValues?.activationKeys || [])}
selectedKeys={pluginValues?.activationKeys || []}
hostGroupActivationKeys={pluginData?.hostGroupActivationKeys}
hostGroupId={hostGroupId}
pluginValues={pluginValues}
Expand Down

0 comments on commit ecc90cb

Please sign in to comment.