Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Manager] Fix enrollment key selection #73062

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
),
}}
/>
) : !isLoading && totalAgents === 0 ? (
) : (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix a blinking on the agent list page when there is no agents

emptyPrompt
) : undefined
)
}
items={totalAgents ? agents : []}
itemId="id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,71 +39,105 @@ export const EnrollmentStepAgentConfig: React.FC<Props> = (props) => {
enrollmentAPIKeyId?: string;
}>({});

useEffect(() => {
if (agentConfigs && agentConfigs.length && !selectedState.agentConfigId) {
setSelectedState({
...selectedState,
agentConfigId: agentConfigs[0].id,
});
}
}, [agentConfigs, selectedState]);
useEffect(
function triggerOnConfigChangeEffect() {
if (onConfigChange && selectedState.agentConfigId) {
onConfigChange(selectedState.agentConfigId);
}
},
[selectedState.agentConfigId, onConfigChange]
);

useEffect(() => {
if (onConfigChange && selectedState.agentConfigId) {
onConfigChange(selectedState.agentConfigId);
}
}, [selectedState.agentConfigId, onConfigChange]);
useEffect(
function triggerOnKeyChangeEffect() {
if (!withKeySelection || !onKeyChange) {
return;
}

useEffect(() => {
if (!withKeySelection) {
return;
}
if (!selectedState.agentConfigId) {
setEnrollmentAPIKeys([]);
return;
}
if (selectedState.enrollmentAPIKeyId) {
onKeyChange(selectedState.enrollmentAPIKeyId);
}
},
[withKeySelection, onKeyChange, selectedState.enrollmentAPIKeyId]
);

async function fetchEnrollmentAPIKeys() {
try {
const res = await sendGetEnrollmentAPIKeys({
page: 1,
perPage: 10000,
});
if (res.error) {
throw res.error;
useEffect(
function useDefaultConfigEffect() {
if (agentConfigs && agentConfigs.length && !selectedState.agentConfigId) {
const defaultConfig = agentConfigs.find((config) => config.is_default);
if (defaultConfig) {
setSelectedState({
...selectedState,
agentConfigId: defaultConfig.id,
});
}
}
},
[agentConfigs, selectedState]
);

if (!res.data) {
throw new Error('No data while fetching enrollment API keys');
useEffect(
function useEnrollmentKeysForConfigEffect() {
if (!withKeySelection) {
return;
}
if (!selectedState.agentConfigId) {
setEnrollmentAPIKeys([]);
return;
}

async function fetchEnrollmentAPIKeys() {
try {
const res = await sendGetEnrollmentAPIKeys({
page: 1,
perPage: 10000,
});
if (res.error) {
throw res.error;
}

if (!res.data) {
throw new Error('No data while fetching enrollment API keys');
}

setEnrollmentAPIKeys(
res.data.list.filter((key) => key.config_id === selectedState.agentConfigId)
);
} catch (error) {
notifications.toasts.addError(error, {
title: 'Error',
});
}
}
fetchEnrollmentAPIKeys();
},
[withKeySelection, selectedState.agentConfigId, notifications.toasts]
);

setEnrollmentAPIKeys(
res.data.list.filter((key) => key.config_id === selectedState.agentConfigId)
);
} catch (error) {
notifications.toasts.addError(error, {
title: 'Error',
useEffect(
function useDefaultEnrollmentKeyForConfigEffect() {
if (!withKeySelection) {
return;
}
if (
!selectedState.enrollmentAPIKeyId &&
enrollmentAPIKeys.length > 0 &&
enrollmentAPIKeys[0].config_id === selectedState.agentConfigId
) {
const enrollmentAPIKeyId = enrollmentAPIKeys[0].id;
setSelectedState({
agentConfigId: selectedState.agentConfigId,
enrollmentAPIKeyId,
});
}
}
fetchEnrollmentAPIKeys();
}, [withKeySelection, selectedState.agentConfigId, notifications.toasts]);

// Select first API key when config change
React.useEffect(() => {
if (!withKeySelection || !onKeyChange) {
return;
}
if (!selectedState.enrollmentAPIKeyId && enrollmentAPIKeys.length > 0) {
const enrollmentAPIKeyId = enrollmentAPIKeys[0].id;
setSelectedState({
agentConfigId: selectedState.agentConfigId,
enrollmentAPIKeyId,
});
onKeyChange(enrollmentAPIKeyId);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [enrollmentAPIKeys, selectedState.enrollmentAPIKeyId, selectedState.agentConfigId]);
},
[
withKeySelection,
enrollmentAPIKeys,
selectedState.enrollmentAPIKeyId,
selectedState.agentConfigId,
]
);

return (
<>
Expand Down Expand Up @@ -174,7 +208,6 @@ export const EnrollmentStepAgentConfig: React.FC<Props> = (props) => {
...selectedState,
enrollmentAPIKeyId: e.target.value,
});
onKeyChange(e.target.value);
}}
/>
</>
Expand Down