-
Notifications
You must be signed in to change notification settings - Fork 1
/
FutureViewTask.tsx
207 lines (193 loc) · 6.84 KB
/
FutureViewTask.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
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
import React, { KeyboardEvent, ReactElement, SyntheticEvent, ReactNode } from 'react';
import { connect } from 'react-redux';
import FloatingTaskEditor from 'components/Util/TaskEditors/FloatingTaskEditor';
import { State, SubTask, Task } from 'store/store-types';
import CheckBox from 'components/UI/CheckBox';
import { FloatingPosition, CalendarPosition } from 'components/Util/TaskEditors/editors-types';
import { getTodayAtZeroAM, getDateWithDateString } from 'util/datetime-util';
import OverdueAlert from 'components/UI/OverdueAlert';
import { editMainTask } from 'firebase/actions';
import { useMappedWindowSize } from 'hooks/window-size-hook';
import { NONE_TAG } from 'util/tag-util';
import SamwiseIcon from 'components/UI/SamwiseIcon';
import { removeTaskWithPotentialPrompt } from 'util/task-util';
import FutureViewSubTask from './FutureViewSubTask';
import styles from './FutureViewTask.module.css';
type CompoundTask = {
readonly original: Task;
readonly filteredSubTasks: SubTask[];
readonly color: string;
};
type OwnProps = {
readonly taskId: string;
readonly containerDate: string;
readonly inNDaysView: boolean;
readonly taskEditorPosition: FloatingPosition;
readonly calendarPosition: CalendarPosition;
readonly doesShowCompletedTasks: boolean;
readonly isInMainList: boolean;
};
type Props = OwnProps & {
readonly compoundTask: CompoundTask | null;
};
/**
* The component used to render one task in backlog day.
*/
function FutureViewTask(
{
compoundTask, containerDate, inNDaysView, taskEditorPosition, isInMainList, calendarPosition,
}: Props,
): ReactElement | null {
const isSmallScreen = useMappedWindowSize(({ width }) => width <= 768);
if (compoundTask === null) {
return null;
}
const { original, filteredSubTasks, color } = compoundTask;
/**
* Get an onClickHandler when the element is clicked.
* This methods ensure that only clicking on task text counts.
*
* @param opener the opener passed by the floating task editor.
* @return the onClick handler.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const getOnClickHandler = (opener: () => void) => (event?: SyntheticEvent<HTMLElement>): void => {
if (event == null) {
opener();
return;
}
if (event.target instanceof HTMLElement) {
const elem: HTMLElement = event.target;
// only accept click on text.
if (elem.className === styles.TaskText) {
opener();
}
}
};
const replaceDateForFork = getDateWithDateString(original.date, containerDate);
const replaceDateForForkOpt = original.type === 'ONE_TIME' ? null : replaceDateForFork;
const TaskCheckBox = (): ReactElement => {
const { id, complete } = original;
const onChange = (): void => editMainTask(id, replaceDateForForkOpt, { complete: !complete });
return <CheckBox className={styles.TaskCheckBox} checked={complete} onChange={onChange} />;
};
const TaskName = (): ReactElement => {
const { name, complete } = original;
const tagStyle = complete ? { textDecoration: 'line-through' } : {};
return <span className={styles.TaskText} style={tagStyle}>{name}</span>;
};
const RemoveTaskIcon = (): ReactElement => {
const handler = (): void => removeTaskWithPotentialPrompt(original, replaceDateForFork);
return <SamwiseIcon iconName="x-light" className={styles.TaskIcon} onClick={handler} />;
};
const PinIcon = (): ReactElement => {
const { id, inFocus } = original;
const handler = (): void => editMainTask(id, replaceDateForForkOpt, { inFocus: !inFocus });
return (
<SamwiseIcon
iconName={inFocus ? 'pin-light-filled' : 'pin-light-outline'}
className={styles.TaskIcon}
onClick={handler}
/>
);
};
const renderMainTaskInfo = (simplified = false): ReactElement => {
if (simplified && isInMainList) {
const style = { backgroundColor: color, height: '25px' };
return <div className={styles.TaskMainWrapper} style={style} />;
}
return (
<div className={styles.TaskMainWrapper} style={{ backgroundColor: color }}>
<TaskCheckBox />
<TaskName />
<PinIcon />
<RemoveTaskIcon />
</div>
);
};
const renderSubTasks = (): ReactNode => filteredSubTasks.map((s) => (
<FutureViewSubTask
key={s.id}
subTask={s}
mainTaskId={original.id}
replaceDateForFork={replaceDateForForkOpt}
mainTaskCompleted={original.complete}
/>
));
const { date, complete } = original;
const overdueComponentOpt = (date < getTodayAtZeroAM() && !complete) && (
<OverdueAlert target="future-view-task" />
);
// Construct the trigger for the floating task editor.
const trigger = (opened: boolean, opener: () => void): ReactElement => {
const onClickHandler = getOnClickHandler(opener);
const onSpaceHandler = (e: KeyboardEvent<HTMLDivElement>): void => {
if (e.key === ' ') {
onClickHandler();
}
};
const style = opened ? { zIndex: 8 } : {};
const mainTasks = inNDaysView
? renderMainTaskInfo()
: renderMainTaskInfo(isSmallScreen);
const subtasks = inNDaysView ? renderSubTasks() : null;
return (
<div
role="button"
tabIndex={0}
className={styles.Task}
style={style}
onClick={onClickHandler}
onKeyUp={onSpaceHandler}
>
{overdueComponentOpt}
{mainTasks}
{subtasks}
</div>
);
};
return (
<FloatingTaskEditor
position={taskEditorPosition}
calendarPosition={calendarPosition}
initialTask={original}
taskAppearedDate={containerDate}
trigger={trigger}
/>
);
}
const getCompoundTask = (
{ tasks, subTasks, tags }: State, { taskId, doesShowCompletedTasks }: OwnProps,
): CompoundTask | null => {
const original = tasks.get(taskId);
if (original == null) {
return null;
}
const { color } = tags.get(original.tag) || NONE_TAG;
if (doesShowCompletedTasks) {
let filteredSubTasks: SubTask[] = [];
original.children.forEach((subTaskId) => {
const s = subTasks.get(subTaskId);
if (s != null) { filteredSubTasks.push(s); }
});
filteredSubTasks = filteredSubTasks.sort((a, b) => a.order - b.order);
return { original, filteredSubTasks, color };
}
if (original.complete) {
return null;
}
let filteredSubTasks: SubTask[] = [];
original.children.forEach((subTaskId) => {
const s = subTasks.get(subTaskId);
if (s != null && !s.complete) { filteredSubTasks.push(s); }
});
filteredSubTasks = filteredSubTasks.sort((a, b) => a.order - b.order);
return { original, filteredSubTasks, color };
};
const mapStateToProps = (
state: State, ownProps: OwnProps,
): { readonly compoundTask: CompoundTask | null } => ({
compoundTask: getCompoundTask(state, ownProps),
});
const Connected = connect(mapStateToProps)(FutureViewTask);
export default Connected;