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
144 lines (121 loc) · 3.66 KB
/
TaskStore.ts
File metadata and controls
144 lines (121 loc) · 3.66 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
import { makeAutoObservable } from 'mobx';
import TaskService from './TaskService';
import TaskModel, { ITimeRangeModel } from '../../models/TaskModel';
import TasksByProject from '../../models/TasksByProject';
import TreeModelStoreHelper from '../../base/TreeModelStoreHelper';
export default class TaskStore {
tasks: TasksByProject = {};
activeTask: TaskModel | undefined;
private tasksService = new TaskService();
constructor() {
makeAutoObservable(this);
}
set(projectId: string, tasks: TaskModel[]) {
this.tasks[projectId] = tasks;
this.tasksService.save(this.tasks);
}
setTime(task: TaskModel, timeIndex: number, timeRange: ITimeRangeModel) {
task.time[timeIndex] = timeRange;
this.tasksService.save(this.tasks);
}
getTasks(projectId: string): TaskModel[] {
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 = TreeModelStoreHelper.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)) {
TreeModelStoreHelper.getFlatItemsRecursive(tasks, condition, result);
}
return result;
}
add(task: TaskModel) {
const { projectId } = task;
if (!Array.isArray(this.tasks[projectId])) {
this.tasks[projectId] = [];
}
this.tasks[projectId].push(task);
this.tasks[projectId] = this.tasks[projectId].slice();
this.tasksService.save(this.tasks);
}
delete(task: TaskModel) {
task.setDeleted();
this.tasksService.save(this.tasks);
}
startTimer(task: TaskModel) {
if (this.activeTask) {
this.endTimer(this.activeTask);
}
this.activeTask = task;
task.start();
this.tasksService.save(this.tasks);
}
endTimer(task: TaskModel) {
this.activeTask = undefined;
task.end();
this.tasksService.save(this.tasks);
}
restore() {
this.tasks = this.tasksService.getAll();
this.findActiveTask();
}
getCheckedKeys(projectId: string): string[] {
function condition(task: TaskModel): boolean {
return task.checked;
}
if (Array.isArray(this.tasks[projectId])) {
const found: TaskModel[] = [];
TreeModelStoreHelper.getFlatItemsRecursive(
this.tasks[projectId],
condition,
found
);
return found.map((f) => f.key);
}
return [];
}
checkTasks(projectId: string, taskIds: string[]) {
if (Array.isArray(this.tasks[projectId])) {
this.checkTasksRecursive(this.tasks[projectId], taskIds);
}
this.tasksService.save(this.tasks);
}
private findActiveTask() {
Object.keys(this.tasks).find((projectId) => {
const found = this.findActiveTaskRecursive(this.tasks[projectId]);
if (found) {
this.activeTask = found;
return true;
}
return false;
});
}
private findActiveTaskRecursive(tasks: TaskModel[]): TaskModel | undefined {
function condition(task: TaskModel): boolean {
return task.active;
}
return TreeModelStoreHelper.getItemRecursive(tasks, condition);
}
private checkTasksRecursive(tasks: TaskModel[], taskIds: string[]) {
tasks.forEach((task) => {
task.checked = taskIds.includes(task.key);
if (Array.isArray(task.children)) {
this.checkTasksRecursive(task.children, taskIds);
}
});
}
}