WIP: feat: remove defaults function #1313

Closed
dpschen wants to merge 1 commits from dpschen:feature/remove-defaults-function-from-models into main
33 changed files with 223 additions and 370 deletions

View File

@ -19,16 +19,7 @@ export default class AbstractModel {
// Put all data in our model while overriding those with a value of null or undefined with their defaults
Object.assign(
this,
this.defaults(),
omitBy(data, isNil),
)
}
/**
* Default attributes that define the "empty" state.
* @return {{}}
*/
defaults() {
return {}
}
}

View File

@ -3,20 +3,16 @@ import UserModel from './user'
import FileModel from './file'
export default class AttachmentModel extends AbstractModel {
id = 0
taskId = 0
file = FileModel
createdBy = UserModel
created = null
constructor(data) {
super(data)
this.createdBy = new UserModel(this.createdBy)
this.file = new FileModel(this.file)
this.created = new Date(this.created)
}
defaults() {
return {
id: 0,
taskId: 0,
file: FileModel,
createdBy: UserModel,
created: null,
}
}
}

View File

@ -1,9 +1,5 @@
import AbstractModel from './abstractModel'
export default class AvatarModel extends AbstractModel {
defaults() {
return {
avatarProvider: '',
}
}
avatarProvider = ''
}

View File

@ -1,12 +1,8 @@
import AbstractModel from './abstractModel'
export default class BackgroundImageModel extends AbstractModel {
defaults() {
return {
id: 0,
url: '',
thumb: '',
info: {},
}
}
id = 0
url = ''
thumb = ''
info = {}
}

View File

@ -3,6 +3,18 @@ import UserModel from './user'
import TaskModel from './task'
export default class BucketModel extends AbstractModel {
id = 0
title = ''
listId = 0
limit = 0
tasks = []
isDoneBucket = false
position = 0
createdBy = null
created = null
updated = null
constructor(bucket) {
super(bucket)
@ -12,20 +24,4 @@ export default class BucketModel extends AbstractModel {
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
title: '',
listId: 0,
limit: 0,
tasks: [],
isDoneBucket: false,
position: 0,
createdBy: null,
created: null,
updated: null,
}
}
}

View File

@ -1,10 +1,6 @@
import AbstractModel from './abstractModel'
export default class EmailUpdateModel extends AbstractModel {
defaults() {
return {
newEmail: '',
passwort: '',
}
}
newEmail = ''
password = ''
dpschen marked this conversation as resolved Outdated

Wow. I think that parameter isn't even used like that - should be password https://try.vikunja.io/api/v1/docs#tag/user/paths/~1user~1settings~1email/post

Wow. I think that parameter isn't even used like that - should be `password` https://try.vikunja.io/api/v1/docs#tag/user/paths/~1user~1settings~1email/post

Is fixed by #1540

Is fixed by https://kolaente.dev/vikunja/frontend/pulls/1540
}

View File

