1
- import React , { FC , KeyboardEvent , useEffect , useState } from 'react'
1
+ import React , { FC , KeyboardEvent , useEffect , useMemo , useState } from 'react'
2
2
import { Tag as TTag } from 'common/types/responses'
3
3
import Constants from 'common/constants'
4
4
import Permission from 'common/providers/Permission'
5
5
import Utils from 'common/utils/utils'
6
6
import {
7
7
useCreateTagMutation ,
8
+ useGetTagsQuery ,
8
9
useUpdateTagMutation ,
9
10
} from 'common/services/useTag'
10
11
11
12
import InputGroup from 'components/base/forms/InputGroup'
12
13
import Button from 'components/base/forms/Button'
13
14
import Tag from './Tag'
14
15
import InlineModal from 'components/InlineModal'
15
- import InfoMessage from 'components/InfoMessage '
16
+ import ErrorMessage from 'components/ErrorMessage '
16
17
17
18
type CreateEditTagType = {
18
19
projectId : string
@@ -43,6 +44,18 @@ const CreateEditTag: FC<CreateEditTagType> = ({
43
44
editTag ,
44
45
{ data : editData , isLoading : saving , isSuccess : editSuccess } ,
45
46
] = useUpdateTagMutation ( )
47
+
48
+ const { data : tags } = useGetTagsQuery ( { projectId } )
49
+ const existingTag = useMemo ( ( ) => {
50
+ if ( tag ?. label && tags ) {
51
+ const lowercaseTag = tag ?. label . toLowerCase ( )
52
+ return tags ?. find ( ( tag ) => {
53
+ return tag . label . toLowerCase ( ) === lowercaseTag
54
+ } )
55
+ }
56
+ return false
57
+ } , [ tags , tag ?. label ] )
58
+
46
59
const tagsSaving = creating || saving
47
60
48
61
useEffect ( ( ) => {
@@ -115,7 +128,11 @@ const CreateEditTag: FC<CreateEditTagType> = ({
115
128
onClick = { save }
116
129
type = 'button'
117
130
disabled = {
118
- tagsSaving || ! tag ?. color || ! tag ?. label || ! permission
131
+ ! ! existingTag ||
132
+ tagsSaving ||
133
+ ! tag ?. color ||
134
+ ! tag ?. label ||
135
+ ! permission
119
136
}
120
137
>
121
138
Save Tag
@@ -143,7 +160,6 @@ const CreateEditTag: FC<CreateEditTagType> = ({
143
160
onChange = { ( e : InputEvent ) => update ( 'label' , e ) }
144
161
/>
145
162
< InputGroup
146
- className = 'mb-0'
147
163
title = 'Select a color'
148
164
component = {
149
165
< Row className = { 'gap-3' } >
@@ -160,6 +176,9 @@ const CreateEditTag: FC<CreateEditTagType> = ({
160
176
}
161
177
className = 'select-colour'
162
178
/>
179
+ { existingTag && (
180
+ < ErrorMessage error = { 'A tag already exists with this name' } />
181
+ ) }
163
182
</ div >
164
183
</ InlineModal >
165
184
)
0 commit comments