91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { useState } from 'react';
|
|
import { Task } from '../entities/Task';
|
|
|
|
const useManageTasks = () => {
|
|
const [tasks, setTasks] = useState<Task[]>([]);
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
const [isError, setIsError] = useState<boolean>(false);
|
|
|
|
const fetchTasks = async (query: string = '') => {
|
|
setIsLoading(true);
|
|
setIsError(false);
|
|
try {
|
|
const response = await fetch(`/api/tasks${query}`, {
|
|
credentials: 'include',
|
|
headers: { Accept: 'application/json' },
|
|
});
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
setTasks(data);
|
|
} else {
|
|
throw new Error('Failed to fetch tasks.');
|
|
}
|
|
} catch (error) {
|
|
setIsError(true);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const createTask = async (taskData: Partial<Task>) => {
|
|
try {
|
|
const response = await fetch('/api/task', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'include',
|
|
body: JSON.stringify(taskData),
|
|
});
|
|
if (response.ok) {
|
|
const newTask = await response.json();
|
|
setTasks((prevTasks) => [newTask, ...prevTasks]);
|
|
} else {
|
|
throw new Error('Failed to create task.');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating task:', error);
|
|
}
|
|
};
|
|
|
|
const updateTask = async (taskId: number, taskData: Partial<Task>) => {
|
|
try {
|
|
const response = await fetch(`/api/task/${taskId}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'include',
|
|
body: JSON.stringify(taskData),
|
|
});
|
|
if (response.ok) {
|
|
const updatedTask = await response.json();
|
|
setTasks((prevTasks) =>
|
|
prevTasks.map((task) => (task.id === taskId ? updatedTask : task))
|
|
);
|
|
} else {
|
|
throw new Error('Failed to update task.');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error updating task:', error);
|
|
}
|
|
};
|
|
|
|
const deleteTask = async (taskId: number) => {
|
|
try {
|
|
const response = await fetch(`/api/task/${taskId}`, {
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
});
|
|
if (response.ok) {
|
|
setTasks((prevTasks) => prevTasks.filter((task) => task.id !== taskId));
|
|
} else {
|
|
throw new Error('Failed to delete task.');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting task:', error);
|
|
}
|
|
};
|
|
|
|
const mutateTasks = fetchTasks;
|
|
|
|
return { tasks, isLoading, isError, fetchTasks, mutateTasks, createTask, updateTask, deleteTask };
|
|
};
|
|
|
|
export default useManageTasks;
|