feat: list settings edit script setup (#1988)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: vikunja/frontend#1988
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-02 15:13:32 +00:00 committed by konrad
parent deef7106e6
commit f6437c81da
3 changed files with 78 additions and 44 deletions

View File

@ -0,0 +1,37 @@
import {watch, reactive, shallowReactive, unref, toRefs, readonly} from 'vue'
import type {MaybeRef} from '@vueuse/core'
import {useStore} from 'vuex'
import ListService from '@/services/list'
import ListModel from '@/models/list'
import { success } from '@/message'
import {useI18n} from 'vue-i18n'
export function useList(listId: MaybeRef<ListModel['id']>) {
const listService = shallowReactive(new ListService())
const {loading: isLoading} = toRefs(listService)
const list : ListModel = reactive(new ListModel({}))
const {t} = useI18n()
watch(
() => unref(listId),
async (listId) => {
const loadedList = await listService.get(new ListModel({id: listId}))
Object.assign(list, loadedList)
},
{immediate: true},
)
const store = useStore()
async function save() {
await store.dispatch('lists/updateList', list)
success({message: t('list.edit.success')})
}
return {
isLoading: readonly(isLoading),
list,
save,
}
}

View File

@ -3,6 +3,8 @@ import type { RouteLocation } from 'vue-router'
import {saveLastVisited} from '@/helpers/saveLastVisited' import {saveLastVisited} from '@/helpers/saveLastVisited'
import {store} from '@/store' import {store} from '@/store'
import type ListModel from '@/models/list'
import {saveListView, getListView} from '@/helpers/saveListView' import {saveListView, getListView} from '@/helpers/saveListView'
import {parseDateOrString} from '@/helpers/time/parseDateOrString' import {parseDateOrString} from '@/helpers/time/parseDateOrString'
import {getNextWeekDate} from '@/helpers/time/getNextWeekDate' import {getNextWeekDate} from '@/helpers/time/getNextWeekDate'
@ -273,6 +275,7 @@ const router = createRouter({
path: '/lists/:listId/settings/edit', path: '/lists/:listId/settings/edit',
name: 'list.settings.edit', name: 'list.settings.edit',
component: ListSettingEdit, component: ListSettingEdit,
props: route => ({ listId: parseInt(route.params.listId as ListModel['id']) }),
meta: { meta: {
showAsModal: true, showAsModal: true,
}, },

View File

@ -5,14 +5,14 @@
:primary-label="$t('misc.save')" :primary-label="$t('misc.save')"
@primary="save" @primary="save"
:tertiary="$t('misc.delete')" :tertiary="$t('misc.delete')"
@tertiary="$router.push({ name: 'list.list.settings.delete', params: { id: $route.params.listId } })" @tertiary="$router.push({ name: 'list.settings.delete', params: { id: listId } })"
> >
<div class="field"> <div class="field">
<label class="label" for="title">{{ $t('list.title') }}</label> <label class="label" for="title">{{ $t('list.title') }}</label>
<div class="control"> <div class="control">
<input <input
:class="{ 'disabled': listService.loading}" :class="{ 'disabled': isLoading}"
:disabled="listService.loading || undefined" :disabled="isLoading || undefined"
@keyup.enter="save" @keyup.enter="save"
class="input" class="input"
id="title" id="title"
@ -31,8 +31,8 @@
</label> </label>
<div class="control"> <div class="control">
<input <input
:class="{ 'disabled': listService.loading}" :class="{ 'disabled': isLoading}"
:disabled="listService.loading ? true : null" :disabled="isLoading || undefined"
@keyup.enter="save" @keyup.enter="save"
class="input" class="input"
id="identifier" id="identifier"
@ -46,8 +46,8 @@
<label class="label" for="listdescription">{{ $t('list.edit.description') }}</label> <label class="label" for="listdescription">{{ $t('list.edit.description') }}</label>
<div class="control"> <div class="control">
<editor <editor
:class="{ 'disabled': listService.loading}" :class="{ 'disabled': isLoading}"
:disabled="listService.loading" :disabled="isLoading"
:previewIsDefault="false" :previewIsDefault="false"
id="listdescription" id="listdescription"
:placeholder="$t('list.edit.descriptionPlaceholder')" :placeholder="$t('list.edit.descriptionPlaceholder')"
@ -66,50 +66,44 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import {defineComponent} from 'vue' export default { name: 'list-setting-edit' }
</script>
import AsyncEditor from '@/components/input/AsyncEditor' <script setup lang="ts">
import type {PropType} from 'vue'
import {useRouter} from 'vue-router'
import {useStore} from 'vuex'
import {useI18n} from 'vue-i18n'
import ListModel from '@/models/list' import Editor from '@/components/input/AsyncEditor'
import ListService from '@/services/list'
import ColorPicker from '@/components/input/colorPicker.vue' import ColorPicker from '@/components/input/colorPicker.vue'
import {CURRENT_LIST} from '@/store/mutation-types'
import CreateEdit from '@/components/misc/create-edit.vue' import CreateEdit from '@/components/misc/create-edit.vue'
export default defineComponent({ import {CURRENT_LIST} from '@/store/mutation-types'
name: 'list-setting-edit', import type ListModel from '@/models/list'
data() {
return {
list: ListModel,
listService: new ListService(),
}
},
components: {
CreateEdit,
ColorPicker,
editor: AsyncEditor,
},
watch: {
'$route.params.listId': {
handler: 'loadList',
immediate: true,
},
},
methods: {
async loadList() {
const list = new ListModel({id: this.$route.params.listId})
const loadedList = await this.listService.get(list) import { useList } from '@/composables/useList'
this.list = { ...loadedList } import { useTitle } from '@/composables/useTitle'
},
async save() { const props = defineProps({
await this.$store.dispatch('lists/updateList', this.list) listId: {
await this.$store.dispatch(CURRENT_LIST, {list: this.list}) type: Number as PropType<ListModel['id']>,
this.setTitle(this.$t('list.edit.title', {list: this.list.title})) required: true,
this.$message.success({message: this.$t('list.edit.success')})
this.$router.back()
},
}, },
}) })
const router = useRouter()
const store = useStore()
const {t} = useI18n()
const {list, save: saveList, isLoading} = useList(props.listId)
useTitle(() => list?.title ? t('list.edit.title', {list: list.title}) : '')
async function save() {
await saveList()
await store.dispatch(CURRENT_LIST, {list})
router.back()
}
</script> </script>