From 25733f23418434d2fd7d95b111e69e14e20e065c Mon Sep 17 00:00:00 2001 From: kriptonian1 Date: Sat, 21 Dec 2024 18:28:50 +0530 Subject: [PATCH] fix: improve type safety and error handling in variable addition --- .../app/(main)/project/[project]/layout.tsx | 78 ++++++++++++------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/apps/platform/src/app/(main)/project/[project]/layout.tsx b/apps/platform/src/app/(main)/project/[project]/layout.tsx index bdd48eef..1128f5a6 100644 --- a/apps/platform/src/app/(main)/project/[project]/layout.tsx +++ b/apps/platform/src/app/(main)/project/[project]/layout.tsx @@ -1,4 +1,5 @@ 'use client' +import type { MouseEvent } from 'react' import { useEffect, useState } from 'react' import { useSearchParams } from 'next/navigation' import { AddSVG } from '@public/svg/shared' @@ -52,17 +53,23 @@ function DetailedProjectPage({ environmentName: '', environmentValue: '' }) - const [availableEnvironments, setAvailableEnvironments] = useState([]) + const [availableEnvironments, setAvailableEnvironments] = useState< + Environment[] + >([]) const searchParams = useSearchParams() const tab = searchParams.get('tab') ?? 'rollup-details' - const addVariable = async (e: any) => { + const addVariable = async (e: MouseEvent) => { e.preventDefault() + if (!currentProject) { + throw new Error("Current project doesn't exist") + } + const request: CreateVariableRequest = { name: newVariableData.variableName, - projectSlug: currentProject?.slug as string, + projectSlug: currentProject.slug, entries: newVariableData.environmentValue ? [ { @@ -74,12 +81,13 @@ function DetailedProjectPage({ note: newVariableData.note } - const { success, error, data } = await ControllerInstance.getInstance().variableController.createVariable( + const { error } = + await ControllerInstance.getInstance().variableController.createVariable( request, {} ) - - if(error){ + + if (error) { // eslint-disable-next-line no-console -- we need to log the error console.error(error) } @@ -96,7 +104,6 @@ function DetailedProjectPage({ ) if (success && data) { - //@ts-ignore setCurrentProject(data) } else { // eslint-disable-next-line no-console -- we need to log the error @@ -109,8 +116,17 @@ function DetailedProjectPage({ useEffect(() => { const getAllEnvironments = async () => { - const { success, error, data }: ClientResponse = await ControllerInstance.getInstance().environmentController.getAllEnvironmentsOfProject( - { projectSlug: currentProject!.slug }, + if (!currentProject) { + throw new Error("Current project doesn't exist") + } + + const { + success, + error, + data + }: ClientResponse = + await ControllerInstance.getInstance().environmentController.getAllEnvironmentsOfProject( + { projectSlug: currentProject.slug }, {} ) @@ -182,9 +198,12 @@ function DetailedProjectPage({ )} {tab === 'variable' && ( - + - @@ -206,57 +225,60 @@ function DetailedProjectPage({
setNewVariableData({ ...newVariableData, variableName: e.target.value }) } - className="h-[2.75rem] w-[20rem] border-0 bg-[#2a2a2a] text-gray-300 placeholder:text-gray-500" + placeholder="Enter the key of the variable" + value={newVariableData.variableName} />
setNewVariableData({ ...newVariableData, note: e.target.value }) } - className="h-[2.75rem] w-[20rem] border-0 bg-[#2a2a2a] text-gray-300 placeholder:text-gray-500" + placeholder="Enter the note of the secret" + value={newVariableData.note} />
-