Skip to content

Commit

Permalink
Fixes #37572 - Extract logic and simplify initial AK selection (Katel…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz authored Aug 28, 2024
1 parent e2c7ad1 commit 22ed429
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ const ActivationKeys = ({
);
}, [handleInvalidField, hostGroupId, hostGroupActivationKeys, pluginValues]);

useEffect(() => {
if (activationKeys?.length === 1) {
onChange({ activationKeys: [activationKeys[0].name] });
}
}, [activationKeys, onChange]);

return (
<FormGroup
onFocus={() => setHasInteraction(true)}
Expand Down
15 changes: 15 additions & 0 deletions webpack/components/extensions/RegistrationCommands/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const determineInitialAKSelection = (activationKeys, initialAKSelection) => {
// If we've received the initialAKSelection from the URL, and it's a valid activation key, use it
if (initialAKSelection &&
(activationKeys ?? []).some(ak => ak.name === initialAKSelection)) {
return { activationKeys: initialAKSelection.split(',') };
}
// If there's only one activation key, use it
if (activationKeys?.length === 1) {
return { activationKeys: [activationKeys[0].name] };
}
// Otherwise, don't select any activation keys
return { activationKeys: [] };
};

export default determineInitialAKSelection;
12 changes: 3 additions & 9 deletions webpack/components/extensions/RegistrationCommands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { noop } from 'foremanReact/common/helpers';
import { useUrlParams } from 'foremanReact/components/PF4/TableIndexPage/Table/TableHooks';
import { determineInitialAKSelection } from './helpers';

import ActivationKeys from './fields/ActivationKeys';
import IgnoreSubmanErrors from './fields/IgnoreSubmanErrors';
Expand Down Expand Up @@ -51,15 +52,8 @@ export const RegistrationActivationKeys = ({
}) => {
const { initialAKSelection } = useUrlParams();
useEffect(() => {
onChange({ activationKeys: [] });
}, [onChange, organizationId, hostGroupId]);

useEffect(() => {
if (initialAKSelection &&
(pluginData?.activationKeys ?? []).some(ak => ak.name === initialAKSelection)) {
onChange({ activationKeys: initialAKSelection.split(',') });
}
}, [initialAKSelection, onChange, pluginData?.activationKeys]);
onChange(determineInitialAKSelection(pluginData?.activationKeys, initialAKSelection));
}, [initialAKSelection, onChange, pluginData?.activationKeys, organizationId, hostGroupId]);

return (
<ActivationKeys
Expand Down

0 comments on commit 22ed429

Please sign in to comment.