mirror of
https://github.com/ansible/awx.git
synced 2026-05-06 00:47:37 -02:30
Rename onLoading/onDoneLoading props to onCopyStart and onCopyFinish. Wrap the functions being passed in as those props in useCallback to keep them hooks safe.
This commit is contained in:
@@ -9,17 +9,24 @@ import useRequest, { useDismissableError } from '../../util/useRequest';
|
|||||||
import AlertModal from '../AlertModal';
|
import AlertModal from '../AlertModal';
|
||||||
import ErrorDetail from '../ErrorDetail';
|
import ErrorDetail from '../ErrorDetail';
|
||||||
|
|
||||||
function CopyButton({ i18n, copyItem, onLoading, onDoneLoading, helperText }) {
|
function CopyButton({
|
||||||
|
i18n,
|
||||||
|
copyItem,
|
||||||
|
isDisabled,
|
||||||
|
onCopyStart,
|
||||||
|
onCopyFinish,
|
||||||
|
helperText,
|
||||||
|
}) {
|
||||||
const { isLoading, error: copyError, request: copyItemToAPI } = useRequest(
|
const { isLoading, error: copyError, request: copyItemToAPI } = useRequest(
|
||||||
copyItem
|
copyItem
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return onLoading();
|
return onCopyStart();
|
||||||
}
|
}
|
||||||
return onDoneLoading();
|
return onCopyFinish();
|
||||||
}, [isLoading, onLoading, onDoneLoading]);
|
}, [isLoading, onCopyStart, onCopyFinish]);
|
||||||
|
|
||||||
const { error, dismissError } = useDismissableError(copyError);
|
const { error, dismissError } = useDismissableError(copyError);
|
||||||
|
|
||||||
@@ -27,6 +34,7 @@ function CopyButton({ i18n, copyItem, onLoading, onDoneLoading, helperText }) {
|
|||||||
<>
|
<>
|
||||||
<Tooltip content={helperText.tooltip} position="top">
|
<Tooltip content={helperText.tooltip} position="top">
|
||||||
<Button
|
<Button
|
||||||
|
isDisabled={isDisabled}
|
||||||
aria-label={i18n._(t`Copy`)}
|
aria-label={i18n._(t`Copy`)}
|
||||||
variant="plain"
|
variant="plain"
|
||||||
onClick={copyItemToAPI}
|
onClick={copyItemToAPI}
|
||||||
@@ -50,11 +58,17 @@ function CopyButton({ i18n, copyItem, onLoading, onDoneLoading, helperText }) {
|
|||||||
|
|
||||||
CopyButton.propTypes = {
|
CopyButton.propTypes = {
|
||||||
copyItem: PropTypes.func.isRequired,
|
copyItem: PropTypes.func.isRequired,
|
||||||
onLoading: PropTypes.func.isRequired,
|
onCopyStart: PropTypes.func.isRequired,
|
||||||
onDoneLoading: PropTypes.func.isRequired,
|
onCopyFinish: PropTypes.func.isRequired,
|
||||||
helperText: PropTypes.shape({
|
helperText: PropTypes.shape({
|
||||||
tooltip: PropTypes.string.isRequired,
|
tooltip: PropTypes.string.isRequired,
|
||||||
errorMessage: PropTypes.string.isRequired,
|
errorMessage: PropTypes.string.isRequired,
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
|
isDisabled: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CopyButton.defaultProps = {
|
||||||
|
isDisabled: false,
|
||||||
|
};
|
||||||
|
|
||||||
export default withI18n()(CopyButton);
|
export default withI18n()(CopyButton);
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ describe('<CopyButton/>', () => {
|
|||||||
test('shold mount properly', () => {
|
test('shold mount properly', () => {
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
<CopyButton
|
<CopyButton
|
||||||
onLoading={() => {}}
|
onCopyStart={() => {}}
|
||||||
onDoneLoading={() => {}}
|
onCopyFinish={() => {}}
|
||||||
copyItem={() => {}}
|
copyItem={() => {}}
|
||||||
helperText={{
|
helperText={{
|
||||||
tooltip: `Copy Template`,
|
tooltip: `Copy Template`,
|
||||||
@@ -22,8 +22,8 @@ describe('<CopyButton/>', () => {
|
|||||||
test('should render proper tooltip', () => {
|
test('should render proper tooltip', () => {
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
<CopyButton
|
<CopyButton
|
||||||
onLoading={() => {}}
|
onCopyStart={() => {}}
|
||||||
onDoneLoading={() => {}}
|
onCopyFinish={() => {}}
|
||||||
copyItem={() => {}}
|
copyItem={() => {}}
|
||||||
helperText={{
|
helperText={{
|
||||||
tooltip: `Copy Template`,
|
tooltip: `Copy Template`,
|
||||||
|
|||||||
@@ -48,6 +48,14 @@ function CredentialListItem({
|
|||||||
await fetchCredentials();
|
await fetchCredentials();
|
||||||
}, [credential.id, credential.name, fetchCredentials]);
|
}, [credential.id, credential.name, fetchCredentials]);
|
||||||
|
|
||||||
|
const handleCopyStart = useCallback(() => {
|
||||||
|
setIsDisabled(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleCopyFinish = useCallback(() => {
|
||||||
|
setIsDisabled(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataListItem
|
<DataListItem
|
||||||
key={credential.id}
|
key={credential.id}
|
||||||
@@ -95,8 +103,8 @@ function CredentialListItem({
|
|||||||
{credential.summary_fields.user_capabilities.copy && (
|
{credential.summary_fields.user_capabilities.copy && (
|
||||||
<CopyButton
|
<CopyButton
|
||||||
isDisabled={isDisabled}
|
isDisabled={isDisabled}
|
||||||
onLoading={() => setIsDisabled(true)}
|
onCopyStart={handleCopyStart}
|
||||||
onDoneLoading={() => setIsDisabled(false)}
|
onCopyFinish={handleCopyFinish}
|
||||||
copyItem={copyCredential}
|
copyItem={copyCredential}
|
||||||
helperText={{
|
helperText={{
|
||||||
tooltip: i18n._(t`Copy Credential`),
|
tooltip: i18n._(t`Copy Credential`),
|
||||||
|
|||||||
@@ -52,6 +52,14 @@ function InventoryListItem({
|
|||||||
await fetchInventories();
|
await fetchInventories();
|
||||||
}, [inventory.id, inventory.name, fetchInventories]);
|
}, [inventory.id, inventory.name, fetchInventories]);
|
||||||
|
|
||||||
|
const handleCopyStart = useCallback(() => {
|
||||||
|
setIsDisabled(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleCopyFinish = useCallback(() => {
|
||||||
|
setIsDisabled(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const labelId = `check-action-${inventory.id}`;
|
const labelId = `check-action-${inventory.id}`;
|
||||||
|
|
||||||
let syncStatus = 'disabled';
|
let syncStatus = 'disabled';
|
||||||
@@ -128,8 +136,8 @@ function InventoryListItem({
|
|||||||
<CopyButton
|
<CopyButton
|
||||||
copyItem={copyInventory}
|
copyItem={copyInventory}
|
||||||
isDisabled={isDisabled}
|
isDisabled={isDisabled}
|
||||||
onLoading={() => setIsDisabled(true)}
|
onCopyStart={handleCopyStart}
|
||||||
onDoneLoading={() => setIsDisabled(false)}
|
onCopyFinish={handleCopyFinish}
|
||||||
helperText={{
|
helperText={{
|
||||||
tooltip: i18n._(t`Copy Inventory`),
|
tooltip: i18n._(t`Copy Inventory`),
|
||||||
errorMessage: i18n._(t`Failed to copy inventory.`),
|
errorMessage: i18n._(t`Failed to copy inventory.`),
|
||||||
|
|||||||
@@ -79,6 +79,14 @@ function ProjectListItem({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleCopyStart = useCallback(() => {
|
||||||
|
setIsDisabled(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleCopyFinish = useCallback(() => {
|
||||||
|
setIsDisabled(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const labelId = `check-action-${project.id}`;
|
const labelId = `check-action-${project.id}`;
|
||||||
return (
|
return (
|
||||||
<DataListItem
|
<DataListItem
|
||||||
@@ -182,8 +190,8 @@ function ProjectListItem({
|
|||||||
<CopyButton
|
<CopyButton
|
||||||
copyItem={copyProject}
|
copyItem={copyProject}
|
||||||
isDisabled={isDisabled}
|
isDisabled={isDisabled}
|
||||||
onLoading={() => setIsDisabled(true)}
|
onCopyStart={handleCopyStart}
|
||||||
onDoneLoading={() => setIsDisabled(false)}
|
onCopyFinish={handleCopyFinish}
|
||||||
helperText={{
|
helperText={{
|
||||||
tooltip: i18n._(t`Copy Project`),
|
tooltip: i18n._(t`Copy Project`),
|
||||||
errorMessage: i18n._(t`Failed to copy project.`),
|
errorMessage: i18n._(t`Failed to copy project.`),
|
||||||
|
|||||||
@@ -60,6 +60,14 @@ function TemplateListItem({
|
|||||||
await fetchTemplates();
|
await fetchTemplates();
|
||||||
}, [fetchTemplates, template.id, template.name, template.type]);
|
}, [fetchTemplates, template.id, template.name, template.type]);
|
||||||
|
|
||||||
|
const handleCopyStart = useCallback(() => {
|
||||||
|
setIsDisabled(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleCopyFinish = useCallback(() => {
|
||||||
|
setIsDisabled(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const missingResourceIcon =
|
const missingResourceIcon =
|
||||||
template.type === 'job_template' &&
|
template.type === 'job_template' &&
|
||||||
(!template.summary_fields.project ||
|
(!template.summary_fields.project ||
|
||||||
@@ -157,8 +165,8 @@ function TemplateListItem({
|
|||||||
errorMessage: i18n._(t`Failed to copy template.`),
|
errorMessage: i18n._(t`Failed to copy template.`),
|
||||||
}}
|
}}
|
||||||
isDisabled={isDisabled}
|
isDisabled={isDisabled}
|
||||||
onLoading={() => setIsDisabled(true)}
|
onCopyStart={handleCopyStart}
|
||||||
onDoneLoading={() => setIsDisabled(false)}
|
onCopyFinish={handleCopyFinish}
|
||||||
copyItem={copyTemplate}
|
copyItem={copyTemplate}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user