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
136 lines (120 loc) · 3.24 KB
/
TaskModel.ts
File metadata and controls
136 lines (120 loc) · 3.24 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
import { action, computed, makeObservable, observable } from 'mobx';
import isSameDay from 'date-fns/isSameDay';
import AbstractModel from '../base/AbstractModel';
import { ITreeItem } from '../types/ITreeItem';
export interface IJsonTimeRangeModel {
start: string;
end?: string;
description?: string;
}
export interface ITimeRangeModel {
start: Date;
end?: Date;
description?: string;
}
interface IJsonTaskModel extends ITreeItem<IJsonTaskModel> {
projectId: string;
checked: boolean;
active: boolean;
time: string[][] | IJsonTimeRangeModel[];
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: ITimeRangeModel[] = [];
datesInProgress: Date[] = [];
details: string = '';
deleted: boolean = false;
constructor(props: IJsonTaskModel) {
super();
this.load(props);
this.children = props.children?.map((json) => new TaskModel(json)) || [];
this.time =
props.time?.map<ITimeRangeModel>(
(range: string[] | IJsonTimeRangeModel) => {
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: undefined,
};
}
}
) || [];
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,
stop: action,
});
}
get duration() {
return this.time.reduce((prev: number, range: ITimeRangeModel) => {
const { start, end } = range;
const duration = (end ? end.getTime() : Date.now()) - start.getTime();
return prev + duration;
}, 0);
}
setTitle(title: string) {
this.title = title;
}
setDetails(details: string) {
this.details = details;
}
setChecked(checked: boolean) {
this.checked = checked;
}
setDeleted() {
this.deleted = true;
}
start() {
this.active = true;
this.addDateWhenWasInProgress(new Date());
this.time.push({
start: new Date(),
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 found = this.datesInProgress.find((d) => isSameDay(d, date));
if (!found) {
this.datesInProgress.push(date);
}
}
}