import React, { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { useDispatch } from "react-redux"; import { apiRequest } from "@api/request"; import { urlForLocal } from "@utils/helper"; import { modalToggle, setProjectBoardFetch } from "@redux/projectsTrackerSlice"; import { getCorrectDate } from "@components/Calendar/calendarHelper"; import TrackerModal from "@components/UI/TrackerModal/TrackerModal"; import ModalLayout from "@components/Common/ModalLayout/ModalLayout"; import BaseButton from "@components/Common/BaseButton/BaseButton"; import category from "assets/icons/category.svg"; import watch from "assets/icons/watch.svg"; import file from "assets/icons/fileModal.svg"; import arrow from "assets/icons/arrows/arrowStart.png"; import link from "assets/icons/link.svg"; import archive from "assets/icons/archive.svg"; import del from "assets/icons/delete.svg"; import edit from "assets/icons/edit.svg"; import send from "assets/icons/send.svg"; import plus from "assets/icons/plus.svg"; import fullScreen from "assets/icons/arrows/inFullScreen.svg"; import close from "assets/icons/closeProjectPersons.svg"; import "./ModalTicket.scss"; export const ModalTiсket = ({ active, setActive, task, projectId, projectName, projectUsers, }) => { const dispatch = useDispatch(); const [addSubtask, setAddSubtask] = useState(false); const [editOpen, setEditOpen] = useState(false); const [inputsValue, setInputsValue] = useState({ title: task.title, description: task.description, comment: "", }); const [comments, setComments] = useState([]); const [commentsEditOpen, setCommentsEditOpen] = useState({}); const [commentsEditText, setCommentsEditText] = useState({}); const [dropListOpen, setDropListOpen] = useState(false); const [dropListMembersOpen, setDropListMembersOpen] = useState(false); const [executor, setExecutor] = useState(task.executor); const [members, setMembers] = useState(task.taskUsers); const [users, setUsers] = useState([]); function deleteTask() { apiRequest("/task/update-task", { method: "PUT", data: { task_id: task.id, status: 0, }, }).then((res) => { setActive(false); dispatch(setProjectBoardFetch(projectId)); }); } function editTask() { apiRequest("/task/update-task", { method: "PUT", data: { task_id: task.id, title: inputsValue.title, description: inputsValue.description, }, }).then((res) => { dispatch(setProjectBoardFetch(projectId)); }); } function createComment() { apiRequest("/comment/create", { method: "POST", data: { text: inputsValue.comment, entity_type: 2, entity_id: task.id, }, }).then((res) => { let newComment = res; newComment.created_at = new Date(); setInputsValue((prevValue) => ({ ...prevValue, comment: "" })); setComments((prevValue) => [...prevValue, newComment]); setCommentsEditOpen((prevValue) => ({ ...prevValue, [res.id]: false })); setCommentsEditText((prevValue) => ({ ...prevValue, [res.id]: res.text, })); }); } function deleteComment(commentId) { apiRequest("/comment/update", { method: "PUT", data: { comment_id: commentId, status: 0, }, }).then((res) => { setComments((prevValue) => prevValue.filter((item) => item.id !== commentId) ); }); } function editComment(commentId) { apiRequest("/comment/update", { method: "PUT", data: { comment_id: commentId, text: commentsEditText[commentId], }, }).then((res) => {}); } function taskExecutor(person) { apiRequest("/task/update-task", { method: "PUT", data: { task_id: task.id, executor_id: person.user_id, }, }).then((res) => { setDropListOpen(false); setExecutor(res.executor); }); } function deleteTaskExecutor() { apiRequest("/task/update-task", { method: "PUT", data: { task_id: task.id, executor_id: 0, }, }).then((res) => { setExecutor(null); }); } function addMember(person) { apiRequest("/task/add-user-to-task", { method: "POST", data: { task_id: task.id, user_id: person.user_id, }, }).then((res) => { setDropListMembersOpen(false); setMembers((prevValue) => [...prevValue, res]); }); } function deleteMember(person) { apiRequest("/task/del-user", { method: "DELETE", data: { task_id: task.id, user_id: person.user_id, }, }).then((res) => { setMembers(members.filter((item) => item.user_id !== person.user_id)); }); } useEffect(() => { apiRequest( `/comment/get-by-entity?entity_type=2&entity_id=${task.id}` ).then((res) => { setComments(res); res.forEach((item) => { setCommentsEditOpen((prevValue) => ({ ...prevValue, [item.id]: false, })); setCommentsEditText((prevValue) => ({ ...prevValue, [item.id]: item.text, })); }); }); }, []); useEffect(() => { let ids = members.map((user) => user.user_id); setUsers( projectUsers.reduce((acc, cur) => { if (!ids.includes(cur.user_id)) acc.push(cur); return acc; }, []) ); }, [members]); return ( <>

Проект: {projectName}

Задача {editOpen ? ( { setInputsValue((prevValue) => ({ ...prevValue, title: e.target.value, })); }} /> ) : (
{inputsValue.title}
)}
{editOpen ? ( { setInputsValue((prevValue) => ({ ...prevValue, description: e.target.value, })); }} /> ) : (

{inputsValue.description}

)} {/**/}

{ dispatch(modalToggle("addSubtask")); setAddSubtask(true); }} styles={"tasks__button"} > Добавить под задачу

Загрузить файл {0} Файлов

{ setInputsValue((prevValue) => ({ ...prevValue, comment: e.target.value, })); }} />
{comments.map((comment) => { return (
{getCorrectDate(comment.created_at)}
edit { if (commentsEditOpen[comment.id]) { editComment(comment.id); } setCommentsEditOpen((prevValue) => ({ ...prevValue, [comment.id]: !prevValue[comment.id], })); }} />
delete deleteComment(comment.id)} />
{commentsEditOpen[comment.id] ? ( { setCommentsEditText((prevValue) => ({ ...prevValue, [comment.id]: e.target.value, })); }} /> ) : (

{commentsEditText[comment.id]}

)}
); })}
setActive(false)}> {task.title}

Создатель : {task.user?.fio}

{executor ? (

Исполнитель: {executor.fio}

avatar deleteTaskExecutor()} />
) : (
Добавить исполнителя {dropListOpen && (
setDropListOpen(false)} /> {projectUsers.map((person) => { return (
taskExecutor(person)} > {person.user.fio}
); })}
)}
)} {Boolean(members.length) && (

Участники:

{members.map((member) => { return (

{member.fio}

deleteMember(member)} />
); })}
)}
Добавить участников {dropListMembersOpen && (
setDropListMembersOpen(false)} /> {users.length ? ( users.map((person) => { return (
addMember(person)} > {person.user.fio}
); }) ) : (

Нет пользователей

)}
)}
Длительность :

{"0:00:00"}

{ if (editOpen) { setEditOpen(!editOpen); editTask(); } else { setEditOpen(!editOpen); } }} >

{editOpen ? "сохранить" : "редактировать"}

ссылка на проект

в архив

удалить

); }; export default ModalTiсket;