forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskModel.ts
More file actions
165 lines (147 loc) · 4.17 KB
/
TaskModel.ts
File metadata and controls
165 lines (147 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { action, computed, makeObservable, observable } from 'mobx';
import { isSameDay, startOfDay } from 'date-fns';
import AbstractModel from '../../../base/AbstractModel';
import { ITreeItemWithParent } from '../../../types/ITreeItem';
export interface IJsonTimeRangeModel {
start: string;
end?: string;
description?: string;
}
export interface ITimeRangeModel {
start: Date;
end?: Date;
description?: string;
}
export interface IJsonTaskModel extends ITreeItemWithParent {
children?: IJsonTaskModel[];
projectId?: string;
checked?: boolean;
active?: boolean;
expanded?: boolean;
inMyDay?: string;
time?: string[][] | IJsonTimeRangeModel[];
datesInProgress?: string[];
details?: string[];
}
const parseTimeRageItems = (
timeItems: (string[] | IJsonTimeRangeModel | ITimeRangeModel)[]
) => {
return timeItems.map(
(range: string[] | IJsonTimeRangeModel | ITimeRangeModel) => {
if (Array.isArray(range)) {
return {
start: new Date(range[0]),
end: range[1] ? new Date(range[1]) : undefined,
description: undefined,
};
} else {
return {
start: new Date(range.start),
end: range.end ? new Date(range.end) : undefined,
description: range.description,
};
}
}
);
};
export default class TaskModel extends AbstractModel
implements ITreeItemWithParent {
key: string = '';
title: string = '';
children?: TaskModel[] = [];
parent: TaskModel | undefined = undefined;
projectId: string = '';
checked: boolean = false;
active: boolean = false;
expanded: boolean = true;
inMyDay: Date | null = null;
time: ITimeRangeModel[] = [];
datesInProgress: Date[] = [];
details: string = '';
withoutActions: boolean = false; // TODO make a new class
constructor(props: IJsonTaskModel | TaskModel) {
super();
const newProps = {
...props,
children: props.children?.map((json) => new TaskModel(json)) || [],
time: props.time ? parseTimeRageItems(props.time) : [],
datesInProgress:
props.datesInProgress?.map((date) => new Date(date)) || [],
inMyDay: props?.inMyDay ? new Date(props?.inMyDay) : null,
};
this.load(newProps);
makeObservable(this, {
key: observable,
title: observable,
children: observable,
// parent: none
projectId: observable,
checked: observable,
active: observable,
expanded: observable,
inMyDay: observable,
time: observable,
datesInProgress: observable,
details: observable,
withoutActions: observable,
duration: computed,
setTitle: action,
setDetails: action,
start: action,
stop: action,
});
}
get duration() {
return this.time.reduce((acc: number, range: ITimeRangeModel) => {
const { start, end } = range;
const duration = (end ? end.getTime() : Date.now()) - start.getTime();
return acc + duration;
}, 0);
}
getDurationByDate(date: Date) {
return this.time.reduce((acc: number, range: ITimeRangeModel) => {
const { start, end } = range;
let duration = 0;
if (isSameDay(start, date)) {
duration = (end ? end.getTime() : Date.now()) - start.getTime();
}
return acc + duration;
}, 0);
}
setTitle(title: string) {
this.title = title;
}
setDetails(details: string) {
this.details = details;
}
setChecked(checked: boolean) {
this.checked = checked;
}
start() {
this.active = true;
const dateNow = new Date();
this.addDateWhenWasInProgress(dateNow);
this.time.push({
start: dateNow,
end: undefined,
description: undefined,
});
}
stop() {
if (this.active) {
this.active = false;
const range = this.time[this.time.length - 1];
range.end = new Date();
}
}
wasActiveInDay(date: Date): boolean {
return this.datesInProgress.some((d) => isSameDay(d, date));
}
private addDateWhenWasInProgress(date: Date) {
const normalDate = startOfDay(date);
const found = this.datesInProgress.find((d) => isSameDay(d, normalDate));
if (!found) {
this.datesInProgress = [...this.datesInProgress, normalDate];
}
}
}