feat(task wizard): add quick add magic support

This commit is contained in:
kolaente 2022-09-21 21:56:04 +02:00
parent ec01f516a8
commit bf027397b5
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 57 additions and 32 deletions

View file

@ -0,0 +1,23 @@
import UserService from '@/services/user'
import {findPropertyByValue} from '@/helpers/findPropertyByValue'
// Check if the user exists
function validateUsername(users: IUser[], username: IUser['username']) {
return findPropertyByValue(users, 'username', username)
}
export async function findAssignees(parsedTaskAssignees: string[]) {
if (parsedTaskAssignees.length <= 0) {
return []
}
const userService = new UserService()
const assignees = parsedTaskAssignees.map(async a => {
const users = await userService.getAll({}, {s: a})
return validateUsername(users, a)
})
const validatedUsers = await Promise.all(assignees)
return validatedUsers.filter((item) => Boolean(item))
}

View file

@ -0,0 +1,7 @@
// IDEA: maybe use a small fuzzy search here to prevent errors
export function findPropertyByValue(object, key, value) {
return Object.values(object).find(
(l) => l[key]?.toLowerCase() === value.toLowerCase(),
)
}

View file

@ -5,7 +5,6 @@ import {formatISO} from 'date-fns'
import TaskService from '@/services/task' import TaskService from '@/services/task'
import TaskAssigneeService from '@/services/taskAssignee' import TaskAssigneeService from '@/services/taskAssignee'
import LabelTaskService from '@/services/labelTask' import LabelTaskService from '@/services/labelTask'
import UserService from '@/services/user'
import {HAS_TASKS} from '../mutation-types' import {HAS_TASKS} from '../mutation-types'
import {setLoading} from '../helper' import {setLoading} from '../helper'
@ -28,18 +27,8 @@ import type { RootStoreState, TaskState } from '@/store/types'
import {useLabelStore} from '@/stores/labels' import {useLabelStore} from '@/stores/labels'
import {useListStore} from '@/stores/lists' import {useListStore} from '@/stores/lists'
import {playPop} from '@/helpers/playPop' import {playPop} from '@/helpers/playPop'
import {findPropertyByValue} from '@/helpers/findPropertyByValue'
// IDEA: maybe use a small fuzzy search here to prevent errors import {findAssignees} from '@/helpers/findAssignees'
function findPropertyByValue(object, key, value) {
return Object.values(object).find(
(l) => l[key]?.toLowerCase() === value.toLowerCase(),
)
}
// Check if the user exists
function validateUsername(users: IUser[], username: IUser['username']) {
return findPropertyByValue(users, 'username', username)
}
// Check if the label exists // Check if the label exists
function validateLabel(labels: ILabel[], label: ILabel) { function validateLabel(labels: ILabel[], label: ILabel) {
@ -57,22 +46,6 @@ async function addLabelToTask(task: ITask, label: ILabel) {
return response return response
} }
async function findAssignees(parsedTaskAssignees) {
if (parsedTaskAssignees.length <= 0) {
return []
}
const userService = new UserService()
const assignees = parsedTaskAssignees.map(async a => {
const users = await userService.getAll({}, {s: a})
return validateUsername(users, a)
})
const validatedUsers = await Promise.all(assignees)
return validatedUsers.filter((item) => Boolean(item))
}
const tasksStore : Module<TaskState, RootStoreState>= { const tasksStore : Module<TaskState, RootStoreState>= {
namespaced: true, namespaced: true,
state: () => ({}), state: () => ({}),

View file

@ -12,6 +12,7 @@
v-model="newTask.title" v-model="newTask.title"
v-focus v-focus
/> />
<QuickAddMagic class="ml-4"/>
<BaseButton <BaseButton
v-if="!descriptionFormVisible" v-if="!descriptionFormVisible"
@click="() => descriptionFormVisible = true" @click="() => descriptionFormVisible = true"
@ -32,14 +33,21 @@
<script lang="ts" setup> <script lang="ts" setup>
import {computed, ref} from 'vue' import {computed, ref} from 'vue'
import {useI18n} from 'vue-i18n' import {useI18n} from 'vue-i18n'
import {formatISO} from 'date-fns'
import CreateEdit from '@/components/misc/create-edit.vue' import CreateEdit from '@/components/misc/create-edit.vue'
import type {ITask} from '@/modelTypes/ITask'
import TaskModel from '@/models/task'
import Editor from '@/components/input/AsyncEditor' import Editor from '@/components/input/AsyncEditor'
import BaseButton from '@/components/base/BaseButton.vue' import BaseButton from '@/components/base/BaseButton.vue'
import QuickAddMagic from '@/components/tasks/partials/quick-add-magic.vue'
import type {ITask} from '@/modelTypes/ITask'
import TaskModel from '@/models/task'
import TaskService from '@/services/task' import TaskService from '@/services/task'
import {useRouter} from 'vue-router' import {useRouter} from 'vue-router'
import {useListStore} from '@/stores/lists' import {useListStore} from '@/stores/lists'
import {getQuickAddMagicMode} from '@/helpers/quickAddMagicMode'
import {parseTaskText} from '@/modules/parseTaskText'
import {findAssignees} from '@/helpers/findAssignees'
const listStore = useListStore() const listStore = useListStore()
const router = useRouter() const router = useRouter()
@ -59,9 +67,23 @@ const descriptionFormVisible = ref(false)
const newTask = ref<ITask>(new TaskModel({})) const newTask = ref<ITask>(new TaskModel({}))
const taskService = ref(new TaskService()) const taskService = ref(new TaskService())
const parsedTask = computed(() => parseTaskText(newTask.value.title, getQuickAddMagicMode()))
async function create() { async function create() {
newTask.value.listId = props.listId newTask.value.listId = props.listId
const task = await taskService.value.create(newTask.value) newTask.value.title = parsedTask.value.text
const assignees = await findAssignees(parsedTask.value.assignees)
const dueDate = parsedTask.value.date !== null ? formatISO(parsedTask.date) : null
const finalTask = new TaskModel({
...newTask.value,
title: parsedTask.value.text,
dueDate,
priority: parsedTask.value.priority,
assignees: parsedTask.value.assignees,
})
const task = await taskService.value.create(finalTask)
return router.push({name: 'task.detail', params: {id: task.id}}) return router.push({name: 'task.detail', params: {id: task.id}})
} }
</script> </script>