import React, { useEffect, useState, useRef } from "react"; import moment from "moment"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { TasksRepository } from "../../repositories/TaskRepository"; import showToast from "../../services/toastService"; import Avatar from "../common/Avatar"; import { getBgClassFromHash } from "../../utils/projectStatus"; import { cacheData, getCachedData } from "../../slices/apiDataManager"; const schema = z.object({ comment: z.string().min(1, "Comment cannot be empty"), }); /** * ReportTaskComments component for displaying and adding comments to a task. * It also shows a summary of the activity and task details. * * @param {object} props - The component props. * @param {object} props.commentsData - Data related to the task and its comments, including the description. * @param {function} props.closeModal - Callback function to close the modal. */ const ReportTaskComments = ({ commentsData, closeModal }) => { const [loading, setloading] = useState(false); const [comments, setComment] = useState([]); const [bgClass, setBgClass] = useState(""); const { register, handleSubmit, formState: { errors }, reset, } = useForm({ resolver: zodResolver(schema), }); const containerRef = useRef(null); const firstRender = useRef(true); useEffect(() => { setComment(commentsData?.comments); }, [commentsData]); // Scroll logic: scroll to bottom when new comments are added useEffect(() => { if (!firstRender.current && containerRef.current) { containerRef.current.scrollTop = containerRef.current.scrollHeight; } else { firstRender.current = false; // Mark the first render as complete } }, [comments]); // Run this when comments array is updated const onSubmit = async (data) => { let sendComment = { ...data, taskAllocationId: commentsData?.id, commentDate: new Date().toISOString(), }; try { setloading(true); const resp = await TasksRepository.taskComments(sendComment); setComment((prevItems) => [...prevItems, resp.data]); const taskList = getCachedData("taskList"); const updatedTaskList = taskList.data.map((task) => { if (task.id === resp.data.taskAllocationId) { const existingComments = Array.isArray(task.comments) ? task.comments : []; return { ...task, comments: [...existingComments, resp.data], }; } return task; }); cacheData("taskList", { data: updatedTaskList, projectId: taskList.projectId, }); reset(); setloading(false); showToast("Successfully Sent", "success"); // closeModal(); } catch (error) { setloading(false); showToast(error.response.data?.message || "Something went wrong", "error"); } }; console.log("Kartik", commentsData) return (
{commentsData?.workItem?.workArea?.floor?.building?.description}
Assigned By : {commentsData?.assignedBy.firstName + " " + commentsData?.assignedBy.lastName} {" "}
Loaction : {`${commentsData?.workItem?.workArea?.floor?.building?.name}`}{" "} {" "} {`${commentsData?.workItem?.workArea?.floor?.floorName} `}{" "} {`${commentsData?.workItem?.workArea?.areaName}`} {` ${commentsData?.workItem?.activityMaster?.activityName}`}
Planned Work: {commentsData?.plannedTask}
{" "} Completed Work : {commentsData?.completedTask}
Team :
{fullName}
{moment.utc(data?.commentDate).local().fromNow()}
{data?.comment}