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