1
- import React , { useEffect , useState } from "react" ;
1
+ import React from "react" ;
2
2
import { useParams } from "react-router-dom" ;
3
3
import {
4
4
Spin ,
@@ -20,8 +20,7 @@ import {
20
20
TeamOutlined ,
21
21
UserOutlined
22
22
} from "@ant-design/icons" ;
23
- import { getEnvironmentById } from "./services/environments.service" ;
24
- import { Environment } from "./types/environment.types" ;
23
+ import { useEnvironmentDetail } from './hooks/useEnvironmentDetail' ;
25
24
26
25
const { Title, Text } = Typography ;
27
26
const { TabPane } = Tabs ;
@@ -33,37 +32,9 @@ const { TabPane } = Tabs;
33
32
const EnvironmentDetail : React . FC = ( ) => {
34
33
// Get environment ID from URL params
35
34
const { environmentId : id } = useParams < { environmentId : string } > ( ) ;
36
- console . log ( id ) ;
37
35
38
- // State for environment data and loading state
39
- const [ environment , setEnvironment ] = useState < Environment | null > ( null ) ;
40
- const [ loading , setLoading ] = useState < boolean > ( true ) ;
41
- const [ error , setError ] = useState < string | null > ( null ) ;
42
-
43
- // Fetch environment data on mount and when ID changes
44
- useEffect ( ( ) => {
45
- fetchEnvironmentData ( ) ;
46
- } , [ id ] ) ;
47
-
48
- // Function to fetch environment data
49
- const fetchEnvironmentData = async ( ) => {
50
- setLoading ( true ) ;
51
- setError ( null ) ;
52
-
53
- try {
54
- const data = await getEnvironmentById ( id ) ;
55
- setEnvironment ( data ) ;
56
- } catch ( err ) {
57
- setError ( err instanceof Error ? err . message : 'Failed to fetch environment details' ) ;
58
- } finally {
59
- setLoading ( false ) ;
60
- }
61
- } ;
62
-
63
- // Handle refresh button click
64
- const handleRefresh = ( ) => {
65
- fetchEnvironmentData ( ) ;
66
- } ;
36
+ // Use the custom hook to handle data fetching and state management
37
+ const { environment, loading, error, refresh } = useEnvironmentDetail ( id ) ;
67
38
68
39
// If loading, show spinner
69
40
if ( loading ) {
@@ -84,7 +55,7 @@ const EnvironmentDetail: React.FC = () => {
84
55
showIcon
85
56
style = { { margin : '24px' } }
86
57
action = {
87
- < Button type = "primary" icon = { < ReloadOutlined /> } onClick = { handleRefresh } >
58
+ < Button type = "primary" icon = { < ReloadOutlined /> } onClick = { refresh } >
88
59
Try Again
89
60
</ Button >
90
61
}
@@ -115,7 +86,7 @@ const EnvironmentDetail: React.FC = () => {
115
86
</ div >
116
87
< Button
117
88
icon = { < ReloadOutlined /> }
118
- onClick = { handleRefresh }
89
+ onClick = { refresh }
119
90
>
120
91
Refresh
121
92
</ Button >
0 commit comments