aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/TaskManagerTask.tsx
blob: 2d24442752cf65135ccbe98982ff62bd0891cba0 (plain)
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
import { action, observable, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Docs } from '../../documents/Documents';
import { DocumentType } from '../../documents/DocumentTypes';
import { FieldView } from './FieldView';
import { DateField } from '../../../fields/DateField';
import { Doc } from '../../../fields/Doc';

import './TaskManagerTask.scss';

interface TaskManagerProps {
    Document: Doc;
}

@observer
export class TaskManagerTask extends React.Component<TaskManagerProps> {
    public static LayoutString(fieldStr: string) {
        return FieldView.LayoutString(TaskManagerTask, fieldStr);
    }

    @action
    updateText = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
        this.props.Document.text = e.target.value;
    };

    @action
    updateTitle = (e: React.ChangeEvent<HTMLInputElement>) => {
        this.props.Document.title = e.target.value;
    };

    @action
    updateAllDay = (e: React.ChangeEvent<HTMLInputElement>) => {
        this.props.Document.allDay = e.target.checked;

        if (e.target.checked) {
            delete this.props.Document.startTime;
            delete this.props.Document.endTime;
        }

        this.setTaskDateRange();
    };

    @action
    updateStart = (e: React.ChangeEvent<HTMLInputElement>) => {
        const newStart = new Date(e.target.value);

        this.props.Document.startTime = new DateField(newStart);

        const endDate = this.props.Document.endTime instanceof DateField ? this.props.Document.endTime.date : undefined;
        if (endDate && newStart > endDate) {
            // Alert user
            alert('Start time cannot be after end time. End time has been adjusted.');
            
            // Fix end time
            const adjustedEnd = new Date(newStart.getTime() + 60 * 60 * 1000);
            this.props.Document.endTime = new DateField(adjustedEnd);
        }

        this.setTaskDateRange();
    };



    @action
    updateEnd = (e: React.ChangeEvent<HTMLInputElement>) => {
        const newEnd = new Date(e.target.value);

        this.props.Document.endTime = new DateField(newEnd);

        const startDate = this.props.Document.startTime instanceof DateField ? this.props.Document.startTime.date : undefined;
        if (startDate && newEnd < startDate) {
            // Alert user
            alert('End time cannot be before start time. Start time has been adjusted.');
            
            // Fix start time
            const adjustedStart = new Date(newEnd.getTime() - 60 * 60 * 1000);
            this.props.Document.startTime = new DateField(adjustedStart);
        }

        this.setTaskDateRange();
    };




    @action
    setTaskDateRange() {
        const doc = this.props.Document;

        if (doc.allDay) {
            // All-day task → use date only
            if (!doc.title) return;

            const parsedDate = new Date(doc.title as string);
            if (!isNaN(parsedDate.getTime())) {
                const localStart = new Date(parsedDate.getFullYear(), parsedDate.getMonth(), parsedDate.getDate());
                const localEnd = new Date(localStart);
                doc.date_range = `${localStart.toISOString()}|${localEnd.toISOString()}`;
                doc.allDay = true;
            }
        } else {
            // Timed task → use full startTime and endTime
            const startField = doc.startTime;
            const endField = doc.endTime;
            const startDate = startField instanceof DateField ? startField.date : null;
            const endDate = endField instanceof DateField ? endField.date : null;

            if (startDate && endDate && !isNaN(startDate.getTime()) && !isNaN(endDate.getTime())) {
                doc.date_range = `${startDate.toISOString()}|${endDate.toISOString()}`;
                doc.allDay = false;
            } else {
                console.warn('startTime or endTime is invalid');
            }
        }
    }

    @action
    toggleComplete = (e: React.ChangeEvent<HTMLInputElement>) => {
        this.props.Document.completed = e.target.checked;
    };

    constructor(props: TaskManagerProps) {
        super(props);
        makeObservable(this);
    }

    componentDidMount() {
        this.setTaskDateRange();
    }
    

    render() {

        function toLocalDateTimeString(date: Date): string {
            const pad = (n: number) => n.toString().padStart(2, '0');
            return (
                date.getFullYear() +
                '-' +
                pad(date.getMonth() + 1) +
                '-' +
                pad(date.getDate()) +
                'T' +
                pad(date.getHours()) +
                ':' +
                pad(date.getMinutes())
            );
        }
        
        const doc = this.props.Document;

        const taskDesc = typeof doc.text === 'string' ? doc.text : '';
        const taskTitle = typeof doc.title === 'string' ? doc.title : '';
        const allDay = !!doc.allDay;
        const isCompleted = !!this.props.Document.completed;

        const startTime = doc.startTime instanceof DateField && doc.startTime.date instanceof Date
            ? toLocalDateTimeString(doc.startTime.date)
            : '';

        const endTime = doc.endTime instanceof DateField && doc.endTime.date instanceof Date
            ? toLocalDateTimeString(doc.endTime.date)
            : '';



        return (
            <div className="task-manager-container">
                <input
                    className="task-manager-title"
                    type="text"
                    placeholder="Task Title"
                    value={taskTitle}
                    onChange={this.updateTitle}
                    disabled={isCompleted}
                    style={{opacity: isCompleted ? 0.7 : 1,}}
                />

                <textarea
                className="task-manager-description"
                placeholder="What’s your task?"
                value={taskDesc}
                onChange={this.updateText}
                disabled={isCompleted}
                style={{opacity: isCompleted ? 0.7 : 1,}}
                />

                <div className="task-manager-checkboxes">

                    <label className="task-manager-allday" style={{opacity: isCompleted ? 0.7 : 1,}}>
                        <input 
                            type="checkbox" 
                            checked={allDay} 
                            onChange={this.updateAllDay} 
                            disabled={isCompleted}
                        />
                        All day
                    </label>

                    <label className="task-manager-complete">
                        <input type="checkbox" checked={isCompleted} onChange={this.toggleComplete} />
                        Complete
                    </label>

                </div>

                {!allDay && (
                <div
                    className="task-manager-times"
                    style={{ opacity: isCompleted ? 0.7 : 1 }}
                >
                    <label>
                        Start:
                        <input
                            type="datetime-local"
                            value={startTime}
                            onChange={this.updateStart}
                            disabled={isCompleted}
                        />
                    </label>
                    <label>
                        End:
                        <input
                            type="datetime-local"
                            value={endTime}
                            onChange={this.updateEnd}
                            disabled={isCompleted}
                        />
                    </label>
                </div>
            )}
            </div>
        );
    }
}

Docs.Prototypes.TemplateMap.set(DocumentType.TASK, {
    layout: { view: TaskManagerTask, dataField: 'text' },
    options: {
        acl: '',
        _height: 35,
        _xMargin: 10,
        _yMargin: 10,
        _layout_autoHeight: true,
        _layout_nativeDimEditable: true,
        _layout_reflowVertical: true,
        _layout_reflowHorizontal: true,
        defaultDoubleClick: 'ignore',
        systemIcon: 'BsCheckSquare', // or whatever icon you like
    },
});