Make sure to use date objects everywhere where dealing with dates

This commit is contained in:
kolaente 2020-02-08 14:16:06 +01:00
parent fc17518e8c
commit 161f853361
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
31 changed files with 203 additions and 62 deletions

View file

@ -7,7 +7,7 @@ export default class AttachmentModel extends AbstractModel {
super(data)
this.created_by = new UserModel(this.created_by)
this.file = new FileModel(this.file)
this.created = new Date(this.created * 1000)
this.created = new Date(this.created)
}
defaults() {
@ -16,7 +16,7 @@ export default class AttachmentModel extends AbstractModel {
task_id: 0,
file: FileModel,
created_by: UserModel,
created: 0,
created: null,
}
}
}

View file

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

View file

@ -13,6 +13,9 @@ export default class LabelModel extends AbstractModel {
}
this.textColor = this.hasDarkColor() ? '#4a4a4a' : '#e5e5e5'
this.created_by = new UserModel(this.created_by)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
@ -25,8 +28,8 @@ export default class LabelModel extends AbstractModel {
listID: 0,
textColor: '',
created: 0,
updated: 0
created: null,
updated: null,
}
}

View file

@ -8,6 +8,9 @@ export default class ListModel extends AbstractModel {
super(data)
this.shared_by = new UserModel(this.shared_by)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
// Default attributes that define the "empty" state.
@ -20,8 +23,8 @@ export default class ListModel extends AbstractModel {
sharing_type: 0,
listID: 0,
created: 0,
updated: 0,
created: null,
updated: null,
}
}
}

View file

@ -13,8 +13,11 @@ export default class ListModel extends AbstractModel {
})
this.owner = new UserModel(this.owner)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
// Default attributes that define the "empty" state.
defaults() {
return {
@ -24,9 +27,9 @@ export default class ListModel extends AbstractModel {
owner: UserModel,
tasks: [],
namespaceID: 0,
created: 0,
updated: 0,
created: null,
updated: null,
}
}
}

View file

@ -10,6 +10,9 @@ export default class NamespaceModel extends AbstractModel {
return new ListModel(l)
})
this.owner = new UserModel(this.owner)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
// Default attributes that define the 'empty' state.
@ -21,8 +24,8 @@ export default class NamespaceModel extends AbstractModel {
owner: UserModel,
lists: [],
created: 0,
updated: 0,
created: null,
updated: null,
}
}
}

View file

