Skip to content

Commit e6fc224

Browse files
committed
added time editing capabilities
1 parent 02e5e79 commit e6fc224

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

client/src/components/task_item.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState } from "react";
22

3+
import { TimeInput } from "@/components/time_input";
34
import { TopicInput } from "@/components/topic_input";
45

56
interface Time {
@@ -49,6 +50,7 @@ export function TaskItem({
4950
const [draftName, setDraftName] = useState(item.name);
5051
const [draftDescription, setDraftDescription] = useState(item.description);
5152
const [draftTopics, setDraftTopics] = useState<TopicSelection[]>([]);
53+
const [draftTimes, setDraftTimes] = useState<Time[]>([]);
5254
const [saving, setSaving] = useState(false);
5355

5456
function startEdit() {
@@ -60,13 +62,15 @@ export function TaskItem({
6062
id: t.id,
6163
})),
6264
);
65+
setDraftTimes(item.times.map((t) => ({ ...t })));
6366
setIsEditing(true);
6467
}
6568

6669
function cancelEdit() {
6770
setDraftName(item.name);
6871
setDraftDescription(item.description);
6972
setDraftTopics(item.topics.map((t) => ({ type: "existing", id: t.id })));
73+
setDraftTimes(item.times.map((t) => ({ ...t })));
7074
setIsEditing(false);
7175
}
7276

@@ -83,6 +87,7 @@ export function TaskItem({
8387
name: t.name,
8488
color_hex: t.color_hex,
8589
}));
90+
8691
const response = await fetch(
8792
`http://localhost:8000/api/planner/tasks/${item.id}/`,
8893
{
@@ -94,6 +99,7 @@ export function TaskItem({
9499
completed: item.completed,
95100
existing_topic_ids: existing_topic_ids,
96101
new_topics: new_topics,
102+
times: draftTimes,
97103
}),
98104
},
99105
);
@@ -143,14 +149,18 @@ export function TaskItem({
143149

144150
{/* Task Times */}
145151
<div className="flex flex-col items-end text-sm text-zinc-300">
146-
{item.times?.length > 0
147-
? item.times.map((t) => (
148-
<span key={t.id}>
149-
{formatDay(t.day)} {t.start_time.slice(0, 5)} -{" "}
150-
{t.end_time.slice(0, 5)}
151-
</span>
152-
))
153-
: "No time set"}
152+
{isEditing ? (
153+
<TimeInput times={draftTimes} setTimes={setDraftTimes} />
154+
) : item.times?.length > 0 ? (
155+
item.times.map((t) => (
156+
<span key={t.id}>
157+
{formatDay(t.day)} {t.start_time.slice(0, 5)} -{" "}
158+
{t.end_time.slice(0, 5)}
159+
</span>
160+
))
161+
) : (
162+
"No time set"
163+
)}
154164
</div>
155165
</div>
156166

server/task_planner/serializers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def create(self, validated_data):
7373
def update(self, instance, validated_data):
7474
existing_topics = validated_data.pop("existing_topic_ids", [])
7575
new_topics_data = validated_data.pop("new_topics", [])
76-
#times_data = validated_data.pop("times", [])
76+
times_data = validated_data.pop("times", [])
7777

7878
for attr, value in validated_data.items():
7979
setattr(instance, attr, value)
@@ -86,14 +86,14 @@ def update(self, instance, validated_data):
8686
for topic_data in new_topics_data:
8787
topic = Topic.objects.create(**topic_data, user_id=instance.user_id)
8888
instance.topics.add(topic)
89-
"""
89+
9090
if times_data is not None:
9191
instance.times.all().delete()
9292
Time.objects.bulk_create([
9393
Time(task=instance, **time_data)
9494
for time_data in times_data
9595
])
96-
"""
96+
9797

9898
instance.save()
9999
instance.refresh_from_db()

0 commit comments

Comments
 (0)