forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskStore.ts
More file actions
285 lines (241 loc) · 7.35 KB
/
TaskStore.ts
File metadata and controls
285 lines (241 loc) · 7.35 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { autorun, makeAutoObservable } from 'mobx';
import { v4 as uuid } from 'uuid';
import TaskService from './TaskService';
import TaskModel, { ITimeRangeModel } from './models/TaskModel';
import {
Task,
TasksByProject,
} from '../../modules/tasks/models/TasksByProject';
import TreeModelHelper from '../../helpers/TreeModelHelper';
import BadgeService from '../BadgeService';
import rootStore, { RootStore } from '../RootStore';
import GaService from '../../services/gaService/GaService';
import {
EEventCategory,
ETasksEvents,
ETimeRangeEvents,
} from '../../services/gaService/EEvents';
import { DEFAULT_PROJECT_ID } from '../projects/models/ProjectModel';
import throttle from '../../helpers/Throttle';
import { THROTTLE_SAVE_JSON_MS } from '../../config';
export default class TaskStore {
tasks: TasksByProject = {};
activeTask: TaskModel | undefined;
versionHash = uuid();
private tasksService = new TaskService();
private interval: NodeJS.Timeout | undefined;
private saveInStorage = throttle(() => {
this.tasksService.save(this.tasks);
this.updateVersion();
}, THROTTLE_SAVE_JSON_MS);
constructor(private rootStore: RootStore) {
makeAutoObservable(this);
autorun(() => {
const profile = this.rootStore.settingsStore.settings.currentProfile;
if (profile) {
this.tasksService.setProfile(profile);
}
});
}
set(projectId: string, tasksInProject: TaskModel[]) {
this.tasks[projectId] = tasksInProject;
this.saveInStorage();
}
setTime(task: TaskModel, timeIndex: number, timeRange: ITimeRangeModel) {
task.time[timeIndex] = timeRange;
this.saveInStorage();
GaService.event(EEventCategory.TimeRange, ETimeRangeEvents.Update);
}
removeTime(task: TaskModel, timeIndex: number) {
if (task.active) {
this.stopTimer();
}
task.time.splice(timeIndex, 1); // TODO move to task
this.saveInStorage();
GaService.event(EEventCategory.TimeRange, ETimeRangeEvents.Delete);
}
getTasks(projectId: string): Task[] {
return this.tasks[projectId] || [];
}
getTaskByKey(taskKey: string): TaskModel | undefined {
function condition(task: TaskModel): boolean {
return task.key === taskKey;
}
for (const tasks of Object.values(this.tasks)) {
const found = TreeModelHelper.getItemRecursive(tasks, condition);
if (found) {
return found;
}
}
return undefined;
}
getTasksByDate(date: Date): TaskModel[] {
const result: TaskModel[] = [];
function condition(task: TaskModel): boolean {
return task.wasActiveInDay(date);
}
for (const tasks of Object.values(this.tasks)) {
TreeModelHelper.getFlatItemsRecursiveBase(tasks, condition, result);
}
return result;
}
add(task: TaskModel) {
const { projectId } = task;
if (!Array.isArray(this.tasks[projectId])) {
this.tasks[projectId] = [];
}
this.tasks[projectId] = [...this.tasks[projectId], task];
this.saveInStorage();
GaService.event(EEventCategory.Tasks, ETasksEvents.Create);
}
addToMyDay(task: TaskModel) {
task.inMyDay = new Date();
const pathToNode = TreeModelHelper.getPathToNode(task);
TreeModelHelper.copyItemsToTreeUnderProject(
rootStore.projectStore.get(task.projectId),
this.tasks[task.projectId],
// @ts-ignore
this.tasks[DEFAULT_PROJECT_ID.MyDay],
pathToNode
);
}
remove(task: TaskModel) {
function condition(_task: TaskModel) {
return _task.key === task.key;
}
if (task.active) {
this.stopTimer();
}
for (const projectKey in this.tasks) {
if (this.tasks.hasOwnProperty(projectKey)) {
this.tasks[projectKey] = TreeModelHelper.deleteItems(
this.tasks[projectKey],
condition
);
}
}
this.saveInStorage();
GaService.event(EEventCategory.Tasks, ETasksEvents.Delete);
}
removeProjectTasks(projectKey: string) {
delete this.tasks[projectKey];
this.saveInStorage();
}
startTimer(task: TaskModel) {
this.stopTimer(true);
this.activeTask = task;
task.start();
this.setupReminder(task);
this.saveInStorage();
}
stopTimer(silent?: boolean) {
if (this.activeTask) {
this.activeTask.stop();
}
if (!silent) {
this.setupReminder();
this.saveInStorage();
}
}
restore() {
this.tasks = this.tasksService.getAll();
this.findAndSetActiveTask();
this.setupReminder(this.activeTask);
}
getCheckedKeys(projectId: string): string[] {
const condition = (task: TaskModel) => task.checked;
return this.getTaskKeysByCondition(projectId, condition);
}
getExpandedKeys(projectId: string): string[] {
const condition = (task: TaskModel) => task.expanded;
return this.getTaskKeysByCondition(projectId, condition);
}
checkTasks(projectId: string, taskIds: string[]) {
function checkTaskFn(task: TaskModel, taskIds: string[]) {
task.checked = taskIds.includes(task.key);
}
if (Array.isArray(this.tasks[projectId])) {
TreeModelHelper.modifyItemsWithIdsRecursive<TaskModel>(
this.tasks[projectId],
taskIds,
checkTaskFn
);
this.saveInStorage();
}
GaService.event(EEventCategory.Tasks, ETasksEvents.Check);
}
markExpanded(projectId: string, taskIds: string[]) {
const markExpanded = (task: Task, taskIds: string[]) => {
if (task instanceof TaskModel) {
task.expanded = taskIds.includes(task.key);
}
};
if (Array.isArray(this.tasks[projectId])) {
TreeModelHelper.modifyItemsWithIdsRecursive<Task>(
this.tasks[projectId],
taskIds,
markExpanded
);
this.saveInStorage();
}
}
private getTaskKeysByCondition(
projectId: string,
condition: (task: TaskModel) => boolean
) {
if (Array.isArray(this.tasks[projectId])) {
return TreeModelHelper.getFlatItemsRecursive(
this.tasks[projectId],
condition
).map((task) => task.key);
}
return [];
}
private findAndSetActiveTask() {
for (const tasks of Object.values(this.tasks)) {
const found = this.findActiveTaskRecursive(tasks);
if (found) {
this.activeTask = found;
break;
}
}
}
private findActiveTaskRecursive(tasks: TaskModel[]): TaskModel | undefined {
function condition(task: TaskModel): boolean {
return task.active;
}
return TreeModelHelper.getItemRecursive(tasks, condition);
}
private setupReminder(task?: TaskModel) {
BadgeService.setBadge(!!task);
if (!this.rootStore.settingsStore.settings.showNotifications) {
return;
}
this.removeReminder();
if (task) {
console.log('Setup: Task in progress');
this.interval = setInterval(() => {
console.log('Task in progress');
new Notification('You are tracking time', {
body: `Task '${task.title}' in progress`,
});
}, 40 * 60 * 1000);
} else {
console.log('Setup: No tasks in progress');
this.interval = setInterval(() => {
console.log('No tasks in progress');
new Notification('You are not tracking time', {
body: 'There are not task that you track',
});
}, 15 * 60 * 1000);
}
}
removeReminder() {
if (this.interval !== undefined) {
clearInterval(this.interval);
}
}
private updateVersion() {
this.versionHash = uuid();
}
}