Skip to content

Commit 698e83a

Browse files
authored
Adding the Warning message whenever a terms is created within a same … (#1696)
1 parent 5a4f3bd commit 698e83a

File tree

1 file changed

+61
-8
lines changed
  • odd-platform-ui/src/components/Terms/TermSearch/TermForm

1 file changed

+61
-8
lines changed

odd-platform-ui/src/components/Terms/TermSearch/TermForm/TermsForm.tsx

+61-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { cloneElement, type FC, useEffect, useMemo } from 'react';
1+
import React, { cloneElement, type FC, useEffect, useMemo, useState } from 'react';
22
import { Controller, useForm } from 'react-hook-form';
33
import { useNavigate } from 'react-router-dom';
4-
import { Grid, Typography } from '@mui/material';
4+
import { Grid, Typography, Alert,IconButton } from '@mui/material';
55
import { useTranslation } from 'react-i18next';
66
import type { TermDetails, TermFormData } from 'generated-sources';
77
import {
@@ -12,11 +12,14 @@ import {
1212
NamespaceAutocomplete,
1313
} from 'components/shared/elements';
1414
import { useAppDispatch, useAppSelector } from 'redux/lib/hooks';
15-
import { createTerm, updateTerm } from 'redux/thunks';
15+
import { createTerm, updateTerm, fetchTermsSearchResults } from 'redux/thunks';
1616
import {
1717
getTermCreatingStatuses,
1818
getTermDetails,
1919
getTermUpdatingStatuses,
20+
getTermSearchResults,
21+
getTermSearchId,
22+
getTermSearchFacetsSynced,
2023
} from 'redux/selectors';
2124
import { termDetailsPath, useTermsRouteParams } from 'routes';
2225
import { useQueryClient } from '@tanstack/react-query';
@@ -40,6 +43,12 @@ const TermsForm: FC<TermsFormDialogProps> = ({ btnCreateEl }) => {
4043
getTermUpdatingStatuses
4144
);
4245

46+
const searchId = useAppSelector(getTermSearchId);
47+
const isTermSearchFacetsSynced = useAppSelector(getTermSearchFacetsSynced);
48+
const existingTerms = useAppSelector(getTermSearchResults);
49+
50+
const [error, setError] = useState<string | null>(null);
51+
4352
const defaultValues = useMemo(
4453
() => ({
4554
name: term?.name ?? '',
@@ -57,13 +66,31 @@ const TermsForm: FC<TermsFormDialogProps> = ({ btnCreateEl }) => {
5766

5867
useEffect(() => {
5968
reset(defaultValues);
60-
}, [defaultValues]);
69+
}, [defaultValues, reset]);
70+
71+
useEffect(() => {
72+
if (searchId && isTermSearchFacetsSynced) {
73+
dispatch(fetchTermsSearchResults({ searchId, page: 1, size: 1000 }));
74+
}
75+
}, [dispatch, searchId, isTermSearchFacetsSynced]);
6176

6277
const clearState = () => {
6378
reset();
79+
setError(null);
6480
};
6581

6682
const onSubmit = (data: TermFormData) => {
83+
const duplicateTerm = existingTerms.find(
84+
(existingTerm: TermDetails) =>
85+
existingTerm.name === data.name &&
86+
existingTerm.namespace.name === data.namespaceName
87+
);
88+
89+
if (duplicateTerm) {
90+
setError('A term with the same name already exists in this namespace.');
91+
return;
92+
}
93+
6794
const parsedData = { ...data };
6895
(term && term.id
6996
? dispatch(updateTerm({ termId: term.id, termFormData: parsedData }))
@@ -81,12 +108,38 @@ const TermsForm: FC<TermsFormDialogProps> = ({ btnCreateEl }) => {
81108

82109
const termFormTitle = (
83110
<Typography variant='h4' component='span'>
84-
{term.id ? t('Edit') : t('Add')} {t('term')}
111+
{term?.id ? t('Edit') : t('Add')} {t('term')}
85112
</Typography>
86113
);
87114

88115
const termFormContent = () => (
89116
<form id='term-create-form' onSubmit={handleSubmit(onSubmit)}>
117+
{error && (
118+
<Alert
119+
severity="error"
120+
sx={{ mb: 2, alignItems: 'center' }}
121+
action={
122+
<Typography
123+
component="button"
124+
onClick={() => setError(null)}
125+
sx={{
126+
fontSize: '1.1rem',
127+
color: 'inherit',
128+
background: 'none',
129+
border: 'none',
130+
cursor: 'pointer',
131+
padding: 0,
132+
marginLeft: 2,
133+
lineHeight: '1.6rem',
134+
}}
135+
>
136+
×
137+
</Typography>
138+
}
139+
>
140+
{error}
141+
</Alert>
142+
)}
90143
<Controller
91144
name='name'
92145
control={control}
@@ -131,7 +184,7 @@ const TermsForm: FC<TermsFormDialogProps> = ({ btnCreateEl }) => {
131184

132185
const termFormActionButtons = () => (
133186
<Button
134-
text={term ? t('Save term') : t('Add term')}
187+
text={term?.id ? t('Save term') : t('Add term')}
135188
buttonType='main-lg'
136189
type='submit'
137190
form='term-create-form'
@@ -149,8 +202,8 @@ const TermsForm: FC<TermsFormDialogProps> = ({ btnCreateEl }) => {
149202
title={termFormTitle}
150203
renderContent={termFormContent}
151204
renderActions={termFormActionButtons}
152-
handleCloseSubmittedForm={term ? isTermUpdated : isTermCreated}
153-
isLoading={term ? isTermUpdating : isTermCreating}
205+
handleCloseSubmittedForm={term?.id ? isTermUpdated : isTermCreated}
206+
isLoading={term?.id ? isTermUpdating : isTermCreating}
154207
clearState={clearState}
155208
formSubmitHandler={handleSubmit(onSubmit)}
156209
/>

0 commit comments

Comments
 (0)