From 68cf6d6fd1470ba231fbc93f5ecacf31e4b2f90d Mon Sep 17 00:00:00 2001 From: akhisud3195 Date: Wed, 19 Feb 2025 12:42:47 +0530 Subject: [PATCH] Use editable-field with save for scenarios --- .../components/ScenarioComponents.tsx | 121 ++++++------------ 1 file changed, 38 insertions(+), 83 deletions(-) diff --git a/apps/rowboat/app/projects/[projectId]/simulation/components/ScenarioComponents.tsx b/apps/rowboat/app/projects/[projectId]/simulation/components/ScenarioComponents.tsx index c509f99f..f6214960 100644 --- a/apps/rowboat/app/projects/[projectId]/simulation/components/ScenarioComponents.tsx +++ b/apps/rowboat/app/projects/[projectId]/simulation/components/ScenarioComponents.tsx @@ -1,10 +1,11 @@ 'use client'; -import { useState, useEffect, useCallback, ChangeEvent } from 'react'; +import { useState, useEffect } from 'react'; import { XMarkIcon } from '@heroicons/react/24/outline'; import { WithStringId } from '../../../../lib/types/types'; import { Scenario } from "../../../../lib/types/testing_types"; import { z } from 'zod'; +import { EditableField } from '../../../../lib/components/editable-field'; type ScenarioType = WithStringId>; @@ -16,49 +17,20 @@ interface ScenarioViewerProps { export function ScenarioViewer({ scenario, onSave, onClose }: ScenarioViewerProps) { const [editedScenario, setEditedScenario] = useState(scenario); - const [saveTimeout, setSaveTimeout] = useState(null); // Reset state when scenario changes useEffect(() => { setEditedScenario(scenario); }, [scenario]); - const handleChange = useCallback((field: keyof ScenarioType, event: ChangeEvent) => { - event.preventDefault(); - const value = event.target.value; - - setEditedScenario(prev => ({ - ...prev, + const handleFieldChange = (field: keyof ScenarioType) => (value: string) => { + const updatedScenario = { + ...editedScenario, [field]: value, - })); - - if (saveTimeout) { - clearTimeout(saveTimeout); - } - - const timeoutId = setTimeout(() => { - onSave({ - ...editedScenario, - [field]: value, - }); - }, 1000); // Increased debounce time to 1 second - - setSaveTimeout(timeoutId); - }, [editedScenario, onSave, saveTimeout]); - - // Cleanup timeout on unmount - useEffect(() => { - return () => { - if (saveTimeout) { - clearTimeout(saveTimeout); - } }; - }, [saveTimeout]); - - const adjustTextareaHeight = useCallback((element: HTMLTextAreaElement) => { - element.style.height = 'auto'; - element.style.height = `${element.scrollHeight}px`; - }, []); + setEditedScenario(updatedScenario); + onSave(updatedScenario); + }; return (
@@ -73,62 +45,45 @@ export function ScenarioViewer({ scenario, onSave, onClose }: ScenarioViewerProp
-
-
NAME
- handleChange('name', e)} - className="text-base border border-gray-200 rounded px-2 py-1 hover:border-gray-300 focus:border-blue-500 focus:ring-1 focus:ring-blue-500" - autoComplete="off" - spellCheck="false" - /> -
+
-
-
DESCRIPTION
-