@ -1,21 +1,17 @@
import AbstractModel from './abstractModel'
export default class FileModel extends AbstractModel {
id = 0
mime = ''
name = ''
size = 0
created = null
constructor(data) {
super(data)
this.created = new Date(this.created)
}
defaults() {
return {
id: 0,
mime: '',
name: '',
size: 0,
created: null,
}
}
getHumanSize() {
const sizes = {
0: 'B',

View File

@ -4,6 +4,17 @@ import {colorIsDark} from '@/helpers/color/colorIsDark'
const DEFAULT_LABEL_BACKGROUND_COLOR = 'e8e8e8'
export default class LabelModel extends AbstractModel {
id = 0
title = ''
hexColor = ''
description = ''
createdBy = UserModel
listId = 0
textColor = ''
created = null
updated = null
constructor(data) {
super(data)
// FIXME: this should be empty and be definied in the client.
@ -21,19 +32,4 @@ export default class LabelModel extends AbstractModel {
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
title: '',
hexColor: '',
description: '',
createdBy: UserModel,
listId: 0,
textColor: '',
created: null,
updated: null,
}
}
}

View File

@ -1,11 +1,7 @@
import AbstractModel from './abstractModel'
export default class LabelTask extends AbstractModel {
defaults() {
return {
id: 0,
taskId: 0,
labelId: 0,
}
}
id = 0
taskId = 0
labelId = 0
}

View File

@ -2,6 +2,17 @@ import AbstractModel from './abstractModel'
import UserModel from './user'
export default class ListModel extends AbstractModel {
id = 0
hash = ''
right = 0
sharedBy = UserModel
sharingType = 0
listId = 0
name = ''
password = ''
created = null
updated = null
constructor(data) {
// The constructor of AbstractModel handles all the default parsing.
@ -12,21 +23,4 @@ export default class ListModel extends AbstractModel {
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
// Default attributes that define the "empty" state.
defaults() {
return {
id: 0,
hash: '',
right: 0,
sharedBy: UserModel,
sharingType: 0,
listId: 0,
name: '',
password: '',
created: null,
updated: null,
}
}
}

View File

@ -5,6 +5,22 @@ import {getSavedFilterIdFromListId} from '@/helpers/savedFilter'
import SubscriptionModel from '@/models/subscription'
export default class ListModel extends AbstractModel {
id = 0
title = ''
description = ''
owner = UserModel
tasks = []
namespaceId = 0
isArchived = false
hexColor = ''
identifier = ''
backgroundInformation = null
isFavorite = false
subscription = null
position = 0
created = null
updated = null
constructor(data) {
super(data)
@ -28,28 +44,6 @@ export default class ListModel extends AbstractModel {
this.updated = new Date(this.updated)
}
// Default attributes that define the "empty" state.
defaults() {
return {
id: 0,
title: '',
description: '',
owner: UserModel,
tasks: [],
namespaceId: 0,
isArchived: false,
hexColor: '',
identifier: '',
backgroundInformation: null,
isFavorite: false,
subscription: null,
position: 0,
created: null,
updated: null,
}
}
isSavedFilter() {
return this.getSavedFilterId() > 0
}

View File

@ -2,16 +2,12 @@ import AbstractModel from './abstractModel'
import ListModel from './list'
export default class ListDuplicateModel extends AbstractModel {
listId = 0
namespaceId = 0
list = ListModel
constructor(data) {
super(data)
this.list = new ListModel(this.list)
}
defaults() {
return {
listId: 0,
namespaceId: 0,
list: ListModel,
}
}
}

View File

@ -4,6 +4,18 @@ import UserModel from './user'
import SubscriptionModel from '@/models/subscription'
export default class NamespaceModel extends AbstractModel {
id = 0
title = ''
description = ''
owner = UserModel
lists = []
isArchived = false
hexColor = ''
subscription = null
created = null
updated = null
constructor(data) {
super(data)
@ -24,21 +36,4 @@ export default class NamespaceModel extends AbstractModel {
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
// Default attributes that define the 'empty' state.
defaults() {
return {
id: 0,
title: '',
description: '',
owner: UserModel,
lists: [],
isArchived: false,
hexColor: '',
subscription: null,
created: null,
updated: null,
}
}
}

View File

@ -8,6 +8,12 @@ import TeamModel from '@/models/team'
import names from './constants/notificationNames.json'
export default class NotificationModel extends AbstractModel {
id = 0
name = ''
notification = null
read = false
readAt = null
constructor(data) {
super(data)
@ -41,16 +47,6 @@ export default class NotificationModel extends AbstractModel {
this.readAt = parseDateOrNull(this.readAt)
}
defaults() {
return {
id: 0,
name: '',
notification: null,
read: false,
readAt: null,
}
}
toText(user = null) {
let who = ''

View File

@ -1,17 +1,13 @@
import AbstractModel from './abstractModel'
export default class PasswordResetModel extends AbstractModel {
token = ''
newPassword = ''
email = ''
constructor(data) {
super(data)
this.token = localStorage.getItem('passwordResetToken')
}
defaults() {
return {
token: '',
newPassword: '',
email: '',
}
}
}

View File

@ -1,10 +1,6 @@
import AbstractModel from './abstractModel'
export default class PasswordUpdateModel extends AbstractModel {
defaults() {
return {
newPassword: '',
oldPassword: '',
}
}
newPassword = ''
oldPassword = ''
}

View File

@ -2,6 +2,23 @@ import AbstractModel from '@/models/abstractModel'
import UserModel from '@/models/user'
export default class SavedFilterModel extends AbstractModel {
id = 0
title = ''
description = ''
filters = {
sortBy: ['done', 'id'],
orderBy: ['asc', 'desc'],
filterBy: ['done'],
filterValue: ['false'],
filterComparator: ['equals'],
filterConcat: 'and',
filterIncludeNulls: true,
}
owner = {}
created = null
updated = null
constructor(data) {
super(data)
@ -11,27 +28,6 @@ export default class SavedFilterModel extends AbstractModel {
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
title: '',
description: '',
filters: {
sortBy: ['done', 'id'],
orderBy: ['asc', 'desc'],
filterBy: ['done'],
filterValue: ['false'],
filterComparator: ['equals'],
filterConcat: 'and',
filterIncludeNulls: true,
},
owner: {},
created: null,
updated: null,
}
}
/**
* Calculates the corresponding list id to this saved filter.
* This function matches the one in the api.

View File

@ -2,19 +2,15 @@ import AbstractModel from '@/models/abstractModel'
import UserModel from '@/models/user'
export default class SubscriptionModel extends AbstractModel {
id = 0
entity = ''
entityId = 0
created = null
user = {}
constructor(data) {
super(data)
this.user = new UserModel(this.user)
this.created = new Date(this.created)
}
defaults() {
return {
id: 0,
entity: '',
entityId: 0,
created: null,
user: {},
}
}
}

View File

@ -10,6 +10,41 @@ import {parseDateOrNull} from '@/helpers/parseDateOrNull'
const SUPPORTS_TRIGGERED_NOTIFICATION = 'Notification' in window && 'showTrigger' in Notification.prototype
export default class TaskModel extends AbstractModel {
id = 0
title = ''
description = ''
done = false
doneAt = null
priority = 0
labels = []
assignees = []
dueDate = 0
startDate = 0
endDate = 0
repeatAfter = 0
repeatFromCurrentDate = false
repeatMode = REPEAT_MODE_DEFAULT
reminderDates = []
parentTaskId = 0
hexColor = ''
percentDone = 0
relatedTasks = {}
attachments = []
identifier = ''
index = 0
isFavorite = false
subscription = null
position = 0
kanbanPosition = 0
createdBy = UserModel
created = null
updated = null
listId = 0 // Meta, only used when creating a new task
defaultColor = '198CFF'
@ -76,45 +111,6 @@ export default class TaskModel extends AbstractModel {
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
title: '',
description: '',
done: false,
doneAt: null,
priority: 0,
labels: [],
assignees: [],
dueDate: 0,
startDate: 0,
endDate: 0,
repeatAfter: 0,
repeatFromCurrentDate: false,
repeatMode: REPEAT_MODE_DEFAULT,
reminderDates: [],
parentTaskId: 0,
hexColor: '',
percentDone: 0,
relatedTasks: {},
attachments: [],
identifier: '',
index: 0,
isFavorite: false,
subscription: null,
position: 0,
kanbanPosition: 0,
createdBy: UserModel,
created: null,
updated: null,
listId: 0, // Meta, only used when creating a new task
}
}
getTextIdentifier() {
if (this.identifier === '') {
return `#${this.index}`

View File

@ -1,16 +1,12 @@
import AbstractModel from './abstractModel'
export default class TaskAssigneeModel extends AbstractModel {
created = null
userId = 0
taskId = 0
constructor(data) {
super(data)
this.created = new Date(this.created)
}
defaults() {
return {
created: null,
userId: 0,
taskId: 0,
}
}
}

View File

@ -2,21 +2,17 @@ import AbstractModel from './abstractModel'
import UserModel from './user'
export default class TaskCommentModel extends AbstractModel {
id = 0
taskId = 0
comment = ''
author = UserModel
created = null
update = null
constructor(data) {
super(data)
this.author = new UserModel(this.author)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
taskId: 0,
comment: '',
author: UserModel,
created: null,
update: null,
}
}
}

View File

@ -2,21 +2,17 @@ import AbstractModel from './abstractModel'
import UserModel from './user'
export default class TaskRelationModel extends AbstractModel {
id = 0
otherTaskId = 0
taskId = 0
relationKind = ''
createdBy = UserModel
created = null
constructor(data) {
super(data)
this.createdBy = new UserModel(this.createdBy)
this.created = new Date(this.created)
}
defaults() {
return {
id: 0,
otherTaskId: 0,
taskId: 0,
relationKind: '',
createdBy: UserModel,
created: null,
}
}
}

View File

@ -3,6 +3,16 @@ import UserModel from './user'
import TeamMemberModel from './teamMember'
export default class TeamModel extends AbstractModel {
id = 0
name = ''
description = ''
members = []
right = 0
createdBy = {}
created = null
updated = null
constructor(data) {
super(data)
@ -15,18 +25,4 @@ export default class TeamModel extends AbstractModel {
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
name: '',
description: '',
members: [],
right: 0,
createdBy: {},
created: null,
updated: null,
}
}
}

View File

@ -1,10 +1,5 @@
import TeamShareBaseModel from './teamShareBase'
export default class TeamListModel extends TeamShareBaseModel {
defaults() {
return {
...super.defaults(),
listId: 0,
}
}
listId = 0
}

View File

@ -1,11 +1,6 @@
import UserModel from './user'
export default class TeamMemberModel extends UserModel {
defaults() {
return {
...super.defaults(),
admin: false,
teamId: 0,
}
}
admin = false
teamId = 0
}

View File

@ -1,10 +1,5 @@
import TeamShareBaseModel from './teamShareBase'
export default class TeamNamespaceModel extends TeamShareBaseModel {
defaults() {
return {
...super.defaults(),
namespaceId: 0,
}
}
namespaceId = 0
}

View File

@ -5,19 +5,15 @@ import AbstractModel from './abstractModel'
* It is extended in a way so it can be used for namespaces as well for lists.
*/
export default class TeamShareBaseModel extends AbstractModel {
teamId = 0
right = 0
created = null
updated = null
constructor(data) {
super(data)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
teamId: 0,
right: 0,
created: null,
updated: null,
}
}
}

View File

@ -1,11 +1,7 @@
import AbstractModel from './abstractModel'
export default class TotpModel extends AbstractModel {
defaults() {
return {
secret: '',
enabled: false,
url: '',
}
}
secret = ''
enabled = false
url = ''
}

View File

@ -2,6 +2,14 @@ import AbstractModel from './abstractModel'
import UserSettingsModel from '@/models/userSettings'
export default class UserModel extends AbstractModel {
id = 0
email = ''
username = ''
name = ''
created = null
updated = null
settings = null
constructor(data) {
super(data)
@ -13,18 +21,6 @@ export default class UserModel extends AbstractModel {
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
email: '',
username: '',
name: '',
created: null,
updated: null,
settings: null,
}
}
getAvatarUrl(size = 50) {
return `${window.API_URL}/avatar/${this.username}?size=${size}`
}

View File

@ -2,10 +2,5 @@ import UserShareBaseModel from './userShareBase'
// This class extends the user share model with a 'rights' parameter which is used in sharing
export default class UserListModel extends UserShareBaseModel {
defaults() {
return {
...super.defaults(),
listId: 0,
}
}
listId = 0
}

View File

@ -2,10 +2,5 @@ import UserShareBaseModel from './userShareBase'
// This class extends the user share model with a 'rights' parameter which is used in sharing
export default class UserNamespaceModel extends UserShareBaseModel {
defaults() {
return {
...super.defaults(),
namespaceId: 0,
}
}
namespaceId = 0
}

View File

@ -2,15 +2,11 @@
import AbstractModel from './abstractModel'
export default class UserSettingsModel extends AbstractModel {
defaults() {
return {
name: '',
emailRemindersEnabled: true,
discoverableByName: false,
discoverableByEmail: false,
overdueTasksRemindersEnabled: true,
defaultListId: undefined,
weekStart: 0,
}
}
name = ''
emailRemindersEnabled = true
discoverableByName = false
discoverableByEmail = false
overdueTasksRemindersEnabled = true
defaultListId = undefined
weekStart = 0
}

View File

@ -1,19 +1,15 @@
import AbstractModel from './abstractModel'
export default class UserShareBaseModel extends AbstractModel {
userId = ''
right = 0
created = null
updated = null
constructor(data) {
super(data)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
userId: '',
right: 0,
created: null,
updated: null,
}
}
}