Skip to content

Commit

Permalink
Don't allow new renamer config if name already exists (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithmohan authored Aug 13, 2024
1 parent b77c850 commit e861305
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/components/Utilities/Renamer/ConfigModal.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -63,6 +65,11 @@ const ConfigModal = ({ config, onClose, rename, show }: Props) => {
}
});

const configExists = useMemo(
() => !!find(renamerConfigs, item => item.Name === configName),
[configName, renamerConfigs],
);

return (
<ModalPanel
show={show}
Expand Down Expand Up @@ -111,7 +118,8 @@ const ConfigModal = ({ config, onClose, rename, show }: Props) => {
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
</Button>
Expand Down

0 comments on commit e861305

Please sign in to comment.