Skip to content

Commit 1d59eba

Browse files
committed
fixed empty time input
1 parent 169c222 commit 1d59eba

3 files changed

Lines changed: 35 additions & 29 deletions

File tree

client/src/components/task_form.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
6666
const new_topics = topics
6767
.filter((t) => t.type === "new")
6868
.map((t) => ({ name: t.name, color_hex: t.color_hex }));
69+
70+
const validTimes = times.filter(t => t.start_time && t.end_time);
71+
6972
const response = await fetch("http://localhost:8000/api/planner/tasks/", {
7073
method: "POST",
7174
headers: {
@@ -77,7 +80,7 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
7780
description: description,
7881
completed: false,
7982
user_id: userId,
80-
times: times,
83+
times: validTimes,
8184
existing_topic_ids: existing_topic_ids,
8285
new_topics: new_topics,
8386
}),

client/src/components/task_item.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export function TaskItem({
9393
color_hex: t.color_hex,
9494
}));
9595

96+
const validTimes = draftTimes.filter(t => t.start_time && t.end_time);
97+
9698
const response = await fetch(
9799
`http://localhost:8000/api/planner/tasks/${item.id}/`,
98100
{
@@ -107,7 +109,7 @@ export function TaskItem({
107109
completed: item.completed,
108110
existing_topic_ids: existing_topic_ids,
109111
new_topics: new_topics,
110-
times: draftTimes,
112+
times: validTimes,
111113
}),
112114
},
113115
);

client/src/components/time_input.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,35 @@ const daysOfWeek = [
2222
];
2323

2424
export function TimeInput({ times, setTimes }: TimeInputProps) {
25+
26+
const addTime = () => {
27+
// Add a new empty draft time only if there isn't already an empty one
28+
const hasEmpty = times.some(t => !t.start_time || !t.end_time);
29+
if (!hasEmpty) {
30+
setTimes([
31+
...times,
32+
{ id: 0, day: 1, start_time: "", end_time: "", repeating: false },
33+
]);
34+
}
35+
};
36+
37+
const updateTime = (index: number, field: keyof Time, value: any) => {
38+
const newTimes = [...times];
39+
newTimes[index] = { ...newTimes[index], [field]: value };
40+
setTimes(newTimes);
41+
};
42+
43+
const removeTime = (index: number) => {
44+
setTimes(times.filter((_, i) => i !== index));
45+
};
46+
2547
return (
2648
<div>
2749
{times.map((time, index) => (
2850
<div key={index}>
2951
<select
3052
value={time.day}
31-
onChange={(e) => {
32-
const newTimes = [...times];
33-
newTimes[index].day = parseInt(e.target.value);
34-
setTimes(newTimes);
35-
}}
53+
onChange={(e) => updateTime(index, "day", parseInt(e.target.value))}
3654
>
3755
{daysOfWeek.map((day) => (
3856
<option key={day.value} value={day.value}>
@@ -43,46 +61,29 @@ export function TimeInput({ times, setTimes }: TimeInputProps) {
4361
<input
4462
type="time"
4563
value={time.start_time}
46-
onChange={(e) => {
47-
const newTimes = [...times];
48-
newTimes[index].start_time = e.target.value;
49-
setTimes(newTimes);
50-
}}
64+
onChange={(e) => updateTime(index, "start_time", e.target.value)}
5165
/>
5266
<input
5367
type="time"
5468
value={time.end_time}
55-
onChange={(e) => {
56-
const newTimes = [...times];
57-
newTimes[index].end_time = e.target.value;
58-
setTimes(newTimes);
59-
}}
69+
onChange={(e) => updateTime(index, "end_time", e.target.value)}
6070
/>
6171
<input
6272
type="checkbox"
6373
checked={time.repeating}
64-
onChange={(e) => {
65-
const newTimes = [...times];
66-
newTimes[index].repeating = e.target.checked;
67-
setTimes(newTimes);
68-
}}
74+
onChange={(e) => updateTime(index, "repeating", e.target.checked)}
6975
/>
7076
<button
7177
type="button"
72-
onClick={() => setTimes(times.filter((_, i) => i !== index))}
78+
onClick={() => removeTime(index)}
7379
>
7480
Remove
7581
</button>
7682
</div>
7783
))}
7884
<button
7985
type="button"
80-
onClick={() =>
81-
setTimes([
82-
...times,
83-
{ id: 0, day: 1, start_time: "", end_time: "", repeating: false },
84-
])
85-
}
86+
onClick={addTime}
8687
>
8788
Add Time
8889
</button>

0 commit comments

Comments
 (0)