Skip to content

Commit 9ad1c47

Browse files
committed
fix: check tag while renaming
Signed-off-by: Roberto Guido <info@madbob.org>
1 parent a240cdf commit 9ad1c47

3 files changed

Lines changed: 37 additions & 63 deletions

File tree

src/components/TagItem.vue

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363
</template>
6464

6565
<script>
66-
import { showInfo } from '@nextcloud/dialogs'
66+
import { showError, showInfo } from '@nextcloud/dialogs'
6767
import { NcActionInput as ActionInput, NcActions as Actions, NcActionText as ActionText, NcLoadingIcon as IconLoading, NcActionButton, NcColorPicker } from '@nextcloud/vue'
6868
import { mapStores } from 'pinia'
6969
import IconEdit from 'vue-material-design-icons/PencilOutline.vue'
7070
import DeleteIcon from 'vue-material-design-icons/TrashCanOutline.vue'
7171
import logger from '../logger.js'
7272
import useMainStore from '../store/mainStore.js'
73-
import { translateTagDisplayName } from '../util/tag.js'
73+
import { translateTagDisplayName, validateTag } from '../util/tag.js'
7474
7575
export default {
7676
name: 'TagItem',
@@ -151,19 +151,22 @@ export default {
151151
},
152152
153153
async renameTag(tag, event) {
154-
this.renameTagInput = false
154+
const displayName = event.target.querySelector('input[type=text]').value.trim()
155+
const valid = validateTag(tag.id, displayName)
156+
if (valid !== true) {
157+
showError(valid)
158+
return
159+
}
160+
155161
this.showSaving = false
156-
const displayName = event.target.querySelector('input[type=text]').value
157162
158163
try {
159164
await this.mainStore.updateTag({
160165
tag,
161166
displayName,
162167
color: this.tag.color,
163168
})
164-
this.renameTagLabel = true
165-
this.renameTagInput = false
166-
this.showSaving = false
169+
this.closeEditTag()
167170
} catch (error) {
168171
showInfo(t('mail', 'An error occurred, unable to rename the tag.'))
169172
logger.error('could not rename tag', { error })

src/components/TagModal.vue

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import DeleteTagModal from './DeleteTagModal.vue'
6161
import TagItem from './TagItem.vue'
6262
import logger from '../logger.js'
6363
import useMainStore from '../store/mainStore.js'
64+
import { validateTag } from '../util/tag.js'
6465
import { hiddenTags } from './tags.js'
6566
6667
function randomColor() {
@@ -99,8 +100,6 @@ export default {
99100
tagLabel: true,
100101
tagInput: false,
101102
showSaving: false,
102-
renameTagLabel: true,
103-
renameTagInput: false,
104103
deleteTagModal: false,
105104
tagToDelete: null,
106105
color: randomColor(),
@@ -161,19 +160,13 @@ export default {
161160
return
162161
}
163162
164-
const displayName = event.target.querySelector('input[type=text]').value
165-
if (displayName.toLowerCase() in hiddenTags) {
166-
showError(this.t('mail', 'Tag name is a hidden system tag'))
167-
return
168-
}
169-
if (this.mainStore.getTags.some((tag) => tag.displayName === displayName)) {
170-
showError(this.t('mail', 'Tag already exists'))
171-
return
172-
}
173-
if (displayName.trim() === '') {
174-
showError(this.t('mail', 'Tag name cannot be empty'))
163+
const displayName = event.target.querySelector('input[type=text]').value.trim()
164+
const valid = validateTag(null, displayName)
165+
if (valid !== true) {
166+
showError(valid)
175167
return
176168
}
169+
177170
try {
178171
await this.mainStore.createTag({
179172
displayName,
@@ -188,49 +181,6 @@ export default {
188181
}
189182
},
190183
191-
convertHex(color, opacity) {
192-
if (color.length === 4) {
193-
const r = parseInt(color.substring(1, 2), 16)
194-
const g = parseInt(color.substring(2, 3), 16)
195-
const b = parseInt(color.substring(3, 4), 16)
196-
return `rgba(${r}, ${g}, ${b}, ${opacity})`
197-
} else {
198-
const r = parseInt(color.substring(1, 3), 16)
199-
const g = parseInt(color.substring(3, 5), 16)
200-
const b = parseInt(color.substring(5, 7), 16)
201-
return `rgba(${r}, ${g}, ${b}, ${opacity})`
202-
}
203-
},
204-
205-
openEditTag() {
206-
this.renameTagLabel = false
207-
this.renameTagInput = true
208-
this.showSaving = false
209-
},
210-
211-
async renameTag(tag, event) {
212-
this.renameTagInput = false
213-
this.showSaving = false
214-
const displayName = event.target.querySelector('input[type=text]').value
215-
216-
try {
217-
await this.mainStore.updateTag({
218-
tag,
219-
displayName,
220-
color: tag.color,
221-
})
222-
this.renameTagLabel = true
223-
this.renameTagInput = false
224-
this.showSaving = false
225-
} catch (error) {
226-
showInfo(t('mail', 'An error occurred, unable to rename the tag.'))
227-
logger.error('could not rename tag', { error })
228-
this.renameTagLabel = false
229-
this.renameTagInput = false
230-
this.showSaving = true
231-
}
232-
},
233-
234184
deleteTag(tag) {
235185
this.tagToDelete = tag
236186
this.deleteTagModal = true

src/util/tag.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
import { translate as t } from '@nextcloud/l10n'
7+
import { hiddenTags } from '../components/tags.js'
78
import { FOLLOW_UP_TAG_LABEL } from '../store/constants.js'
9+
import useMainStore from '../store/mainStore.js'
810

911
/**
1012
* Translate the display name of special tags or leave them as is if the user renamed them.
@@ -19,3 +21,22 @@ export function translateTagDisplayName(tag) {
1921

2022
return tag.displayName
2123
}
24+
25+
export function validateTag(tagId, tagName) {
26+
const testableDisplayName = tagName.toLowerCase().trim()
27+
28+
if (testableDisplayName === '') {
29+
return t('mail', 'Tag name cannot be empty')
30+
}
31+
32+
if (Object.keys(hiddenTags).some((tag) => tag.toLowerCase() === testableDisplayName)) {
33+
return t('mail', 'Tag name is a hidden system tag')
34+
}
35+
36+
const mainStore = useMainStore()
37+
if (mainStore.getTags.some((tag) => tagId !== tag.id && tag.displayName.toLowerCase() === testableDisplayName)) {
38+
return t('mail', 'Tag already exists')
39+
}
40+
41+
return true
42+
}

0 commit comments

Comments
 (0)