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 labels = computed(() => Object.values(labelStore.labels).sort((f, s) => f.title > s.title ? 1 : -1))
const loading = computed(() =>labelStore.isLoading)
function deleteLabel(label: ILabel) {
showDeleteModal.value = false
isLabelEdit.value = false
const labelStore = useLabelStore()
return labelStore.deleteLabel(label)
}
function editLabelSubmit() {
const labelStore = useLabelStore() const labelStore = useLabelStore()
labelStore.loadAllLabels() return labelStore.updateLabel(labelEditLabel.value)
}, }
mounted() {
setTitle(this.$t('label.title'))
},
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()
return labelStore.deleteLabel(label)
},
editLabelSubmit() {
const labelStore = useLabelStore()
return labelStore.updateLabel(this.labelEditLabel)
},
editLabel(label: ILabel) {
if (label.createdBy.id !== this.userInfo.id) {
return
}
// 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.
this.labelEditLabel = new LabelModel({
...label,
// The model does not support passing dates into it directly so we need to convert them first
created: +label.created,
updated: +label.updated,
})
this.isLabelEdit = true
// This makes the editor trigger its mounted function again which makes it forget every input function editLabel(label: ILabel) {
// it currently has in its textarea. This is a counter-hack to a hack inside of vue-easymde if (label.createdBy.id !== userInfo.value.id) {
// which made it impossible to detect change from the outside. Therefore the component would return
// not update if new content from the outside was made available. }
// See https://github.com/NikulinIlya/vue-easymde/issues/3 // Duplicating the label to make sure it does not look like changes take effect immediatly as the label
this.editorActive = false // object passed to this function here still has a reference to the store.
this.$nextTick(() => this.editorActive = true) labelEditLabel.value = new LabelModel({
}, ...label,
showDeleteDialoge(label: ILabel) { // The model does not support passing dates into it directly so we need to convert them first
this.labelToDelete = label created: +label.created,
this.showDeleteModal = true updated: +label.updated,
}, })
}, isLabelEdit.value = true
})
// 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
// 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.
// See https://github.com/NikulinIlya/vue-easymde/issues/3
editorActive.value = false
nextTick(() => editorActive.value = true)
}
function showDeleteDialoge(label: ILabel) {
labelToDelete.value = label
showDeleteModal.value = true
}
</script> </script>