-
Notifications
You must be signed in to change notification settings - Fork 1
/
FutureViewControl.tsx
287 lines (272 loc) · 8.54 KB
/
FutureViewControl.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import React, { ReactElement } from 'react';
import { faEye, faEyeSlash } from '@fortawesome/free-solid-svg-icons';
import { FutureViewContainerType, FutureViewDisplayOption } from './future-view-types';
import SquareTextButton from '../../UI/SquareTextButton';
import SquareIconToggle from '../../UI/SquareIconToggle';
import { date2YearMonth } from '../../../util/datetime-util';
import styles from './FutureViewControl.module.scss';
import { useMappedWindowSize } from '../../../hooks/window-size-hook';
import SamwiseIcon from '../../UI/SamwiseIcon';
/*
* --------------------------------------------------------------------------------
* Part 0: Common Types
* --------------------------------------------------------------------------------
*/
type ChangeOffsetInstruction = 'TODAY' | number;
type ChangeableProps = {
readonly displayOption: FutureViewDisplayOption;
readonly offset: number;
};
type Props = ChangeableProps & {
readonly nDays: number;
readonly onChange: (change: Partial<ChangeableProps>) => void;
};
/*
* --------------------------------------------------------------------------------
* Part 1: Helper Small Components
* --------------------------------------------------------------------------------
*/
/**
* The padding span.
*/
const Padding = (): ReactElement => <span className={styles.ControlPadding} />;
/**
* The title component.
*/
const Title = ({ text }: { readonly text: string }): ReactElement => (
<h3 className={styles.Title}>{text}</h3>
);
/*
* --------------------------------------------------------------------------------
* Part 2: NavControl
* --------------------------------------------------------------------------------
*/
type NavControlProps = {
readonly containerType: FutureViewContainerType;
readonly futureViewOffset: number;
readonly changeOffset: (instruction: ChangeOffsetInstruction) => () => void;
readonly isSmallScreen: boolean;
};
/**
* Returns a suitable title for the backlog header title.
*
* @param {number} monthOffset offset of displaying months.
* @return {string} a suitable title for the backlog header title.
*/
function getMonthlyViewHeaderTitle(monthOffset: number): string {
const d = new Date();
d.setMonth(d.getMonth() + monthOffset, 1);
return date2YearMonth(d);
}
function getBiWeeklyViewHeaderTitle(biweeklyOffset: number): string {
const s = new Date();
s.setDate(s.getDate() + biweeklyOffset * 14 - s.getDay()); // minus day offset
const e = new Date(s);
e.setDate(e.getDate() + 13);
const startString = `${s.getMonth() + 1}/${s.getDate()}/${s.getFullYear()}`;
const endString = `${e.getMonth() + 1}/${e.getDate()}/${e.getFullYear()}`;
return `${startString} - ${endString}`;
}
/**
* The component to control nav.
*/
function NavControl(props: NavControlProps): ReactElement {
const { containerType, futureViewOffset, changeOffset, isSmallScreen } = props;
const prevHandler = changeOffset(-1);
const nextHandler = changeOffset(+1);
if (containerType === 'N_DAYS') {
const prevStyle = { left: -35 };
const nextStyle = { right: -35 };
return (
<>
{futureViewOffset >= 0 && (
<SamwiseIcon
iconName="dropdown"
title="Go back"
className={`${styles.NavButtonPrev} ${styles.NavButtonNDays}`}
style={prevStyle}
onClick={prevHandler}
/>
)}
<SamwiseIcon
iconName="dropdown"
title="Go forward"
className={`${styles.NavButtonNext} ${styles.NavButtonNDays}`}
style={nextStyle}
onClick={nextHandler}
/>
</>
);
}
const prev = (
<SamwiseIcon
iconName="dropdown"
title="Go back"
className={styles.NavButtonPrev}
onClick={prevHandler}
/>
);
const next = (
<SamwiseIcon
iconName="dropdown"
title="Go forward"
className={styles.NavButtonNext}
onClick={nextHandler}
/>
);
if (containerType === 'BIWEEKLY') {
return (
<>
{futureViewOffset >= 0 && prev}
<Title text={getBiWeeklyViewHeaderTitle(futureViewOffset)} />
{next}
</>
);
}
if (containerType === 'MONTHLY') {
return (
<>
{!isSmallScreen && <Padding />}
{futureViewOffset >= 0 && prev}
<Title text={getMonthlyViewHeaderTitle(futureViewOffset)} />
{next}
</>
);
}
throw new Error('Bad display option.');
}
/*
* --------------------------------------------------------------------------------
* Part 3: DisplayOptionControl
* --------------------------------------------------------------------------------
*/
/**
* The component to control display options
*/
function DisplayOptionControl({ nDays, displayOption, offset, onChange }: Props): ReactElement {
const { containerType, doesShowCompletedTasks } = displayOption;
const toggleCompletedTasks = (): void => onChange({
displayOption: { containerType, doesShowCompletedTasks: !doesShowCompletedTasks },
});
const switchContainerType = (newContainerType: FutureViewContainerType): void => {
let dayOffset: number;
switch (containerType) {
case 'N_DAYS':
dayOffset = offset * nDays;
break;
case 'BIWEEKLY':
dayOffset = offset * 14;
break;
case 'MONTHLY':
dayOffset = offset * 30;
break;
default:
throw new Error('Bad container type!');
}
let newOffset: number;
switch (newContainerType) {
case 'N_DAYS':
newOffset = Math.floor(dayOffset / nDays);
break;
case 'BIWEEKLY':
newOffset = Math.floor(dayOffset / 14);
break;
case 'MONTHLY':
newOffset = Math.floor(dayOffset / 30);
break;
default:
throw new Error('Bad container type!');
}
onChange({
displayOption: { containerType: newContainerType, doesShowCompletedTasks },
offset: newOffset,
});
};
const renderContainerTypeSwitcherButton = (
type: FutureViewContainerType, text: string,
): ReactElement => {
const className = containerType === type
? `${styles.ContainerTypeSwitcherButton} ${styles.ContainerTypeSwitcherActiveButton}`
: styles.ContainerTypeSwitcherButton;
return (
<button
type="button"
title={`Change to ${text} view`}
className={className}
onClick={() => switchContainerType(type)}
>
<span className={styles.ContainerTypeSwitcherButtonText}>{text}</span>
</button>
);
};
return (
<>
<SquareIconToggle
active={doesShowCompletedTasks}
iconNames={[faEyeSlash, faEye]}
onToggle={toggleCompletedTasks}
/>
<div className={styles.ContainerTypeSwitcher}>
{renderContainerTypeSwitcherButton('N_DAYS', `${nDays}D`)}
{renderContainerTypeSwitcherButton('BIWEEKLY', '2W')}
{renderContainerTypeSwitcherButton('MONTHLY', 'M')}
</div>
</>
);
}
/*
* --------------------------------------------------------------------------------
* Part 4: Main Component
* --------------------------------------------------------------------------------
*/
/**
* The controller component for the future view in task view.
*/
export default function FutureViewControl(props: Props): ReactElement {
const { displayOption, offset, onChange } = props;
const isSmallScreen = useMappedWindowSize(({ width }) => width <= 600);
const { containerType } = displayOption;
const changeOffset = (instruction: ChangeOffsetInstruction): (() => void) => (): void => {
const newOffset = instruction === 'TODAY' ? 0 : (offset + instruction);
onChange({ offset: newOffset });
};
const today = offset !== 0 && (
<SquareTextButton text="Today" onClick={changeOffset('TODAY')} />
);
if (isSmallScreen) {
return (
<div>
<div className={styles.FutureViewControl}>
<Title text="Future" />
<Padding />
{today}
<DisplayOptionControl {...props} />
</div>
<div className={styles.FutureViewControl}>
<Padding />
<NavControl
containerType={containerType}
futureViewOffset={offset}
changeOffset={changeOffset}
isSmallScreen
/>
<Padding />
</div>
</div>
);
}
return (
<div className={styles.FutureViewControl}>
<Title text="Future" />
<NavControl
containerType={containerType}
futureViewOffset={offset}
changeOffset={changeOffset}
isSmallScreen={false}
/>
<Padding />
{today}
<DisplayOptionControl {...props} />
</div>
);
}