Skip to content

Commit 09f74c3

Browse files
author
Dmitry Yadrikhinsky
committed
[Timer] Start/end into model
1 parent f7703d4 commit 09f74c3

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/models/TaskModel.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { action, computed, makeObservable, observable } from 'mobx';
2+
13
import AbstractModel from '../base/AbstractModel';
24
import { ITreeItem } from '../types/ITreeItem';
3-
import { computed, makeObservable, observable } from 'mobx';
45

56
interface IJsonTaskModel extends ITreeItem<IJsonTaskModel> {
67
projectId: string;
@@ -33,9 +34,27 @@ export default class TaskModel extends AbstractModel {
3334
active: observable,
3435
time: observable,
3536
duration: computed,
37+
start: action,
38+
end: action,
3639
});
3740
}
3841

42+
start() {
43+
this.active = true;
44+
this.time.forEach((range) => {
45+
if (range.length === 1) {
46+
range.push(new Date());
47+
}
48+
});
49+
this.time.push([new Date()]);
50+
}
51+
52+
end() {
53+
this.active = false;
54+
const range = this.time[this.time.length - 1];
55+
range.push(new Date());
56+
}
57+
3958
get duration() {
4059
return this.time.reduce((prev: number, range: Date[]) => {
4160
if (range.length > 0) {

src/services/tasks/TaskStore.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default class TaskStore {
2121
return this.tasks[projectId] || [];
2222
}
2323

24+
getTaskByDate(date: Date) {}
25+
2426
add(task: TaskModel) {
2527
const { projectId } = task;
2628
if (!Array.isArray(this.tasks[projectId])) {
@@ -36,21 +38,13 @@ export default class TaskStore {
3638
this.endTimer(this.activeTask);
3739
}
3840
this.activeTask = task;
39-
task.time.forEach((range) => {
40-
if (range.length === 1) {
41-
range[1] = new Date();
42-
}
43-
});
44-
task.time.push([new Date()]);
45-
task.active = true;
41+
task.start();
4642
this.tasksService.save(this.tasks);
4743
}
4844

4945
endTimer(task: TaskModel) {
5046
this.activeTask = undefined;
51-
task.active = false;
52-
const range = task.time[task.time.length - 1];
53-
range.push(new Date());
47+
task.end();
5448
this.tasksService.save(this.tasks);
5549
}
5650

@@ -75,6 +69,11 @@ export default class TaskStore {
7569
this.tasksService.save(this.tasks);
7670
}
7771

72+
private findTasksByDateRecursive(tasks: TaskModel[], result: TaskModel[]) {
73+
for (let task of tasks) {
74+
}
75+
}
76+
7877
private findActiveTask() {
7978
Object.keys(this.tasks).find((projectId) => {
8079
const found = this.findActiveTaskRecursive(this.tasks[projectId]);

0 commit comments

Comments
 (0)