diff --git a/src/components/ModalCalendar.tsx b/src/components/ModalCalendar.tsx index 3f1e596..891e100 100644 --- a/src/components/ModalCalendar.tsx +++ b/src/components/ModalCalendar.tsx @@ -199,4 +199,6 @@ const ModalCalendar: FunctionComponent = ({ isOpen, setIsOpe ); }; +ModalCalendar.displayName = "AHModalCalendar"; + export default ModalCalendar; diff --git a/src/pages/profile_tasks.dto.ts b/src/pages/profile_tasks.dto.ts index 08b55de..40d8d52 100644 --- a/src/pages/profile_tasks.dto.ts +++ b/src/pages/profile_tasks.dto.ts @@ -6,3 +6,7 @@ export interface ITask { description: string; tags: string[]; } + +export interface ITaskForm extends Omit { + date: string; +} diff --git a/src/pages/profile_tasks.tsx b/src/pages/profile_tasks.tsx index af707f2..e22b9bf 100644 --- a/src/pages/profile_tasks.tsx +++ b/src/pages/profile_tasks.tsx @@ -5,6 +5,7 @@ import Button from "@/components/ui/Button"; import ModalWindow from "@/components/ui/Modal"; import { withTitle } from "@/constructors/Component"; import { UrlsTitle } from "@/enums/urls"; +import { cn } from "@/utils/class-merge"; import { PlusIcon } from "@heroicons/react/20/solid"; import { BookmarkIcon, @@ -12,13 +13,15 @@ import { CalendarDaysIcon, DocumentDuplicateIcon, FunnelIcon, + InboxArrowDownIcon, MagnifyingGlassIcon, PencilIcon, } from "@heroicons/react/24/outline"; import { FunctionComponent } from "preact"; import { useEffect, useMemo, useRef, useState } from "preact/hooks"; import { Nullable } from "primereact/ts-helpers"; -import { ITask } from "./profile_tasks.dto"; +import { SubmitHandler, useForm } from "react-hook-form"; +import { ITask, ITaskForm } from "./profile_tasks.dto"; import classes from "./profile_tasks.module.scss"; const example_tasks: ITask[] = [ @@ -49,6 +52,15 @@ const example_tasks: ITask[] = [ ]; const ProfileTasks: FunctionComponent = () => { + const [openModal, setIsOpen] = useState(false); // Открыта модалка + const [openModalCalendar, setOpenModalCalendar] = useState(false); // Открыта модалка календаря + const [isEdit, setIsEdit] = useState(false); // Открыта задача + const [isEditModal, setIsEditModal] = useState(false); // Включено редактирование задачи + const [isCreating, setIsCreating] = useState(false); // Включено создание задачи + const [editContent, setEditContent] = useState(null); // Содержимое редактируемой задачи + const taskNameRef = useRef(null); + const taskDescriptionRef = useRef(null); + const [calendarDate, setCalendarDate] = useState>(); const getDate = useMemo(() => { const date = new Date(); const formatter = new Intl.DateTimeFormat("ru-RU", { month: "long", day: "numeric" }); @@ -64,23 +76,39 @@ const ProfileTasks: FunctionComponent = () => { useEffect(() => { localStorage.setItem("tasks", JSON.stringify(tasks)); }, [tasks]); - const [openModal, setIsOpen] = useState(false); - const [openModalCalendar, setOpenModalCalendar] = useState(false); - const [isEdit, setIsEdit] = useState(false); - const [isCreating, setIsCreating] = useState(false); - const [editContent, setEditContent] = useState(null); - const taskNameRef = useRef(null); - const taskDescriptionRef = useRef(null); - const [calendarDate, setCalendarDate] = useState>(); + + const { + handleSubmit, + register, + reset, + formState: { errors }, + } = useForm({ + defaultValues: { + tags: [], + }, + }); + const saveEdited: SubmitHandler = (data) => { + if (!editContent) return; + const editedTask: ITask = { ...data, date: new Date(data.date) }; + setTasks(tasks.map((task) => (task.id === editedTask.id ? editedTask : task))); + setIsEditModal(false); + }; + useEffect(() => { + if (editContent) reset({ ...editContent, date: editContent.date.toISOString().slice(0, 16) }); + else reset(); + }, [editContent]); + useEffect(() => { + if (calendarDate && editContent) setEditContent({ ...editContent, date: calendarDate }); + }, [calendarDate]); return (
{ - if (isEdit) setCalendarDate(null); + if (isEdit && !isEditModal) setCalendarDate(null); }} - onChange={(e) => isCreating && setCalendarDate(e.value)} + onChange={(e) => (isCreating || isEditModal) && setCalendarDate(e.value)} value={calendarDate!} /> { setIsEdit(false); setEditContent(null); setIsCreating(false); + setIsEditModal(false); setCalendarDate(null); }} > {isEdit && editContent && ( -
+
e.preventDefault()}>
-
-

{editContent.name}

-

{editContent.description}

+
+ +