mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 18:07:36 -02:30
add useEndpoint hook
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useEffect, useRef } from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { Link, useHistory } from 'react-router-dom';
|
import { Link, useHistory } from 'react-router-dom';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
@@ -11,37 +11,19 @@ import DeleteButton from '@components/DeleteButton';
|
|||||||
import ContentError from '@components/ContentError';
|
import ContentError from '@components/ContentError';
|
||||||
import ContentLoading from '@components/ContentLoading';
|
import ContentLoading from '@components/ContentLoading';
|
||||||
import { InventoriesAPI } from '@api';
|
import { InventoriesAPI } from '@api';
|
||||||
|
import useEndpoint from './useEndpoint';
|
||||||
import { Inventory } from '../../../types';
|
import { Inventory } from '../../../types';
|
||||||
|
|
||||||
function InventoryDetail({ inventory, i18n }) {
|
function InventoryDetail({ inventory, i18n }) {
|
||||||
const [instanceGroups, setInstanceGroups] = useState([]);
|
|
||||||
const [hasContentLoading, setHasContentLoading] = useState(true);
|
|
||||||
const [contentError, setContentError] = useState(null);
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const isMounted = useRef(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
const { results: instanceGroups, isLoading, error } = useEndpoint(
|
||||||
isMounted.current = true;
|
useCallback(async () => {
|
||||||
(async () => {
|
const { data } = await InventoriesAPI.readInstanceGroups(inventory.id);
|
||||||
setHasContentLoading(true);
|
return data.results;
|
||||||
try {
|
}, [inventory.id]),
|
||||||
const { data } = await InventoriesAPI.readInstanceGroups(inventory.id);
|
inventory.id
|
||||||
if (!isMounted.current) {
|
);
|
||||||
return;
|
|
||||||
}
|
|
||||||
setInstanceGroups(data.results);
|
|
||||||
} catch (err) {
|
|
||||||
setContentError(err);
|
|
||||||
} finally {
|
|
||||||
if (isMounted.current) {
|
|
||||||
setHasContentLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
return () => {
|
|
||||||
isMounted.current = false;
|
|
||||||
};
|
|
||||||
}, [inventory.id]);
|
|
||||||
|
|
||||||
const deleteInventory = async () => {
|
const deleteInventory = async () => {
|
||||||
await InventoriesAPI.destroy(inventory.id);
|
await InventoriesAPI.destroy(inventory.id);
|
||||||
@@ -53,12 +35,12 @@ function InventoryDetail({ inventory, i18n }) {
|
|||||||
user_capabilities: userCapabilities,
|
user_capabilities: userCapabilities,
|
||||||
} = inventory.summary_fields;
|
} = inventory.summary_fields;
|
||||||
|
|
||||||
if (hasContentLoading) {
|
if (isLoading) {
|
||||||
return <ContentLoading />;
|
return <ContentLoading />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentError) {
|
if (error) {
|
||||||
return <ContentError error={contentError} />;
|
return <ContentError error={error} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { useEffect, useState, useRef } from 'react';
|
||||||
|
|
||||||
|
export default function useEndpoint(fetch) {
|
||||||
|
const [results, setResults] = useState([]);
|
||||||
|
const [error, setError] = useState(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const isMounted = useRef(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
isMounted.current = true;
|
||||||
|
(async () => {
|
||||||
|
// Do we want this set here or not? Can result in extra
|
||||||
|
// unmounting/re-mounting of child components
|
||||||
|
setIsLoading(true);
|
||||||
|
try {
|
||||||
|
const fetchedResults = await fetch();
|
||||||
|
if (isMounted.current) {
|
||||||
|
setResults(fetchedResults);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (isMounted.current) {
|
||||||
|
setError(err);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (isMounted.current) {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
return () => {
|
||||||
|
isMounted.current = false;
|
||||||
|
};
|
||||||
|
}, [fetch]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
results,
|
||||||
|
isLoading,
|
||||||
|
error,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user