-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTaskActions.ts
More file actions
168 lines (146 loc) · 4.6 KB
/
useTaskActions.ts
File metadata and controls
168 lines (146 loc) · 4.6 KB
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
import React from "react";
import { createClient } from "@/lib/supabase/client";
import { useToast } from "@/components/ui/use-toast";
import { TaskAction, Task } from "@/types/task";
import { getTodayDateString } from "@/lib/utils";
const supabase = createClient();
export const useTaskActions = (dispatch: React.Dispatch<TaskAction>) => {
const { toast } = useToast();
// タスクを削除
const handleDelete = async (taskId: string) => {
const { error } = await supabase.from("tasks").delete().eq("id", taskId);
if (error) {
toast({
title: "Error",
description: "Failed to delete task",
variant: "destructive",
});
console.error("Error deleting task:", error);
} else {
// ローカル状態から削除したタスクを除外
dispatch({ type: "DELETE_TASK", payload: taskId });
}
};
// タスクの開始/停止を処理
const handleTaskTimer = async (
taskId: string,
action: "start" | "stop" | "complete",
) => {
const updateData: any = {};
if (action === "start") {
updateData.start_time = new Date().toISOString();
} else if (action === "stop") {
updateData.end_time = new Date().toISOString();
}
const { error } = await supabase
.from("tasks")
.update(updateData)
.eq("id", taskId);
if (error) {
toast({
title: "Error",
description: `Failed to ${action} task`,
variant: "destructive",
});
console.error(`Error ${action}ing task:`, error);
} else {
dispatch({ type: "UPDATE_TASK", payload: { id: taskId, ...updateData } });
}
};
// タスクを今日に移動
const handleMoveToToday = async (taskId: string) => {
const taskDate = getTodayDateString();
const { error } = await supabase
.from("tasks")
.update({ task_date: taskDate })
.eq("id", taskId);
if (error) {
toast({
title: "Error",
description: "Failed to move task to today",
variant: "destructive",
});
console.error("Error moving task to today:", error);
} else {
// ローカル状態から削除(画面から消す)
// 注:タスクは別の日付に移動しただけなので、データベースからは削除されていません。
// 現在表示している日付から消すため、DELETE_TASkアクションを使用します。
dispatch({ type: "DELETE_TASK", payload: taskId });
toast({
title: "Success",
description: "タスクを今日に移動しました",
});
}
};
// タスクを中断
const handlePauseTask = async (task: Task) => {
// 1. 現在のタスクに終了時刻を設定して完了させる
const endTime = new Date().toISOString();
const { error: updateError } = await supabase
.from("tasks")
.update({ end_time: endTime })
.eq("id", task.id);
if (updateError) {
toast({
title: "Error",
description: "タスクの中断に失敗しました",
variant: "destructive",
});
console.error("Error pausing task:", updateError);
return;
}
// ローカル状態を更新
dispatch({
type: "UPDATE_TASK",
payload: { id: task.id, end_time: endTime },
});
// 2. 同じ属性で新しいタスクを作成
const newTask = {
title: task.title,
description: task.description,
user_id: task.user_id,
estimated_minute: task.estimated_minute,
category_id: task.category_id,
task_date: task.task_date,
task_order: null,
};
const { data, error: insertError } = await supabase
.from("tasks")
.insert(newTask)
.select();
if (insertError) {
toast({
title: "Error",
description: "新しいタスクの作成に失敗しました",
variant: "destructive",
});
console.error("Error creating new task:", insertError);
return;
}
if (!data || data.length === 0) {
toast({
title: "Error",
description: "新しいタスクの作成に失敗しました",
variant: "destructive",
});
console.error("Error creating new task: No data returned");
return;
}
// 新しく作成したタスクをリストに追加
const createdTask = data[0] as unknown as Task;
dispatch({
type: "ADD_TASK",
payload: createdTask,
});
toast({
title: "Success",
description: `タスク "${task.title}" を中断しました`,
});
};
return {
handleDelete,
handleTaskTimer,
handleMoveToToday,
handlePauseTask,
};
};