Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/components/TagItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
</template>

<script>
import { showInfo } from '@nextcloud/dialogs'
import { showError, showInfo } from '@nextcloud/dialogs'
import { NcActionInput as ActionInput, NcActions as Actions, NcActionText as ActionText, NcLoadingIcon as IconLoading, NcActionButton, NcColorPicker } from '@nextcloud/vue'
import { mapStores } from 'pinia'
import IconEdit from 'vue-material-design-icons/PencilOutline.vue'
import DeleteIcon from 'vue-material-design-icons/TrashCanOutline.vue'
import logger from '../logger.js'
import useMainStore from '../store/mainStore.js'
import { translateTagDisplayName } from '../util/tag.js'
import { translateTagDisplayName, validateTag } from '../util/tag.js'

export default {
name: 'TagItem',
Expand Down Expand Up @@ -151,19 +151,22 @@ export default {
},

async renameTag(tag, event) {
this.renameTagInput = false
const displayName = event.target.querySelector('input[type=text]').value.trim()
const valid = validateTag(tag.id, displayName)
if (valid !== true) {
showError(valid)
return
}

this.showSaving = false
const displayName = event.target.querySelector('input[type=text]').value

try {
await this.mainStore.updateTag({
tag,
displayName,
color: this.tag.color,
})
this.renameTagLabel = true
this.renameTagInput = false
this.showSaving = false
this.closeEditTag()
} catch (error) {
showInfo(t('mail', 'An error occurred, unable to rename the tag.'))
logger.error('could not rename tag', { error })
Expand Down
62 changes: 6 additions & 56 deletions src/components/TagModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import DeleteTagModal from './DeleteTagModal.vue'
import TagItem from './TagItem.vue'
import logger from '../logger.js'
import useMainStore from '../store/mainStore.js'
import { validateTag } from '../util/tag.js'
import { hiddenTags } from './tags.js'

function randomColor() {
Expand Down Expand Up @@ -99,8 +100,6 @@ export default {
tagLabel: true,
tagInput: false,
showSaving: false,
renameTagLabel: true,
renameTagInput: false,
deleteTagModal: false,
tagToDelete: null,
color: randomColor(),
Expand Down Expand Up @@ -161,19 +160,13 @@ export default {
return
}

const displayName = event.target.querySelector('input[type=text]').value
if (displayName.toLowerCase() in hiddenTags) {
showError(this.t('mail', 'Tag name is a hidden system tag'))
return
}
if (this.mainStore.getTags.some((tag) => tag.displayName === displayName)) {
showError(this.t('mail', 'Tag already exists'))
return
}
if (displayName.trim() === '') {
showError(this.t('mail', 'Tag name cannot be empty'))
const displayName = event.target.querySelector('input[type=text]').value.trim()
const valid = validateTag(null, displayName)
if (valid !== true) {
showError(valid)
return
}

try {
await this.mainStore.createTag({
displayName,
Expand All @@ -188,49 +181,6 @@ export default {
}
},

convertHex(color, opacity) {
if (color.length === 4) {
const r = parseInt(color.substring(1, 2), 16)
const g = parseInt(color.substring(2, 3), 16)
const b = parseInt(color.substring(3, 4), 16)
return `rgba(${r}, ${g}, ${b}, ${opacity})`
} else {
const r = parseInt(color.substring(1, 3), 16)
const g = parseInt(color.substring(3, 5), 16)
const b = parseInt(color.substring(5, 7), 16)
return `rgba(${r}, ${g}, ${b}, ${opacity})`
}
},

openEditTag() {
this.renameTagLabel = false
this.renameTagInput = true
this.showSaving = false
},

async renameTag(tag, event) {
this.renameTagInput = false
this.showSaving = false
const displayName = event.target.querySelector('input[type=text]').value

try {
await this.mainStore.updateTag({
tag,
displayName,
color: tag.color,
})
this.renameTagLabel = true
this.renameTagInput = false
this.showSaving = false
} catch (error) {
showInfo(t('mail', 'An error occurred, unable to rename the tag.'))
logger.error('could not rename tag', { error })
this.renameTagLabel = false
this.renameTagInput = false
this.showSaving = true
}
},

deleteTag(tag) {
this.tagToDelete = tag
this.deleteTagModal = true
Expand Down
21 changes: 21 additions & 0 deletions src/util/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/

import { translate as t } from '@nextcloud/l10n'
import { hiddenTags } from '../components/tags.js'
import { FOLLOW_UP_TAG_LABEL } from '../store/constants.js'
import useMainStore from '../store/mainStore.js'

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

return tag.displayName
}

export function validateTag(tagId, tagName) {
const testableDisplayName = tagName.toLowerCase().trim()

if (testableDisplayName === '') {
return t('mail', 'Tag name cannot be empty')
}

if (Object.keys(hiddenTags).some((tag) => tag.toLowerCase() === testableDisplayName)) {
return t('mail', 'Tag name is a hidden system tag')
}

const mainStore = useMainStore()
if (mainStore.getTags.some((tag) => tagId !== tag.id && tag.displayName.toLowerCase() === testableDisplayName)) {
return t('mail', 'Tag already exists')
}

return true
}
Loading