This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
/
mainView.tsx
152 lines (146 loc) · 5.39 KB
/
mainView.tsx
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
import React from "react";
import { ThunkDispatch } from "redux-thunk";
import { connect } from "react-redux";
import { RouteComponentProps } from "react-router-dom";
import Container from "./components/bulma/container";
import Button from "./components/bulma/button";
import Input from "./components/bulma/input";
import Control from "./components/bulma/control";
import Level from "./components/bulma/level";
import TaskList from "./components/taskList";
import Footer from "./components/footer";
import { logout } from "../actions/auth";
import { refetchTasks, deleteTask } from "../actions/tasks";
import { AnyAction } from "../typings/actions";
import { Task } from "../typings/tasks";
import { Store } from "../typings/store";
import { uiDelay } from "../const";
import strings from "./assets/locales";
const mapStateToProps = (state: Store) => ({
tasks: state.task.tasks,
username: state.auth.username,
});
interface Props extends RouteComponentProps {
dispatch: ThunkDispatch<any, any, AnyAction>;
tasks: Task[];
username?: string;
}
interface State {
tasks: Task[];
search: string;
}
class MainView extends React.Component<Props, State> {
state = {
tasks: this.props.tasks,
search: "",
};
logout = () => this.props.dispatch(logout());
refetch = () => this.props.dispatch(refetchTasks(this.props.tasks));
prune = () => {
const danglingTasks = this.state.tasks.filter(
(task) =>
this.state.tasks.filter((child) => child.PID === task.ID)
.length === 0 && task.Completed
);
danglingTasks.forEach((task) => this.props.dispatch(deleteTask(task)));
// more tasks possibly left to go?
danglingTasks.length > 0 && setTimeout(() => this.prune(), uiDelay);
};
updateSearch = (e: React.FormEvent<HTMLInputElement>) =>
this.setState({
search: e.currentTarget.value,
});
toNewTask = () => this.props.history.push("/task/new");
componentDidMount = () => {
this.refetch();
document.addEventListener("keydown", this.handleHotkeys);
};
componentWillUnmount = () => {
document.removeEventListener("keydown", this.handleHotkeys);
};
componentDidUpdate = (prevProps: Props) => {
if (prevProps.tasks !== this.props.tasks) {
this.setState({
tasks: this.props.tasks,
});
}
};
handleHotkeys = (e: KeyboardEvent) => {
const escCode = 27;
const search = document.getElementById("searchInput");
const searchFocused = document.activeElement === search;
if ((e.ctrlKey || e.metaKey) && e.key === "f") {
e.preventDefault();
(search as HTMLElement).focus();
} else if (e.key === "n" && !searchFocused) {
e.preventDefault();
this.toNewTask();
} else if (e.key === "u" && !searchFocused) {
e.preventDefault();
this.refetch();
} else if (e.keyCode === escCode && searchFocused) {
(document.activeElement as HTMLElement).blur();
e.preventDefault();
this.setState({
search: "",
});
}
};
render = () => {
const { username } = this.props;
const { tasks, search } = this.state;
return (
<Container>
<Level level className="navbar">
<Level levelItem levelLeft className="headerText">
{`${strings.main_loggedInMsg} ${username}`}
</Level>
<Level
level
levelItem
levelRight
isMobile
className="headerButtons"
>
<Level levelItem>
<Button
value={strings.btns_addTask}
onClick={this.toNewTask}
/>
</Level>
<Level levelItem>
<Button
value={strings.btns_refetch}
onClick={this.refetch}
/>
</Level>
<Level levelItem>
<Button
value={strings.btns_pruneTasks}
onClick={this.prune}
/>
</Level>
<Level levelItem>
<Button
onClick={this.logout}
value={strings.main_logoutBtn}
/>
</Level>
</Level>
</Level>
<Container>
<Control className="searchBox">
<Input
id="searchInput"
placeholder={strings.main_searchTp}
onChange={this.updateSearch}
/>
</Control>
<TaskList tasks={tasks} search={search}></TaskList>
</Container>
<Footer />
</Container>
);
};
}
export default connect(mapStateToProps)(MainView);