From e5b14c913a32d314a58b52d4257e7ee20ea1a602 Mon Sep 17 00:00:00 2001 From: iandebruin98 Date: Mon, 28 Oct 2024 10:26:00 +0100 Subject: [PATCH] fix: Refactor `fetchData` function to ensure it returns an array --- .../pages/projects/[project]/duplicate.tsx | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/apps/admin-server/src/pages/projects/[project]/duplicate.tsx b/apps/admin-server/src/pages/projects/[project]/duplicate.tsx index 0bdfca841..edf496f65 100644 --- a/apps/admin-server/src/pages/projects/[project]/duplicate.tsx +++ b/apps/admin-server/src/pages/projects/[project]/duplicate.tsx @@ -51,26 +51,43 @@ export default function ProjectDuplicate() { async function fetchData( url: string) { let response = await fetch(url) || []; - if (response.ok) { - let data = await response.json(); - if (!Array.isArray(data)) { - return data; - } - return data.map((item) => { - if (item.deletedAt) { - return null; - } - delete item.projectId; - item.originalId = item.id; - delete item.id; - return item; - }) + if (!response.ok) { + return []; + } + + let data = await response.json(); + + if (!Array.isArray(data)) { + return []; } + return data.map((item) => { + if (item.deletedAt) { + return null; + } + delete item.projectId; + item.originalId = item.id; + delete item.id; + return item; + }) } + type DuplicateData = { + areaId: number; + config: any; + emailConfig: any; + hostStatus: any; + name: string; + title: string; + widgets: any[]; + tags: any[]; + statuses: any[]; + resources: any[]; + resourceSettings: boolean; + }; + async function duplicate(values: z.infer) { - const duplicateData = { + const duplicateData: DuplicateData = { areaId: data.areaId, config: data.config, emailConfig: data.emailConfig,