@ -12,12 +12,12 @@ export default class TaskModel extends AbstractModel {
this.listID = Number(this.listID)
// Make date objects from timestamps
this.dueDate = this.parseDateIfNessecary(this.dueDate)
this.startDate = this.parseDateIfNessecary(this.startDate)
this.endDate = this.parseDateIfNessecary(this.endDate)
this.dueDate = new Date(this.dueDate)
this.startDate = new Date(this.startDate)
this.endDate = new Date(this.endDate)
this.reminderDates = this.reminderDates.map(d => {
return this.parseDateIfNessecary(d)
return new Date(d)
})
this.reminderDates.push(null) // To trigger the datepicker
@ -53,6 +53,9 @@ export default class TaskModel extends AbstractModel {
this.attachments = this.attachments.map(a => {
return new AttachmentModel(a)
})
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
@ -77,8 +80,8 @@ export default class TaskModel extends AbstractModel {
attachments: [],
createdBy: UserModel,
created: 0,
updated: 0,
created: null,
updated: null,
listID: 0, // Meta, only used when creating a new task
}
@ -88,19 +91,6 @@ export default class TaskModel extends AbstractModel {
// Helper functions
///////////////
/**
* Makes a js date object from a unix timestamp (in seconds).
* @param unixTimestamp
* @returns {*}
*/
parseDateIfNessecary(unixTimestamp) {
let dateobj = new Date(unixTimestamp * 1000)
if (unixTimestamp === 0) {
return null
}
return dateobj
}
/**
* Parses the "repeat after x seconds" from the task into a usable js object inside the task.
* This function should only be called from the constructor.

View file

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

View file

@ -1,10 +1,11 @@
import AbstractModel from './abstractModel'
import UserModel from "./user";
import UserModel from './user'
export default class TaskRelationModel extends AbstractModel {
constructor(data) {
super(data)
this.created_by = new UserModel(this.created_by)
this.created = new Date(this.created)
}
defaults() {
@ -15,7 +16,7 @@ export default class TaskRelationModel extends AbstractModel {
relation_kind: '',
created_by: UserModel,
created: 0,
created: null,
}
}
}

View file

@ -11,6 +11,9 @@ export default class TeamModel extends AbstractModel {
return new TeamMemberModel(m)
})
this.createdBy = new UserModel(this.createdBy)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
@ -22,8 +25,8 @@ export default class TeamModel extends AbstractModel {
right: 0,
createdBy: {},
created: 0,
updated: 0
created: null,
updated: null,
}
}
}

View file

@ -1,5 +1,5 @@
import TeamShareBaseModel from './teamShareBase'
import {merge} from "lodash";
import {merge} from 'lodash'
export default class TeamListModel extends TeamShareBaseModel {
defaults() {

View file

@ -5,13 +5,19 @@ 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 {
constructor(data) {
super(data)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
teamID: 0,
right: 0,
created: 0,
updated: 0
created: null,
updated: null
}
}
}

View file

@ -1,14 +1,20 @@
import AbstractModel from './abstractModel'
export default class UserModel extends AbstractModel {
constructor(data) {
super(data)
this.created = new Date(this.created)
this.updated = new Date(this.updated)
}
defaults() {
return {
id: 0,
avatarUrl: '',
email: '',
username: '',
created: 0,
updated: 0
created: null,
updated: null,
}
}
}

View file

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

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import AttachmentModel from '../models/attachment'
import moment from 'moment'
export default class AttachmentService extends AbstractService {
constructor() {
@ -10,6 +11,11 @@ export default class AttachmentService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
return model
}
uploadProgress = 0
useCreateInterceptor() {
@ -82,9 +88,4 @@ export default class AttachmentService extends AbstractService {
cancel()
})
}
processModel(model) {
model.created = Math.round(+new Date(model.created) / 1000)
return model
}
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import LabelModel from '../models/label'
import moment from 'moment'
export default class LabelService extends AbstractService {
constructor() {
@ -11,7 +12,13 @@ export default class LabelService extends AbstractService {
delete: '/labels/{id}',
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new LabelModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import LinkShareModel from '../models/linkShare'
import moment from 'moment'
export default class ListService extends AbstractService {
constructor() {
@ -11,6 +12,12 @@ export default class ListService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new LinkShareModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import ListModel from '../models/list'
import TaskService from './task'
import moment from 'moment'
export default class ListService extends AbstractService {
constructor() {
@ -11,7 +12,13 @@ export default class ListService extends AbstractService {
delete: '/lists/{id}',
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new ListModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import UserModel from '../models/user'
import moment from 'moment'
export default class ListUserService extends AbstractService {
constructor() {
@ -7,7 +8,13 @@ export default class ListUserService extends AbstractService {
getAll: '/lists/{listID}/listusers'
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new UserModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import NamespaceModel from '../models/namespace'
import moment from 'moment'
export default class NamespaceService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class NamespaceService extends AbstractService {
});
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new NamespaceModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import TaskModel from '../models/task'
import AttachmentService from './attachment'
import moment from 'moment'
export default class TaskService extends AbstractService {
constructor() {
@ -29,10 +30,12 @@ export default class TaskService extends AbstractService {
// Ensure the listID is an int
model.listID = Number(model.listID)
// Convert the date in a unix timestamp
model.dueDate = model.dueDate !== null ? Math.round(+new Date(model.dueDate) / 1000) : model.dueDate
model.startDate = model.startDate !== null ? Math.round(+new Date(model.startDate) / 1000): model.startDate
model.endDate = model.endDate !== null ? Math.round(+new Date(model.endDate) / 1000) : model.endDate
// Convert dates into an iso string
model.dueDate = moment(model.dueDate).toISOString()
model.startDate = moment(model.startDate).toISOString()
model.endDate = moment(model.endDate).toISOString()
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
// remove all nulls, these would create empty reminders
for (const index in model.reminderDates) {
@ -44,7 +47,7 @@ export default class TaskService extends AbstractService {
// Make normal timestamps from js dates
if(model.reminderDates.length > 0) {
model.reminderDates = model.reminderDates.map(r => {
return Math.round(+new Date(r) / 1000)
return moment(r).toISOString()
})
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import TaskAssigneeModel from '../models/taskAssignee'
import moment from 'moment'
export default class TaskAssigneeService extends AbstractService {
constructor() {
@ -8,7 +9,12 @@ export default class TaskAssigneeService extends AbstractService {
delete: '/tasks/{task_id}/assignees/{user_id}',
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
return model
}
modelFactory(data) {
return new TaskAssigneeModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import TaskModel from '../models/task'
import moment from 'moment'
export default class TaskCollectionService extends AbstractService {
constructor() {
@ -7,7 +8,13 @@ export default class TaskCollectionService extends AbstractService {
getAll: '/lists/{listID}/tasks',
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new TaskModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import TaskRelationModel from '../models/taskRelation'
import moment from 'moment'
export default class TaskRelationService extends AbstractService {
constructor() {
@ -8,7 +9,12 @@ export default class TaskRelationService extends AbstractService {
delete: '/tasks/{task_id}/relations',
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
return model
}
modelFactory(data) {
return new TaskRelationModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import TeamModel from '../models/team'
import moment from 'moment'
export default class TeamService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class TeamService extends AbstractService {
});
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new TeamModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import TeamListModel from '../models/teamList'
import TeamModel from '../models/team'
import moment from 'moment'
export default class TeamListService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class TeamListService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new TeamListModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import TeamMemberModel from '../models/teamMember'
import moment from 'moment'
export default class TeamMemberService extends AbstractService {
constructor() {
@ -8,7 +9,13 @@ export default class TeamMemberService extends AbstractService {
delete: '/teams/{teamID}/members/{id}', // "id" is the user id because we're intheriting from a normal user
});
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new TeamMemberModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import TeamNamespaceModel from '../models/teamNamespace'
import TeamModel from '../models/team'
import moment from 'moment'
export default class TeamNamespaceService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class TeamNamespaceService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new TeamNamespaceModel(data)
}

View file

@ -1,5 +1,6 @@
import AbstractService from './abstractService'
import UserModel from '../models/user'
import moment from 'moment'
export default class UserService extends AbstractService {
constructor() {
@ -7,7 +8,13 @@ export default class UserService extends AbstractService {
getAll: '/users'
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new UserModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import UserListModel from '../models/userList'
import UserModel from '../models/user'
import moment from 'moment'
export default class UserListService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class UserListService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new UserListModel(data)
}

View file

@ -1,6 +1,7 @@
import AbstractService from './abstractService'
import UserNamespaceModel from '../models/userNamespace'
import UserModel from '../models/user'
import moment from 'moment'
export default class UserNamespaceService extends AbstractService {
constructor() {
@ -12,6 +13,12 @@ export default class UserNamespaceService extends AbstractService {
})
}
processModel(model) {
model.created = moment(model.created).toISOString()
model.updated = moment(model.updated).toISOString()
return model
}
modelFactory(data) {
return new UserNamespaceModel(data)
}