Skip to content

Commit 484d996

Browse files
author
Dmitry Yadrikhinsky
committed
[Timer] useTaskDuration
1 parent a0ec345 commit 484d996

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
parser: '@typescript-eslint/parser',
23
extends: [
34
'plugin:import/errors',
45
'plugin:react/recommended',

src/hooks/TaskHooks.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { msToTime } from '../helpers/DateTime';
2+
import TaskModel from '../models/TaskModel';
3+
import { useEffect, useRef, useState } from 'react';
4+
5+
/**
6+
*
7+
* @param model
8+
*/
9+
export function useTaskDuration(model: TaskModel | undefined) {
10+
model = model || ({} as TaskModel);
11+
12+
const intervalRef = useRef<NodeJS.Timeout>();
13+
const [duration, setDuration] = useState<string>('');
14+
15+
useEffect(() => {
16+
if (!model) {
17+
return;
18+
}
19+
const duration = model.duration;
20+
if (duration !== 0) {
21+
setDuration(msToTime(duration));
22+
}
23+
if (model.active) {
24+
intervalRef.current = setInterval(() => {
25+
setDuration(msToTime(model?.duration || 0));
26+
}, 1000);
27+
}
28+
return () => {
29+
if (intervalRef.current) {
30+
clearInterval(intervalRef.current);
31+
}
32+
};
33+
}, [model, model.active]);
34+
35+
return duration;
36+
}

src/models/ProjectModel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ export default class ProjectModel extends AbstractModel
77
implements IProjectItem {
88
key: string = '';
99
title: string = '';
10-
children: IProjectItem[] = [];
10+
children: ProjectModel[] = [];
1111

1212
constructor(props: IProjectItem) {
1313
super();
1414
this.load(props);
15+
this.children = props.children?.map((json) => new ProjectModel(json)) || [];
1516
}
1617
}

src/screens/projects/components/TaskNode/TaskNode.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React from 'react';
22
import {
33
CaretRightFilled,
44
DeleteOutlined,
@@ -9,7 +9,7 @@ import './TaskNode.less';
99

1010
import TaskModel from '../../../../models/TaskModel';
1111
import rootStore from '../../../../services/RootStore';
12-
import { msToTime } from '../../../../helpers/DateTime';
12+
import { useTaskDuration } from '../../../../hooks/TaskHooks';
1313

1414
const { tasksStore } = rootStore;
1515

@@ -18,25 +18,7 @@ interface TaskNodeProps {
1818
}
1919

2020
export default function TaskNode({ model }: TaskNodeProps) {
21-
const intervalRef = useRef<NodeJS.Timeout>();
22-
const [duration, setDuration] = useState<string>('');
23-
24-
useEffect(() => {
25-
const duration = model.duration;
26-
if (duration !== 0) {
27-
setDuration(msToTime(duration));
28-
}
29-
if (model.active) {
30-
intervalRef.current = setInterval(() => {
31-
setDuration(msToTime(model.duration));
32-
}, 1000);
33-
}
34-
return () => {
35-
if (intervalRef.current) {
36-
clearInterval(intervalRef.current);
37-
}
38-
};
39-
}, [model.active]);
21+
const duration = useTaskDuration(model);
4022

4123
return (
4224
<div className="task-node">

src/services/projects/ProjectStore.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ export default class ProjectStore {
1717
this.projectService.save(this.projects);
1818
}
1919

20+
get(projectId: string): ProjectModel | undefined {
21+
return this.getRecursive(this.projects, projectId);
22+
}
23+
2024
add(project: ProjectModel) {
2125
this.projects.push(project);
2226
this.projects = this.projects.slice();
2327
this.projectService.save(this.projects);
2428
}
2529

26-
delete(project: ProjectModel) {}
27-
2830
setActiveProject(projectId: string) {
2931
this.activeProject = projectId;
3032
}
@@ -33,4 +35,23 @@ export default class ProjectStore {
3335
this.projects = this.projectService.getAll();
3436
this.activeProject = Object.keys(this.projects)[0];
3537
}
38+
39+
private getRecursive(
40+
projects: ProjectModel[],
41+
projectId: string
42+
): ProjectModel | undefined {
43+
for (const projectKey in projects) {
44+
const project = projects[projectKey];
45+
if (project.key === projectId) {
46+
return project;
47+
}
48+
if (Array.isArray(project.children)) {
49+
const found = this.getRecursive(project.children, projectId);
50+
if (found) {
51+
return found;
52+
}
53+
}
54+
}
55+
return undefined;
56+
}
3657
}

src/services/tasks/TaskStore.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ export default class TaskStore {
3131
this.tasksService.save(this.tasks);
3232
}
3333

34-
delete(projectId: string, task: TaskModel) {}
35-
3634
startTimer(task: TaskModel) {
3735
if (this.activeTask) {
3836
this.endTimer(this.activeTask);
@@ -89,7 +87,8 @@ export default class TaskStore {
8987
}
9088

9189
private findActiveTaskRecursive(tasks: TaskModel[]): TaskModel | undefined {
92-
return tasks.find((task) => {
90+
for (const taskKey in tasks) {
91+
const task = tasks[taskKey];
9392
if (task.active) {
9493
return task;
9594
}
@@ -99,8 +98,8 @@ export default class TaskStore {
9998
return found;
10099
}
101100
}
102-
return false;
103-
});
101+
}
102+
return undefined;
104103
}
105104

106105
private getCheckedKeysRecursive(tasks: TaskModel[], checkedIds: string[]) {

0 commit comments

Comments
 (0)