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"; import ImagePreview from "../common/ImagePreview"; import { useAuditStatus, useSubmitTaskComment } from "../../hooks/useTasks"; import Label from "../common/Label"; const ReportTaskComments = ({ commentsData, closeModal, actionAllow = false, handleCloseAction, }) => { const defaultCompletedTask = Number(commentsData?.completedTask) || 0; const schema = actionAllow ? z.object({ comment: z.string().min(1, "Comment cannot be empty"), workStatus: z .string() .nonempty({ message: "Audit status is required" }) .default(""), approvedTask: z.preprocess( (val) => val === "" || val === null || Number.isNaN(val) ? undefined : Number(val), z .number({ required_error: "Completed Work must be a number", invalid_type_error: "Completed Work must be a number", }) .min(0, "Completed Work must be greater than 0") .max(defaultCompletedTask, { message: `Completed task cannot exceed: ${defaultCompletedTask}`, }) ), }) : z.object({ comment: z.string().min(1, "Comment cannot be empty"), }); const [loading, setloading] = useState(false); const [comments, setComment] = useState([]); const { status, loading: auditStatusLoading } = useAuditStatus(); const [ IsNeededSubTask, setIsNeededSubTask ] = useState( false ); const { submitComment, isPending } = useSubmitTaskComment({ actionAllow, onSuccessCallback: (data) => { setComment((prev) => [...prev, data]); reset(); if ( actionAllow ) { handleCloseAction(IsNeededSubTask); } }, }); const { watch, register, handleSubmit, setValue, formState: { errors }, reset, } = useForm({ resolver: zodResolver(schema), }); const containerRef = useRef(null); const firstRender = useRef(true); useEffect(() => { const taskList = getCachedData("taskList"); if (taskList && taskList.data && commentsData?.id) { const currentTask = taskList.data.find( (task) => task.id === commentsData.id ); if (currentTask && currentTask.comments) { setComment(currentTask.comments); } else { setComment(commentsData?.comments || []); } } else { setComment(commentsData?.comments || []); } firstRender.current = true; }, [commentsData]); useEffect(() => { if (!firstRender.current && containerRef.current) { containerRef.current.scrollTop = containerRef.current.scrollHeight; } else { firstRender.current = false; } }, [comments]); const onSubmit = (formData) => { submitComment({ data: formData, commentsData }); }; const selectedAuditStatus = watch("workStatus"); useEffect(() => { reset({ approvedTask: defaultCompletedTask || 0, }); }, [defaultCompletedTask]); const completed_Task = watch("approvedTask") return (
Activity Summary
Location:
{`${commentsData?.workItem?.workArea?.floor?.building?.name}`}{" "} {" "} {`${commentsData?.workItem?.workArea?.floor?.floorName}`}{" "} {" "} {`${commentsData?.workItem?.workArea?.areaName}`}{" "} {" "} {`${commentsData?.workItem?.activityMaster?.activityName}`}
{commentsData?.assignedBy && (
Assigned By: {commentsData.assignedBy.firstName}{" "} {commentsData.assignedBy.lastName}
)}
{commentsData.reportedBy && (
Reported By:
{commentsData.reportedBy.firstName + " " + commentsData.reportedBy.lastName}
)}
Planned Work: {commentsData?.plannedTask}{" "} {commentsData?.workItem?.activityMaster?.unitOfMeasurement}
{commentsData?.reportedDate != null && (
Completed Work: {commentsData?.completedTask}{" "} {commentsData?.workItem?.activityMaster?.unitOfMeasurement}
)}

Team :

{commentsData?.teamMembers?.map((member, idx) => ( {member?.firstName + " " + member?.lastName} ))}
Note:
{commentsData?.description}
{commentsData?.approvedBy && ( <>
{commentsData.approvedBy && (
Approved By:
{commentsData.approvedBy.firstName + " " + commentsData.approvedBy.lastName}
)}
{commentsData?.workStatus != null && (
Work Status : {commentsData?.workStatus.name} {/* {commentsData?.} */}
)}
Total Approved : {commentsData?.completedTask}
)} {commentsData?.reportedPreSignedUrls?.length > 0 && ( <>

Attachment :

)}
{actionAllow && !commentsData.approvedBy && ( <>
{errors.approvedTask && (

{errors.approvedTask.message}

)}
{errors.workStatus && (
{errors.workStatus.message}
)}
)}