From e232d33c07287dd4acfe919f89c88f8bdf9b1759 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Tue, 25 Jul 2023 18:49:52 -0300 Subject: [PATCH] feat(EditFlowSettingsComponent): add validation for duplicate flow names - Added `invalidName` and `setInvalidName` props to `EditFlowSettings` component to handle validation for duplicate flow names. - Added `nameLists` useRef to store the list of flow names fetched from the database. - Added `useEffect` to fetch the flow names from the database and populate `nameLists` useRef. - Modified `handleNameChange` function to check if the entered name already exists in `nameLists` useRef and set `invalidName` accordingly. - Added a new span element to display an error message if the name is already in use. fix(flowSettingsModal): disable save button when the flow name is invalid - Added `invalidName` and `setInvalidName` states to `FlowSettingsModal` component to handle the validation for invalid flow names. - Passed `invalidName` and `setInvalidName` props to `EditFlowSettings` component. - Disabled the save button when `invalidName` is true. --- .../EditFlowSettingsComponent/index.tsx | 24 +++++++++++++++++-- .../src/modals/flowSettingsModal/index.tsx | 6 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/frontend/src/components/EditFlowSettingsComponent/index.tsx b/src/frontend/src/components/EditFlowSettingsComponent/index.tsx index 28b82d78e..6504c2b4e 100644 --- a/src/frontend/src/components/EditFlowSettingsComponent/index.tsx +++ b/src/frontend/src/components/EditFlowSettingsComponent/index.tsx @@ -1,7 +1,8 @@ -import React, { ChangeEvent, useState } from "react"; +import React, { ChangeEvent, useEffect, useRef, useState } from "react"; import { Input } from "../../components/ui/input"; import { Label } from "../../components/ui/label"; import { Textarea } from "../../components/ui/textarea"; +import { readFlowsFromDatabase } from "../../controllers/API"; type InputProps = { name: string | null; @@ -9,6 +10,8 @@ type InputProps = { maxLength?: number; flows: Array<{ id: string; name: string; description: string }>; tabId: string; + invalidName: boolean; + setInvalidName: (invalidName: boolean) => void; setName: (name: string) => void; setDescription: (description: string) => void; updateFlow: (flow: { id: string; name: string }) => void; @@ -16,6 +19,8 @@ type InputProps = { export const EditFlowSettings: React.FC = ({ name, + invalidName, + setInvalidName, description, maxLength = 50, flows, @@ -25,6 +30,14 @@ export const EditFlowSettings: React.FC = ({ updateFlow, }) => { const [isMaxLength, setIsMaxLength] = useState(false); + const nameLists = useRef([]); + useEffect(() => { + readFlowsFromDatabase().then((flows) => { + flows.forEach((flow) => { + nameLists.current.push(flow.name); + }); + }); + }, []); const handleNameChange = (event: ChangeEvent) => { const { value } = event.target; @@ -33,7 +46,11 @@ export const EditFlowSettings: React.FC = ({ } else { setIsMaxLength(false); } - + if (!nameLists.current.includes(value)) { + setInvalidName(false); + } else { + setInvalidName(true); + } setName(value); }; @@ -55,6 +72,9 @@ export const EditFlowSettings: React.FC = ({ {isMaxLength && ( Character limit reached )} + {invalidName && ( + Name already in use + )} f.id === tabId).description ); + const [invalidName, setInvalidName] = useState(false); + function handleClick() { let savedFlow = flows.find((f) => f.id === tabId); savedFlow.name = name; @@ -39,6 +41,8 @@ export default function FlowSettingsModal({ -