mirror of
https://github.com/ansible/awx.git
synced 2026-03-29 06:45:09 -02:30
Prevent multi credential state updates from happening after unmount
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
import 'styled-components/macro';
|
import 'styled-components/macro';
|
||||||
import React, { Fragment, useState, useCallback, useEffect } from 'react';
|
import React, {
|
||||||
|
Fragment,
|
||||||
|
useState,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
} from 'react';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { withRouter } from 'react-router-dom';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
@@ -26,6 +32,7 @@ async function loadCredentials(params, selectedCredentialTypeId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function MultiCredentialsLookup(props) {
|
function MultiCredentialsLookup(props) {
|
||||||
|
const isMounted = useRef(null);
|
||||||
const { value, onChange, onError, history, i18n } = props;
|
const { value, onChange, onError, history, i18n } = props;
|
||||||
const [selectedType, setSelectedType] = useState(null);
|
const [selectedType, setSelectedType] = useState(null);
|
||||||
|
|
||||||
@@ -37,6 +44,9 @@ function MultiCredentialsLookup(props) {
|
|||||||
} = useRequest(
|
} = useRequest(
|
||||||
useCallback(async () => {
|
useCallback(async () => {
|
||||||
const types = await CredentialTypesAPI.loadAllTypes();
|
const types = await CredentialTypesAPI.loadAllTypes();
|
||||||
|
if (!isMounted.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const match = types.find(type => type.kind === 'ssh') || types[0];
|
const match = types.find(type => type.kind === 'ssh') || types[0];
|
||||||
setSelectedType(match);
|
setSelectedType(match);
|
||||||
return types;
|
return types;
|
||||||
@@ -45,7 +55,11 @@ function MultiCredentialsLookup(props) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
isMounted.current = true;
|
||||||
fetchTypes();
|
fetchTypes();
|
||||||
|
return () => {
|
||||||
|
isMounted.current = false;
|
||||||
|
};
|
||||||
}, [fetchTypes]);
|
}, [fetchTypes]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -72,6 +86,10 @@ function MultiCredentialsLookup(props) {
|
|||||||
CredentialsAPI.readOptions(),
|
CredentialsAPI.readOptions(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (!isMounted.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
results.map(result => {
|
results.map(result => {
|
||||||
if (result.kind === 'vault' && result.inputs?.vault_id) {
|
if (result.kind === 'vault' && result.inputs?.vault_id) {
|
||||||
result.label = `${result.name} | ${result.inputs.vault_id}`;
|
result.label = `${result.name} | ${result.inputs.vault_id}`;
|
||||||
@@ -101,7 +119,11 @@ function MultiCredentialsLookup(props) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
isMounted.current = true;
|
||||||
fetchCredentials();
|
fetchCredentials();
|
||||||
|
return () => {
|
||||||
|
isMounted.current = false;
|
||||||
|
};
|
||||||
}, [fetchCredentials]);
|
}, [fetchCredentials]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user