From e861305cb040ddef375b63df96dc50706bda19b5 Mon Sep 17 00:00:00 2001 From: Harshith Mohan <26010946+harshithmohan@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:49:52 +0530 Subject: [PATCH] Don't allow new renamer config if name already exists (#975) --- src/components/Utilities/Renamer/ConfigModal.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/Utilities/Renamer/ConfigModal.tsx b/src/components/Utilities/Renamer/ConfigModal.tsx index ca7f96d46..c6caf9100 100644 --- a/src/components/Utilities/Renamer/ConfigModal.tsx +++ b/src/components/Utilities/Renamer/ConfigModal.tsx @@ -1,11 +1,12 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; +import { find } from 'lodash'; import Button from '@/components/Input/Button'; import Input from '@/components/Input/Input'; import Select from '@/components/Input/Select'; import ModalPanel from '@/components/Panels/ModalPanel'; import { useRenamerNewConfigMutation, useRenamerPatchConfigMutation } from '@/core/react-query/renamer/mutations'; -import { useRenamersQuery } from '@/core/react-query/renamer/queries'; +import { useRenamerConfigsQuery, useRenamersQuery } from '@/core/react-query/renamer/queries'; import useEventCallback from '@/hooks/useEventCallback'; import type { RenamerConfigType } from '@/core/react-query/renamer/types'; @@ -19,6 +20,7 @@ type Props = { const ConfigModal = ({ config, onClose, rename, show }: Props) => { const renamers = useRenamersQuery(show && !rename).data; + const renamerConfigs = useRenamerConfigsQuery(show && !rename).data; const [configName, setConfigName] = useState(''); const [selectedRenamer, setSelectedRenamer] = useState('na'); @@ -63,6 +65,11 @@ const ConfigModal = ({ config, onClose, rename, show }: Props) => { } }); + const configExists = useMemo( + () => !!find(renamerConfigs, item => item.Name === configName), + [configName, renamerConfigs], + ); + return ( { buttonType="primary" className="px-6 py-2" loading={isPatchConfigPending || isNewConfigPending} - disabled={!configName || selectedRenamer === 'na'} + disabled={!configName || selectedRenamer === 'na' || configExists} + tooltip={configExists ? 'Config with the same name already exists!' : ''} > Save