Skip to content

Commit e699912

Browse files
authored
chore: Add existing tag check (#3194)
1 parent ae94267 commit e699912

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

frontend/web/components/tags/CreateEditTag.tsx

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import React, { FC, KeyboardEvent, useEffect, useState } from 'react'
1+
import React, { FC, KeyboardEvent, useEffect, useMemo, useState } from 'react'
22
import { Tag as TTag } from 'common/types/responses'
33
import Constants from 'common/constants'
44
import Permission from 'common/providers/Permission'
55
import Utils from 'common/utils/utils'
66
import {
77
useCreateTagMutation,
8+
useGetTagsQuery,
89
useUpdateTagMutation,
910
} from 'common/services/useTag'
1011

1112
import InputGroup from 'components/base/forms/InputGroup'
1213
import Button from 'components/base/forms/Button'
1314
import Tag from './Tag'
1415
import InlineModal from 'components/InlineModal'
15-
import InfoMessage from 'components/InfoMessage'
16+
import ErrorMessage from 'components/ErrorMessage'
1617

1718
type CreateEditTagType = {
1819
projectId: string
@@ -43,6 +44,18 @@ const CreateEditTag: FC<CreateEditTagType> = ({
4344
editTag,
4445
{ data: editData, isLoading: saving, isSuccess: editSuccess },
4546
] = 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+
4659
const tagsSaving = creating || saving
4760

4861
useEffect(() => {
@@ -115,7 +128,11 @@ const CreateEditTag: FC<CreateEditTagType> = ({
115128
onClick={save}
116129
type='button'
117130
disabled={
118-
tagsSaving || !tag?.color || !tag?.label || !permission
131+
!!existingTag ||
132+
tagsSaving ||
133+
!tag?.color ||
134+
!tag?.label ||
135+
!permission
119136
}
120137
>
121138
Save Tag
@@ -143,7 +160,6 @@ const CreateEditTag: FC<CreateEditTagType> = ({
143160
onChange={(e: InputEvent) => update('label', e)}
144161
/>
145162
<InputGroup
146-
className='mb-0'
147163
title='Select a color'
148164
component={
149165
<Row className={'gap-3'}>
@@ -160,6 +176,9 @@ const CreateEditTag: FC<CreateEditTagType> = ({
160176
}
161177
className='select-colour'
162178
/>
179+
{existingTag && (
180+
<ErrorMessage error={'A tag already exists with this name'} />
181+
)}
163182
</div>
164183
</InlineModal>
165184
)

0 commit comments

Comments
 (0)