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
107 lines (94 loc) · 2.58 KB
/
TaskModel.ts
File metadata and controls
107 lines (94 loc) · 2.58 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
import { action, computed, makeObservable, observable } from 'mobx';
import isSameDay from 'date-fns/isSameDay';
import AbstractModel from '../base/AbstractModel';
import { ITreeItem } from '../types/ITreeItem';
interface IJsonTaskModel extends ITreeItem<IJsonTaskModel> {
projectId: string;
checked: boolean;
active: boolean;
time: string[][];
datesInProgress: string[];
children: IJsonTaskModel[];
details: string[];
}
export default class TaskModel extends AbstractModel {
key: string = '';
title: string = '';
children: TaskModel[] = [];
projectId: string = '';
checked: boolean = false;
active: boolean = false;
time: Date[][] = [];
datesInProgress: Date[] = [];
details: string = '';
constructor(props: IJsonTaskModel) {
super();
this.load(props);
this.children = props.children?.map((json) => new TaskModel(json)) || [];
this.time = props.time?.map((range) => range.map((t) => new Date(t))) || [];
this.datesInProgress =
props.datesInProgress?.map((date) => new Date(date)) || [];
makeObservable(this, {
key: observable,
title: observable,
children: observable,
projectId: observable,
checked: observable,
active: observable,
time: observable,
datesInProgress: observable,
details: observable,
duration: computed,
setTitle: action,
setDetails: action,
start: action,
end: action,
});
}
get duration() {
return this.time.reduce((prev: number, range: Date[]) => {
if (range.length > 0) {
const duration =
(range.length === 2 ? range[1].getTime() : Date.now()) -
range[0].getTime();
return prev + duration;
}
return prev;
}, 0);
}
setTitle(title: string) {
this.title = title;
}
setDetails(details: string) {
this.details = details;
}
setChecked(checked: boolean) {
this.checked = checked;
}
start() {
this.active = true;
this.addDateWhenWasInProgress(new Date());
this.time.forEach((range) => {
if (range.length === 1) {
range.push(new Date());
}
});
this.time.push([new Date()]);
}
end() {
if (this.active) {
this.active = false;
const range = this.time[this.time.length - 1];
range.push(new Date());
}
}
wasActiveInDay(date: Date): boolean {
return this.datesInProgress.some((d) => isSameDay(d, date));
}
private addDateWhenWasInProgress(date: Date) {
const found = this.datesInProgress.find((d) => isSameDay(d, date));
if (!found) {
this.datesInProgress.push(date);
}
}
}