From e6e766a3620795c4c91d5be8b05d15a59c936603 Mon Sep 17 00:00:00 2001 From: girijaasoni <115526987+girijaasoni@users.noreply.github.com> Date: Thu, 24 Aug 2023 01:25:26 +0530 Subject: [PATCH] Fixes #36295 - Auto activation key selection if there's only one (#10518) --- .../RegistrationCommandsPageHelpers.js | 20 ++-- .../__snapshots__/ActivationKeys.test.js.snap | 10 +- .../fields/ActivationKeys.js | 109 ++++++++++++++---- .../extensions/RegistrationCommands/index.js | 2 +- 4 files changed, 110 insertions(+), 31 deletions(-) diff --git a/webpack/components/extensions/RegistrationCommands/RegistrationCommandsPageHelpers.js b/webpack/components/extensions/RegistrationCommands/RegistrationCommandsPageHelpers.js index 276e27dace1..8de4d59db7d 100644 --- a/webpack/components/extensions/RegistrationCommands/RegistrationCommandsPageHelpers.js +++ b/webpack/components/extensions/RegistrationCommands/RegistrationCommandsPageHelpers.js @@ -1,21 +1,27 @@ // Activation Keys helpers -export const validateAKField = (hostGroupId, userKeys, hgKeys) => { +export const validateAKField = ( + hasInteraction, + hostGroupId, + activationKeys, + 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 && activationKeys?.length > 0) { + 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; }; diff --git a/webpack/components/extensions/RegistrationCommands/__tests__/__snapshots__/ActivationKeys.test.js.snap b/webpack/components/extensions/RegistrationCommands/__tests__/__snapshots__/ActivationKeys.test.js.snap index b9c3c76bd2e..a85d2d625a3 100644 --- a/webpack/components/extensions/RegistrationCommands/__tests__/__snapshots__/ActivationKeys.test.js.snap +++ b/webpack/components/extensions/RegistrationCommands/__tests__/__snapshots__/ActivationKeys.test.js.snap @@ -11,13 +11,21 @@ exports[`ActivationKeys renders 1`] = ` Create new activation key } + helperTextInvalidIcon={ + + } isRequired={true} label="Activation Keys" labelIcon={ } + onFocus={[Function]} validated="error" > setIsOpen(!isOpen)} + onToggle={() => { + setHasInteraction(true); + setIsOpen(!isOpen); + }} onSelect={onSelect} onClear={() => updatePluginValues([])} isOpen={isOpen} + validated={ + activationKeys?.length > 0 + ? validateAKField( + hasInteraction, + hostGroupId, + activationKeys, + pluginValues?.activationKeys, + hostGroupActivationKeys, + ) + : 'default' + } id="activation_keys_field" 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 => ( - - ))} + {activationKeys && + activationKeys.map(ack => ( + + ))} - ); + + ); }; - 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, diff --git a/webpack/components/extensions/RegistrationCommands/index.js b/webpack/components/extensions/RegistrationCommands/index.js index a09aa790730..46ad283d21d 100644 --- a/webpack/components/extensions/RegistrationCommands/index.js +++ b/webpack/components/extensions/RegistrationCommands/index.js @@ -80,7 +80,7 @@ export const RegistrationActivationKeys = ({