Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/task-editor/due.tsx
1691 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Task due date
8
- displays due date
9
- allows for changing it
10
*/
11
12
import { React, CSS } from "../../app-framework";
13
import { DateTimePicker, Icon, Gap, TimeAgo } from "../../components";
14
import { TaskActions } from "./actions";
15
16
const STYLE: CSS = {
17
zIndex: 1,
18
position: "absolute",
19
border: "1px solid lightgrey",
20
background: "white",
21
borderRadius: "4px",
22
margin: "-20px 0 0 -150px", // we use a negative margin to adjust absolute position of calendar popover (hackish)
23
boxShadow: "0 6px 12px rgba(0,0,0,.175)",
24
} as const;
25
26
interface Props {
27
actions?: TaskActions;
28
task_id: string;
29
due_date?: number;
30
editing?: boolean;
31
read_only?: boolean;
32
is_done?: boolean; // do not show due date in red if task already done.
33
}
34
35
export const DueDate: React.FC<Props> = React.memo(
36
({ actions, task_id, due_date, editing, read_only, is_done }) => {
37
function stop_editing() {
38
actions?.stop_editing_due_date(task_id);
39
actions?.enable_key_handler();
40
}
41
42
function edit() {
43
actions?.edit_due_date(task_id);
44
}
45
46
function set_due_date(date) {
47
actions?.set_due_date(task_id, date);
48
if (!date) {
49
stop_editing();
50
}
51
}
52
53
function render_calendar() {
54
let value;
55
if (!editing) {
56
return;
57
}
58
if (due_date) {
59
value = new Date(due_date);
60
} else {
61
value = new Date();
62
}
63
return (
64
<div style={STYLE}>
65
<DateTimePicker
66
value={value}
67
open={true}
68
placeholder={"Set Task Due Date"}
69
onChange={(date) => set_due_date(date?.valueOf())}
70
onFocus={actions?.disable_key_handler}
71
onBlur={stop_editing}
72
/>
73
</div>
74
);
75
}
76
77
function render_remove_due_date() {
78
if (!due_date) {
79
return;
80
}
81
return (
82
<span style={{ color: "#888" }}>
83
<Gap />
84
<Icon
85
name="times"
86
onClick={() => {
87
set_due_date(null);
88
actions?.stop_editing_due_date(task_id);
89
}}
90
/>
91
</span>
92
);
93
}
94
95
function render_due_date() {
96
let elt;
97
let style: CSS | undefined = undefined;
98
if (due_date) {
99
const date = new Date(due_date);
100
if (date <= new Date() && !is_done) {
101
style = { color: "white", backgroundColor: "red", padding: "3px" };
102
}
103
elt = <TimeAgo date={new Date(due_date)} />;
104
} else {
105
elt = <span>none</span>;
106
}
107
return (
108
<span onClick={!read_only ? edit : undefined} style={style}>
109
{elt}
110
</span>
111
);
112
}
113
114
if (read_only) {
115
return render_due_date();
116
} else {
117
return (
118
<div style={{ cursor: "pointer" }}>
119
{render_due_date()}
120
{render_remove_due_date()}
121
{render_calendar()}
122
</div>
123
);
124
}
125
}
126
);
127
128