feat: namespace settings archive script setup

This commit is contained in:
Dominik Pschenitschni 2022-09-13 17:33:56 +02:00 committed by Gitea
parent 221edb2086
commit ad6b335d41
3 changed files with 81 additions and 42 deletions

View file

@ -1,12 +1,21 @@
import { computed, watchEffect } from 'vue' import { computed } from 'vue'
import type { ComputedGetter } from 'vue' import type { Ref } from 'vue'
import { setTitle } from '@/helpers/setTitle' import {useTitle as useTitleVueUse, resolveRef} from '@vueuse/core'
export function useTitle(titleGetter: ComputedGetter<string>) { type UseTitleParameters = Parameters<typeof useTitleVueUse>
const titleRef = computed(titleGetter)
watchEffect(() => setTitle(titleRef.value)) export function useTitle(...args: UseTitleParameters) {
return titleRef const [newTitle, ...restArgs] = args
const pageTitle = resolveRef(newTitle) as Ref<string>
const completeTitle = computed(() =>
(typeof pageTitle.value === 'undefined' || pageTitle.value === '')
? 'Vikunja'
: `${pageTitle.value} | Vikunja`,
)
return useTitleVueUse(completeTitle, ...restArgs)
} }

View file

@ -238,6 +238,7 @@ const router = createRouter({
meta: { meta: {
showAsModal: true, showAsModal: true,
}, },
props: route => ({ namespaceId: parseInt(route.params.id as string) }),
}, },
{ {
path: '/namespaces/:id/settings/delete', path: '/namespaces/:id/settings/delete',

View file

@ -7,50 +7,79 @@
<template #text> <template #text>
<p> <p>
{{ namespace.isArchived ? $t('namespace.archive.unarchiveText') : $t('namespace.archive.archiveText')}} {{
namespace.isArchived
? $t('namespace.archive.unarchiveText')
: $t('namespace.archive.archiveText')
}}
</p> </p>
</template> </template>
</modal> </modal>
</template> </template>
<script lang="ts"> <script lang="ts">
import {defineComponent} from 'vue' export default { name: 'namespace-setting-archive' }
</script>
<script setup lang="ts">
import {watch, reactive, ref, shallowReactive} from 'vue'
import {useRouter} from 'vue-router'
import {useStore} from '@/store'
import {useI18n} from 'vue-i18n'
import {success} from '@/message'
import {useTitle} from '@/composables/useTitle'
import NamespaceService from '@/services/namespace' import NamespaceService from '@/services/namespace'
import { setTitle } from '@/helpers/setTitle' import type {INamespace} from '@/modelTypes/INamespace'
import NamespaceModel from '@/models/namespace'
export default defineComponent({ const props = defineProps({
name: 'namespace-setting-archive', namespaceId: {
data() { type: Number,
return { required: true,
namespaceService: new NamespaceService(),
namespace: null,
title: '',
}
},
created() {
this.namespace = this.$store.getters['namespaces/getNamespaceById'](this.$route.params.id)
this.title = this.namespace.isArchived ?
this.$t('namespace.archive.titleUnarchive', {namespace: this.namespace.title}) :
this.$t('namespace.archive.titleArchive', {namespace: this.namespace.title})
setTitle(this.title)
},
methods: {
async archiveNamespace() {
try {
const isArchived = !this.namespace.isArchived
const namespace = await this.namespaceService.update({
...this.namespace,
isArchived,
})
this.$store.commit('namespaces/setNamespaceById', namespace)
this.$message.success({message: this.$t(isArchived ? 'namespace.archive.success' : 'namespace.archive.unarchiveSuccess')})
} finally {
this.$router.back()
}
},
}, },
}) })
const store = useStore()
const router = useRouter()
const {t} = useI18n({useScope: 'global'})
const title = ref('')
useTitle(title)
const namespaceService = shallowReactive(new NamespaceService())
const namespace : INamespace = reactive(new NamespaceModel())
watch(
() => props.namespaceId,
async () => {
Object.assign(namespace, store.getters['namespaces/getNamespaceById'](props.namespaceId))
// FIXME: ressouce should be loaded in store
Object.assign(namespace, await namespaceService.get({id: props.namespaceId}))
title.value = namespace.isArchived ?
t('namespace.archive.titleUnarchive', {namespace: namespace.title}) :
t('namespace.archive.titleArchive', {namespace: namespace.title})
},
{ immediate: true },
)
async function archiveNamespace() {
try {
const isArchived = !namespace.isArchived
const archivedNamespace = await namespaceService.update({
...namespace,
isArchived,
})
store.commit('namespaces/setNamespaceById', archivedNamespace)
success({
message: isArchived
? t('namespace.archive.success')
: t('namespace.archive.unarchiveSuccess'),
})
} finally {
router.back()
}
}
</script> </script>