feat: CRUD tasks
This commit is contained in:
@@ -54,3 +54,40 @@ export interface ICreateTaskResponse {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ITaskDetails {
|
||||||
|
profile: string;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
subject: string;
|
||||||
|
taskType: string;
|
||||||
|
dateTime_due: string;
|
||||||
|
remind_before_days: number;
|
||||||
|
repeat_reminder: number;
|
||||||
|
reminder_time: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IDeleteTaskResponse {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IEditTaskResponse {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
profile: string;
|
||||||
|
task: {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
subject: string;
|
||||||
|
taskType: string;
|
||||||
|
dateTime_due: string;
|
||||||
|
isCompleted: boolean;
|
||||||
|
reminder?: {
|
||||||
|
remind_before_days: number;
|
||||||
|
repeat_interval: number;
|
||||||
|
reminder_time: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,7 +28,15 @@ import { Checkbox, CheckboxPassThroughMethodOptions } from "primereact/checkbox"
|
|||||||
import { Nullable } from "primereact/ts-helpers";
|
import { Nullable } from "primereact/ts-helpers";
|
||||||
import { SubmitHandler, useForm } from "react-hook-form";
|
import { SubmitHandler, useForm } from "react-hook-form";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
import { ITask, ITaskForm, IApiResponse, ICreateTaskResponse } from "./profile_tasks.dto";
|
import {
|
||||||
|
ITask,
|
||||||
|
ITaskForm,
|
||||||
|
IApiResponse,
|
||||||
|
ICreateTaskResponse,
|
||||||
|
ITaskDetails,
|
||||||
|
IDeleteTaskResponse,
|
||||||
|
IEditTaskResponse,
|
||||||
|
} from "./profile_tasks.dto";
|
||||||
import classes from "./profile_tasks.module.scss";
|
import classes from "./profile_tasks.module.scss";
|
||||||
import apiClient from "@/services/api";
|
import apiClient from "@/services/api";
|
||||||
|
|
||||||
@@ -68,11 +76,9 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
const response = await apiClient<IApiResponse>("/api/tasks/view_tasks/");
|
const response = await apiClient<IApiResponse>("/api/tasks/view_tasks/");
|
||||||
|
|
||||||
// Update choices
|
|
||||||
setSubjectChoices(response.subject_choices);
|
setSubjectChoices(response.subject_choices);
|
||||||
setTaskTypeChoices(response.task_type_choices);
|
setTaskTypeChoices(response.task_type_choices);
|
||||||
|
|
||||||
// Convert API tasks to our format
|
|
||||||
const convertedTasks: ITask[] = response.days.flatMap((day) =>
|
const convertedTasks: ITask[] = response.days.flatMap((day) =>
|
||||||
day.tasks.map((apiTask) => ({
|
day.tasks.map((apiTask) => ({
|
||||||
id: apiTask.id.toString(),
|
id: apiTask.id.toString(),
|
||||||
@@ -105,7 +111,6 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update example_tags to use the choices from the API
|
|
||||||
const example_tags = useMemo(
|
const example_tags = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
first: Object.keys(subjectChoices),
|
first: Object.keys(subjectChoices),
|
||||||
@@ -161,13 +166,16 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
throw new Error(response.message);
|
throw new Error(response.message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await apiClient(`/api/tasks/update_task/${editContent?.id}/`, {
|
const response = await apiClient<IEditTaskResponse>(`/api/tasks/edit_task/${editContent?.id}/`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(taskData),
|
body: JSON.stringify(taskData),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
throw new Error(response.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh tasks after saving
|
|
||||||
await fetchTasks();
|
await fetchTasks();
|
||||||
|
|
||||||
if (isCreating) setIsOpen(false);
|
if (isCreating) setIsOpen(false);
|
||||||
@@ -253,11 +261,14 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
if (!editContent) return;
|
if (!editContent) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await apiClient(`/api/delete_task/${editContent.id}/`, {
|
const response = await apiClient<IDeleteTaskResponse>(`/api/tasks/delete_task/${editContent.id}/`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Refresh tasks after deletion
|
if (!response.success) {
|
||||||
|
throw new Error(response.message);
|
||||||
|
}
|
||||||
|
|
||||||
await fetchTasks();
|
await fetchTasks();
|
||||||
|
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
@@ -267,7 +278,6 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update the task marking functionality
|
|
||||||
const handleMarkTask = async (taskId: string, isCompleted: boolean) => {
|
const handleMarkTask = async (taskId: string, isCompleted: boolean) => {
|
||||||
try {
|
try {
|
||||||
await apiClient(`/api/update_task/${taskId}/`, {
|
await apiClient(`/api/update_task/${taskId}/`, {
|
||||||
@@ -275,7 +285,6 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
body: JSON.stringify({ isCompleted }),
|
body: JSON.stringify({ isCompleted }),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Refresh tasks after marking
|
|
||||||
await fetchTasks();
|
await fetchTasks();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to mark task:", error);
|
console.error("Failed to mark task:", error);
|
||||||
@@ -285,7 +294,6 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
const filteredTasks = useMemo(() => {
|
const filteredTasks = useMemo(() => {
|
||||||
let filtered = tasks;
|
let filtered = tasks;
|
||||||
|
|
||||||
// Фильтрация по поиску
|
|
||||||
if (searchQuery) {
|
if (searchQuery) {
|
||||||
filtered = filtered.filter(
|
filtered = filtered.filter(
|
||||||
(task) =>
|
(task) =>
|
||||||
@@ -294,7 +302,6 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Фильтрация по тегам
|
|
||||||
if (filterTags.first || filterTags.second) {
|
if (filterTags.first || filterTags.second) {
|
||||||
filtered = filtered.filter(
|
filtered = filtered.filter(
|
||||||
(task) =>
|
(task) =>
|
||||||
@@ -313,6 +320,30 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
if (searchInputRef.current && openSearchModal) searchInputRef.current.focus();
|
if (searchInputRef.current && openSearchModal) searchInputRef.current.focus();
|
||||||
}, [searchInputRef, openSearchModal]);
|
}, [searchInputRef, openSearchModal]);
|
||||||
|
|
||||||
|
const handleViewTask = async (taskId: string) => {
|
||||||
|
try {
|
||||||
|
const taskDetails = await apiClient<ITaskDetails>(`/api/tasks/view_task/${taskId}/`);
|
||||||
|
|
||||||
|
const task: ITask = {
|
||||||
|
id: taskId,
|
||||||
|
name: taskDetails.title,
|
||||||
|
checked: false,
|
||||||
|
date: new Date(taskDetails.dateTime_due),
|
||||||
|
description: taskDetails.description,
|
||||||
|
tags: [taskDetails.subject, taskDetails.taskType],
|
||||||
|
new: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
setIsOpen(true);
|
||||||
|
setIsEdit(true);
|
||||||
|
setEditContent(task);
|
||||||
|
setCalendarDate(task.date);
|
||||||
|
setIsEditModal(false);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch task details:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={classes.container}>
|
<div class={classes.container}>
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
@@ -582,12 +613,7 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
key={task.id}
|
key={task.id}
|
||||||
checked={task.checked}
|
checked={task.checked}
|
||||||
overdue={task.date < new Date()}
|
overdue={task.date < new Date()}
|
||||||
onClick={() => {
|
onClick={() => handleViewTask(task.id)}
|
||||||
setIsOpen(true);
|
|
||||||
setIsEdit(true);
|
|
||||||
setEditContent(task);
|
|
||||||
setCalendarDate(task.date);
|
|
||||||
}}
|
|
||||||
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -603,12 +629,7 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
name={task.name}
|
name={task.name}
|
||||||
key={task.id}
|
key={task.id}
|
||||||
checked={task.checked}
|
checked={task.checked}
|
||||||
onClick={() => {
|
onClick={() => handleViewTask(task.id)}
|
||||||
setIsOpen(true);
|
|
||||||
setIsEdit(true);
|
|
||||||
setEditContent(task);
|
|
||||||
setCalendarDate(task.date);
|
|
||||||
}}
|
|
||||||
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -622,12 +643,7 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
name={task.name}
|
name={task.name}
|
||||||
key={task.id}
|
key={task.id}
|
||||||
checked={task.checked}
|
checked={task.checked}
|
||||||
onClick={() => {
|
onClick={() => handleViewTask(task.id)}
|
||||||
setIsOpen(true);
|
|
||||||
setIsEdit(true);
|
|
||||||
setEditContent(task);
|
|
||||||
setCalendarDate(task.date);
|
|
||||||
}}
|
|
||||||
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -674,12 +690,7 @@ const ProfileTasks: FunctionComponent = () => {
|
|||||||
key={task.id}
|
key={task.id}
|
||||||
checked={task.checked}
|
checked={task.checked}
|
||||||
overdue={task.date < new Date()}
|
overdue={task.date < new Date()}
|
||||||
onClick={() => {
|
onClick={() => handleViewTask(task.id)}
|
||||||
setIsOpen(true);
|
|
||||||
setIsEdit(true);
|
|
||||||
setEditContent(task);
|
|
||||||
setCalendarDate(task.date);
|
|
||||||
}}
|
|
||||||
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
onMarkClick={() => handleMarkTask(task.id, !task.checked)}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user