Skip to content

Commit 2defd76

Browse files
fix: improve getAllConnections — rename param, add optional chaining, fallback to empty array
- Rename connectionData → updatedConnection for clarity - Add optional chaining (updatedConnection?.id, c?.id) for edge cases - Ensure connectionList always falls back to [] if API returns undefined/null - Applied to both NewEnv.jsx and NewProject.jsx
1 parent ab35aa4 commit 2defd76

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

frontend/src/base/components/environment/NewEnv.jsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,24 @@ const NewEnv = ({
250250
);
251251

252252
const getAllConnections = useCallback(
253-
async (connectionData) => {
254-
if (connectionData) {
253+
async (updatedConnection) => {
254+
if (updatedConnection?.id) {
255255
setConnectionList((prev) => {
256-
const exists = prev.some((c) => c.id === connectionData.id);
256+
const list = prev || [];
257+
const exists = list.some((c) => c?.id === updatedConnection.id);
257258
if (exists) {
258-
return prev.map((c) =>
259-
c.id === connectionData.id ? connectionData : c
259+
return list.map((c) =>
260+
c?.id === updatedConnection.id ? updatedConnection : c
260261
);
261262
}
262-
return [...prev, connectionData];
263+
return [...list, updatedConnection];
263264
});
264265
return;
265266
}
266267
setLoading(true);
267268
try {
268269
const data = await fetchAllConnections(axiosRef, selectedOrgId);
269-
setConnectionList(data?.filter((el) => !el?.is_sample_project));
270+
setConnectionList(data?.filter((el) => !el?.is_sample_project) || []);
270271
} catch (error) {
271272
console.error(error);
272273
notify({ error });

frontend/src/base/new-project/NewProject.jsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,23 @@ function NewProject({ open, setOpen, getAllProject, id }) {
6767
}, [form, setOpen]);
6868

6969
const getAllConnections = useCallback(
70-
async (connectionData) => {
71-
if (connectionData) {
70+
async (updatedConnection) => {
71+
if (updatedConnection?.id) {
7272
setConnectionList((prev) => {
73-
const exists = prev.some((c) => c.id === connectionData.id);
73+
const list = prev || [];
74+
const exists = list.some((c) => c?.id === updatedConnection.id);
7475
if (exists) {
75-
return prev.map((c) =>
76-
c.id === connectionData.id ? connectionData : c
76+
return list.map((c) =>
77+
c?.id === updatedConnection.id ? updatedConnection : c
7778
);
7879
}
79-
return [...prev, connectionData];
80+
return [...list, updatedConnection];
8081
});
8182
return;
8283
}
8384
try {
8485
const data = await getAllConnectionsApi(axiosPrivate, selectedOrgId);
85-
setConnectionList(data);
86+
setConnectionList(data || []);
8687
} catch (error) {
8788
console.error(error);
8889
notify({ error });

0 commit comments

Comments
 (0)