chore: migrate namespace edit component to script setup

This commit is contained in:
kolaente 2022-06-12 15:12:14 +02:00
parent aadf75c7bf
commit 0997c3868d
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 51 additions and 60 deletions

View file

@ -40,8 +40,6 @@
</modal> </modal>
</template> </template>
<script lang="ts">
</script>
<script setup lang="ts"> <script setup lang="ts">
defineProps({ defineProps({
title: { title: {

View file

@ -25,7 +25,7 @@
<div class="field"> <div class="field">
<label class="label" for="namespacedescription">{{ $t('namespace.attributes.description') }}</label> <label class="label" for="namespacedescription">{{ $t('namespace.attributes.description') }}</label>
<div class="control"> <div class="control">
<editor <AsyncEditor
:class="{ 'disabled': namespaceService.loading}" :class="{ 'disabled': namespaceService.loading}"
:preview-is-default="false" :preview-is-default="false"
id="namespacedescription" id="namespacedescription"
@ -55,71 +55,64 @@
</create-edit> </create-edit>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import {defineComponent} from 'vue' import {nextTick, ref, watch} from 'vue'
import {useStore} from 'vuex'
import {success} from '@/message'
import router from '@/router'
import AsyncEditor from '@/components/input/AsyncEditor' import AsyncEditor from '@/components/input/AsyncEditor'
import NamespaceService from '@/services/namespace'
import NamespaceModel from '@/models/namespace'
import Fancycheckbox from '@/components/input/fancycheckbox.vue' import Fancycheckbox from '@/components/input/fancycheckbox.vue'
import ColorPicker from '@/components/input/colorPicker.vue' import ColorPicker from '@/components/input/colorPicker.vue'
import CreateEdit from '@/components/misc/create-edit.vue' import CreateEdit from '@/components/misc/create-edit.vue'
export default defineComponent({ import NamespaceService from '@/services/namespace'
name: 'namespace-setting-edit', import NamespaceModel from '@/models/namespace'
data() { import {useI18n} from 'vue-i18n'
return { import {useTitle} from '@/composables/useTitle'
namespaceService: new NamespaceService(),
namespace: new NamespaceModel(),
editorActive: false,
title: '',
}
},
components: {
CreateEdit,
ColorPicker,
Fancycheckbox,
editor: AsyncEditor,
},
props: {
namespaceId: {
type: Number,
required: true,
},
},
watch: {
// call again the method if the route changes
namespaceId: {
handler: 'loadNamespace',
immediate: true,
},
},
methods: {
async loadNamespace() {
// HACK: 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
this.editorActive = false
this.$nextTick(() => this.editorActive = true)
this.namespace = await this.namespaceService.get({id: this.namespaceId}) const {t} = useI18n()
// This will trigger the dynamic loading of components once we actually have all the data to pass to them const store = useStore()
this.manageTeamsComponent = 'manageSharing'
this.manageUsersComponent = 'manageSharing'
this.title = this.$t('namespace.edit.title', {namespace: this.namespace.title})
this.setTitle(this.title)
},
async save() { const namespaceService = ref(new NamespaceService())
const namespace = await this.namespaceService.update(this.namespace) const namespace = ref(new NamespaceModel())
// Update the namespace in the parent const editorActive = ref(false)
this.$store.commit('namespaces/setNamespaceById', namespace) const title = ref('')
this.$message.success({message: this.$t('namespace.edit.success')}) useTitle(() => title.value)
this.$router.back()
}, const props = defineProps({
namespaceId: {
type: Number,
required: true,
}, },
}) })
watch(
() => props.namespaceId,
loadNamespace,
{
immediate: true,
},
)
async function loadNamespace() {
// HACK: 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)
namespace.value = await namespaceService.value.get({id: props.namespaceId})
title.value = t('namespace.edit.title', {namespace: namespace.value.title})
}
async function save() {
const updatedNamespace = await namespaceService.value.update(namespace.value)
// Update the namespace in the parent
store.commit('namespaces/setNamespaceById', updatedNamespace)
success({message: t('namespace.edit.success')})
router.back()
}
</script> </script>