diff --git a/src/helpers/findAssignees.ts b/src/helpers/findAssignees.ts new file mode 100644 index 00000000..ca557cfb --- /dev/null +++ b/src/helpers/findAssignees.ts @@ -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)) +} + diff --git a/src/helpers/findPropertyByValue.ts b/src/helpers/findPropertyByValue.ts new file mode 100644 index 00000000..0bd39f50 --- /dev/null +++ b/src/helpers/findPropertyByValue.ts @@ -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(), + ) +} + diff --git a/src/store/modules/tasks.ts b/src/store/modules/tasks.ts index d18e218e..9ee71684 100644 --- a/src/store/modules/tasks.ts +++ b/src/store/modules/tasks.ts @@ -5,7 +5,6 @@ import {formatISO} from 'date-fns' import TaskService from '@/services/task' import TaskAssigneeService from '@/services/taskAssignee' import LabelTaskService from '@/services/labelTask' -import UserService from '@/services/user' import {HAS_TASKS} from '../mutation-types' import {setLoading} from '../helper' @@ -28,18 +27,8 @@ import type { RootStoreState, TaskState } from '@/store/types' import {useLabelStore} from '@/stores/labels' import {useListStore} from '@/stores/lists' import {playPop} from '@/helpers/playPop' - -// IDEA: maybe use a small fuzzy search here to prevent errors -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) -} +import {findPropertyByValue} from '@/helpers/findPropertyByValue' +import {findAssignees} from '@/helpers/findAssignees' // Check if the label exists function validateLabel(labels: ILabel[], label: ILabel) { @@ -57,22 +46,6 @@ async function addLabelToTask(task: ITask, label: ILabel) { 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= { namespaced: true, state: () => ({}), diff --git a/src/views/tasks/CreateTask.vue b/src/views/tasks/CreateTask.vue index 9a97d087..2edf99f1 100644 --- a/src/views/tasks/CreateTask.vue +++ b/src/views/tasks/CreateTask.vue @@ -12,6 +12,7 @@ v-model="newTask.title" v-focus /> + import {computed, ref} from 'vue' import {useI18n} from 'vue-i18n' +import {formatISO} from 'date-fns' + 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 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 {useRouter} from 'vue-router' import {useListStore} from '@/stores/lists' +import {getQuickAddMagicMode} from '@/helpers/quickAddMagicMode' +import {parseTaskText} from '@/modules/parseTaskText' +import {findAssignees} from '@/helpers/findAssignees' const listStore = useListStore() const router = useRouter() @@ -59,9 +67,23 @@ const descriptionFormVisible = ref(false) const newTask = ref(new TaskModel({})) const taskService = ref(new TaskService()) +const parsedTask = computed(() => parseTaskText(newTask.value.title, getQuickAddMagicMode())) + async function create() { 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}}) }