feat: ListLabels script setup (#2416)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2416
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-09-26 16:09:24 +00:00 committed by konrad
parent ba2605af1b
commit 89e428b4d2
2 changed files with 68 additions and 80 deletions

View file

@ -22,7 +22,7 @@ export default class LabelModel extends AbstractModel<ILabel> implements ILabel
created: Date = null created: Date = null
updated: Date = null updated: Date = null
constructor(data: Partial<ILabel>) { constructor(data: Partial<ILabel> = {}) {
super() super()
this.assignData(data) this.assignData(data)

View file

@ -109,90 +109,78 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import {defineComponent} from 'vue' import {computed, nextTick, ref} from 'vue'
import {mapState as mapVuexState} from 'vuex' import {useI18n} from 'vue-i18n'
import {mapState} from 'pinia'
import LabelModel from '../../models/label' import BaseButton from '@/components/base/BaseButton.vue'
import Editor from '@/components/input/AsyncEditor'
import ColorPicker from '@/components/input/colorPicker.vue'
import LabelModel from '@/models/label'
import type {ILabel} from '@/modelTypes/ILabel' import type {ILabel} from '@/modelTypes/ILabel'
import {useLabelStore} from '@/stores/labels' import {useLabelStore} from '@/stores/labels'
import BaseButton from '@/components/base/BaseButton.vue' import { useTitle } from '@/composables/useTitle'
import AsyncEditor from '@/components/input/AsyncEditor' import { useStore } from '@/store'
import ColorPicker from '@/components/input/colorPicker.vue'
import { setTitle } from '@/helpers/setTitle'
export default defineComponent({ const {t} = useI18n({useScope: 'global'})
name: 'ListLabels',
components: { const labelEditLabel = ref<ILabel>(new LabelModel())
BaseButton, const isLabelEdit = ref(false)
ColorPicker, const editorActive = ref(false)
editor: AsyncEditor, const showDeleteModal = ref(false)
}, const labelToDelete = ref<ILabel>(null)
data() {
return { useTitle(() => t('label.title'))
labelEditLabel: new LabelModel(),
isLabelEdit: false, const store = useStore()
editorActive: false, const userInfo = computed(() => store.state.auth.info)
showDeleteModal: false,
labelToDelete: null, const labelStore = useLabelStore()
} labelStore.loadAllLabels()
},
created() { // Alphabetically sort the labels
const labelStore = useLabelStore() const labels = computed(() => Object.values(labelStore.labels).sort((f, s) => f.title > s.title ? 1 : -1))
labelStore.loadAllLabels() const loading = computed(() =>labelStore.isLoading)
},
mounted() { function deleteLabel(label: ILabel) {
setTitle(this.$t('label.title')) showDeleteModal.value = false
}, isLabelEdit.value = false
computed: {
...mapVuexState({
userInfo: state => state.auth.info,
}),
...mapState(useLabelStore, {
// Alphabetically sort the labels
labels: state => Object.values(state.labels).sort((f, s) => f.title > s.title ? 1 : -1),
loading: state => state.isLoading,
}),
},
methods: {
deleteLabel(label: ILabel) {
this.showDeleteModal = false
this.isLabelEdit = false
const labelStore = useLabelStore() const labelStore = useLabelStore()
return labelStore.deleteLabel(label) return labelStore.deleteLabel(label)
}, }
editLabelSubmit() {
function editLabelSubmit() {
const labelStore = useLabelStore() const labelStore = useLabelStore()
return labelStore.updateLabel(this.labelEditLabel) return labelStore.updateLabel(labelEditLabel.value)
}, }
editLabel(label: ILabel) {
if (label.createdBy.id !== this.userInfo.id) { function editLabel(label: ILabel) {
if (label.createdBy.id !== userInfo.value.id) {
return return
} }
// Duplicating the label to make sure it does not look like changes take effect immediatly as the label // Duplicating the label to make sure it does not look like changes take effect immediatly as the label
// object passed to this function here still has a reference to the store. // object passed to this function here still has a reference to the store.
this.labelEditLabel = new LabelModel({ labelEditLabel.value = new LabelModel({
...label, ...label,
// The model does not support passing dates into it directly so we need to convert them first // The model does not support passing dates into it directly so we need to convert them first
created: +label.created, created: +label.created,
updated: +label.updated, updated: +label.updated,
}) })
this.isLabelEdit = true isLabelEdit.value = true
// This makes the editor trigger its mounted function again which makes it forget every input // This makes the editor trigger its mounted function again which makes it forget every input
// it currently has in its textarea. This is a counter-hack to a hack inside of vue-easymde // it currently has in its textarea. This is a counter-hack to a hack inside of vue-easymde
// which made it impossible to detect change from the outside. Therefore the component would // which made it impossible to detect change from the outside. Therefore the component would
// not update if new content from the outside was made available. // not update if new content from the outside was made available.
// See https://github.com/NikulinIlya/vue-easymde/issues/3 // See https://github.com/NikulinIlya/vue-easymde/issues/3
this.editorActive = false editorActive.value = false
this.$nextTick(() => this.editorActive = true) nextTick(() => editorActive.value = true)
}, }
showDeleteDialoge(label: ILabel) {
this.labelToDelete = label function showDeleteDialoge(label: ILabel) {
this.showDeleteModal = true labelToDelete.value = label
}, showDeleteModal.value = true
}, }
})
</script> </script>