import React, { useState } from "react"; import TrackerTaskSubComment from "../TrackerTaskComment/TrackerTaskComment"; import { apiRequest } from "../../api/request"; import {urlForLocal} from "../../helper"; import {getCorrectDate} from "../Calendar/calendarHelper"; import edit from "../../images/edit.svg"; import del from "../../images/delete.svg"; import accept from "../../images/accept.png"; export const TrackerTaskComment = ({ taskId, comment, commentDelete, addSubComment, subCommentDelete }) => { const [commentsEditOpen, setCommentsEditOpen] = useState(false) const [commentsEditText, setCommentsEditText] = useState(comment.text) const [subCommentsCreateOpen, setSubCommentsCreateOpen] = useState(false) const [subCommentsCreateText, setSubCommentsCreateText] = useState('') function editComment() { if (commentsEditText === comment.text) return apiRequest("/comment/update", { method: "PUT", data: { comment_id: comment.id, text: commentsEditText } }).then(() => { }) } function deleteComment() { apiRequest("/comment/update", { method: "PUT", data: { comment_id: comment.id, status: 0 } }).then(() => { if (comment.parent_id) { subCommentDelete(comment) } else { commentDelete(comment) } }) } function createSubComment() { setSubCommentsCreateOpen(false) if(!subCommentsCreateText) return apiRequest("/comment/create", { method: "POST", data: { text: subCommentsCreateText, entity_type: 2, entity_id: taskId, parent_id: comment.id } }).then((res) => { let newSubComment = res newSubComment.created_at = new Date() setSubCommentsCreateText('') addSubComment(comment.id, newSubComment) }) } return (
avatar

{comment.user.fio}

{getCorrectDate(comment.created_at)} {comment.user_id === Number(localStorage.getItem('id')) && <>
edit { if (commentsEditOpen) { editComment() } setCommentsEditOpen(!commentsEditOpen) }} />
delete deleteComment()} /> }
{commentsEditOpen ? { setCommentsEditText(e.target.value) }} /> :

{commentsEditText}

} {!comment.parent_id && <> { subCommentsCreateOpen ?
{ setSubCommentsCreateText(e.target.value) }}/> accept { createSubComment() }} />
: { setSubCommentsCreateOpen(true) }} className='comments__list__item__answer'>Ответить } } {Boolean(comment.subComments?.length) && comment.subComments.map((subComment) => { return }) }
) } export default TrackerTaskComment