This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/models/list.js
konrad 2705c1571e
All checks were successful
continuous-integration/drone/push Build is passing
Handle task relations the right way (#36)
2019-10-28 21:45:37 +00:00

97 lines
1.8 KiB
JavaScript

import AbstractModel from './abstractModel'
import TaskModel from './task'
import UserModel from './user'
export default class ListModel extends AbstractModel {
constructor(data) {
super(data)
// Make all tasks to task models
this.tasks = this.tasks.map(t => {
return new TaskModel(t)
})
this.owner = new UserModel(this.owner)
this.sortTasks()
}
// Default attributes that define the "empty" state.
defaults() {
return {
id: 0,
title: '',
description: '',
owner: UserModel,
tasks: [],
namespaceID: 0,
created: 0,
updated: 0,
}
}
////////
// Helpers
//////
/**
* Sorts all tasks according to their due date
* @returns {this}
*/
sortTasks() {
if (this.tasks === null || this.tasks === []) {
return
}
return this.tasks.sort(function(a,b) {
if (a.done < b.done)
return -1
if (a.done > b.done)
return 1
if (a.id > b.id)
return -1
if (a.id < b.id)
return 1
return 0
})
}
/**
* Adds a task to the task array of this list. Usually only used when creating a new task
* @param task
*/
addTaskToList(task) {
this.tasks.push(task)
this.sortTasks()
}
/**
* Gets a task by its ID by looping through all tasks.
* @param id
* @returns {TaskModel}
*/
getTaskByID(id) {
// TODO: Binary search?
for (const t in this.tasks) {
if (this.tasks[t].id === parseInt(id)) {
return this.tasks[t]
}
}
return {} // FIXME: This should probably throw something to make it clear to the user noting was found
}
/**
* Loops through all tasks and updates the one with the id it has
* @param task
*/
updateTaskByID(task) {
for (const t in this.tasks) {
if (this.tasks[t].id === task.id) {
this.tasks[t] = task
break
}
}
this.sortTasks()
}
}