Skip to content

Commit 2e88e9f

Browse files
committed
Create useEnvironmentDetail Hook and add in Detail Page
1 parent 4be0dad commit 2e88e9f

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

client/packages/lowcoder/src/pages/setting/environments/EnvironmentDetail.tsx

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from "react";
1+
import React from "react";
22
import { useParams } from "react-router-dom";
33
import {
44
Spin,
@@ -20,8 +20,7 @@ import {
2020
TeamOutlined,
2121
UserOutlined
2222
} from "@ant-design/icons";
23-
import { getEnvironmentById } from "./services/environments.service";
24-
import { Environment } from "./types/environment.types";
23+
import { useEnvironmentDetail } from './hooks/useEnvironmentDetail';
2524

2625
const { Title, Text } = Typography;
2726
const { TabPane } = Tabs;
@@ -33,37 +32,9 @@ const { TabPane } = Tabs;
3332
const EnvironmentDetail: React.FC = () => {
3433
// Get environment ID from URL params
3534
const { environmentId: id } = useParams<{ environmentId: string }>();
36-
console.log(id);
3735

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);
6738

6839
// If loading, show spinner
6940
if (loading) {
@@ -84,7 +55,7 @@ const EnvironmentDetail: React.FC = () => {
8455
showIcon
8556
style={{ margin: '24px' }}
8657
action={
87-
<Button type="primary" icon={<ReloadOutlined />} onClick={handleRefresh}>
58+
<Button type="primary" icon={<ReloadOutlined />} onClick={refresh}>
8859
Try Again
8960
</Button>
9061
}
@@ -115,7 +86,7 @@ const EnvironmentDetail: React.FC = () => {
11586
</div>
11687
<Button
11788
icon={<ReloadOutlined />}
118-
onClick={handleRefresh}
89+
onClick={refresh}
11990
>
12091
Refresh
12192
</Button>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useState, useEffect, useCallback } from 'react';
2+
import { getEnvironmentById } from '../services/environments.service';
3+
import { Environment } from '../types/environment.types';
4+
5+
/**
6+
* Custom hook to fetch and manage environment detail data
7+
* @param id - Environment ID to fetch
8+
* @returns Object containing environment data, loading state, error state, and refresh function
9+
*/
10+
export const useEnvironmentDetail = (id: string) => {
11+
const [environment, setEnvironment] = useState<Environment | null>(null);
12+
const [loading, setLoading] = useState<boolean>(true);
13+
const [error, setError] = useState<string | null>(null);
14+
15+
// Function to fetch environment data
16+
const fetchEnvironmentData = useCallback(async () => {
17+
if (!id) {
18+
setError('No environment ID provided');
19+
setLoading(false);
20+
return;
21+
}
22+
23+
setLoading(true);
24+
setError(null);
25+
26+
try {
27+
const data = await getEnvironmentById(id);
28+
setEnvironment(data);
29+
} catch (err) {
30+
setError(err instanceof Error ? err.message : 'Failed to fetch environment details');
31+
} finally {
32+
setLoading(false);
33+
}
34+
}, [id]);
35+
36+
// Fetch environment data on mount and when ID changes
37+
useEffect(() => {
38+
fetchEnvironmentData();
39+
}, [fetchEnvironmentData]);
40+
41+
// Return the state and a function to refresh data
42+
return {
43+
environment,
44+
loading,
45+
error,
46+
refresh: fetchEnvironmentData,
47+
};
48+
};

0 commit comments

Comments
 (0)