Add the actual parsing when creating a task

This commit is contained in:
kolaente 2021-06-28 22:33:58 +02:00
parent 58cd49bcfc
commit d924bc4edb
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 26 additions and 79 deletions

View File

@ -1,4 +1,8 @@
export const parseDateOrNull = date => {
if (date instanceof Date) {
return date
}
if (date && !date.startsWith('0001')) {
return new Date(date)
}

View File

@ -71,7 +71,7 @@ const getPriority = text => {
for (const p of ps) {
for (const pi in priorities) {
if (priorities[pi] === parseInt(p)) {
return p
return parseInt(p)
}
}
}

View File

@ -9,7 +9,7 @@
* @returns {*}
*/
export const replaceAll = (str, search, replace) => {
const esc = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const esc = search.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
const reg = new RegExp(esc, 'ig');
return str.replace(reg, replace);
}

View File

@ -109,7 +109,8 @@
</div>
<card
v-if="isTaskEdit"
class="taskedit mt-0" :title="$t('list.list.editTask')" :has-close="true" @close="() => isTaskEdit = false"
class="taskedit mt-0" :title="$t('list.list.editTask')" :has-close="true"
@close="() => isTaskEdit = false"
:shadow="false">
<edit-task :task="taskEditTask"/>
</card>
@ -165,6 +166,7 @@ import TaskModel from '../../../models/task'
import LabelTaskService from '../../../services/labelTask'
import LabelTask from '../../../models/labelTask'
import LabelModel from '../../../models/label'
import UserModel from '@/models/user'
import EditTask from '../../../components/tasks/edit-task'
import SingleTaskInList from '../../../components/tasks/partials/singleTaskInList'
@ -173,8 +175,9 @@ import {saveListView} from '@/helpers/saveListView'
import Rights from '../../../models/rights.json'
import {mapState} from 'vuex'
import FilterPopup from '@/components/list/partials/filter-popup'
import {HAS_TASKS} from '@/store/mutation-types'
import Nothing from '@/components/misc/nothing'
import {parseTaskText} from '@/helpers/parseTaskText'
import {formatISO} from 'date-fns'
export default {
name: 'List',
@ -229,57 +232,29 @@ export default {
}
this.showError = false
let task = new TaskModel({title: this.newTaskText, listId: this.$route.params.listId})
const parsedTask = parseTaskText(this.newTaskText)
const task = new TaskModel({
title: parsedTask.text,
listId: this.$route.params.listId,
dueDate: formatISO(parsedTask.date), // I don't know why, but it all goes up in flames when I just pass in the date normally.
priority: parsedTask.priority,
assignees: parsedTask.assignees.map(a => new UserModel({username: a}))
})
this.taskService.create(task)
.then(task => {
this.tasks.push(task)
this.sortTasks()
this.newTaskText = ''
// Unlike a proper programming language, Javascript only knows references to objects and does not
// allow you to control what is a reference and what isnt. Because of this we can't just add
// all labels to the task they belong to right after we found and added them to the task since
// the task update method also ensures all data the api sees has the right format. That means
// it processes labels. That processing changes the date format and the label color and makes
// the label pretty much unusable for everything else. Normally, this is not a big deal, because
// the labels on a task get thrown away anyway and replaced with the new models from the api
// when we get the updated answer back. However, in this specific case because we're passing a
// label we obtained from vuex that reference is kept and not thrown away. The task itself gets
// a new label object - you won't notice the bad reference until you want to add the same label
// again and notice it doesn't have a color anymore.
// I think this is what happens: (or rather would happen without the hack I've put in)
// 1. Query the store for a label which matches the name
// 2. Find one - remember, we get only a *reference* to the label from the store, not a new label object.
// (Now there's *two* places with a reference to the same label object: in the store and in the
// variable which holds the label from the search in the store)
// 3. .push the label to the task
// 4. Update the task to remove the labels from the name
// 4.1. The task update processes all labels belonging to that task, changing attributes of our
// label in the process. Because this is a reference, it is also "updated" in the store.
// 5. Get an api response back. The service handler now creates a new label object for all labels
// returned from the api. It will throw away all references to the old label in the process.
// 6. Now we have two objects with the same label data: The old one we originally obtained from
// the store and the one that was created when parsing the api response. The old one was
// modified before sending the api request and thus, our store which still holds a reference
// to the old label now contains old data.
// I guess this is the point where normally the GC would come in and collect the old label
// object if the store wouldn't still hold a reference to it.
//
// Now, as a workaround, I'm putting all new labels added to that task in this separate variable to
// add them only after the task was updated to circumvent the task update service processing the
// label before sending it. Feels more hacky than it probably is.
const newLabels = []
// Check if the task has words starting with ~ in the title and make them to labels
const parts = task.title.split(' ~')
// The first element will always contain the title, even if there is no occurrence of ~
if (parts.length > 1) {
if (parsedTask.labels.length > 1) {
// First, create an unresolved promise for each entry in the array to wait
// until all labels are added to update the task title once again
let labelAddings = []
let labelAddsToWaitFor = []
parts.forEach((p, index) => {
parsedTask.labels.forEach((p, index) => {
if (index < 1) {
return
}
@ -290,19 +265,7 @@ export default {
})
// Then do everything that is involved in finding, creating and adding the label to the task
parts.forEach((p, index) => {
if (index < 1) {
return
}
// The part up until the next space
const labelTitle = p.split(' ')[0]
// Don't create an empty label
if (labelTitle === '') {
return
}
parsedTask.labels.forEach((labelTitle, index) => {
// Check if the label exists
const label = Object.values(this.$store.state.labels.labels).find(l => {
return l.title.toLowerCase() === labelTitle.toLowerCase()
@ -316,11 +279,7 @@ export default {
})
this.labelTaskService.create(labelTask)
.then(result => {
newLabels.push(label)
// Remove the label text from the task title
task.title = task.title.replace(` ~${labelTitle}`, '')
task.labels.push(label)
// Make the promise done (the one with the index 0 does not exist)
labelAddings[index - 1].resolve(result)
})
@ -338,11 +297,7 @@ export default {
})
this.labelTaskService.create(labelTask)
.then(result => {
newLabels.push(res)
// Remove the label text from the task title
task.title = task.title.replace(` ~${labelTitle}`, '')
task.labels.push(res)
// Make the promise done (the one with the index 0 does not exist)
labelAddings[index - 1].resolve(result)
})
@ -356,20 +311,8 @@ export default {
}
})
// This waits to update the task until all labels have been added and the title has
// been modified to remove each label text
// This waits until all labels are created and added to the task
Promise.all(labelAddsToWaitFor)
.then(() => {
this.taskService.update(task)
.then(updatedTask => {
updatedTask.labels = newLabels
this.updateTasks(updatedTask)
this.$store.commit(HAS_TASKS, true)
})
.catch(e => {
this.error(e)
})
})
}
})
.catch(e => {