diff --git a/src/components/Activities/ReportTask.jsx b/src/components/Activities/ReportTask.jsx index fe54d471..13b7228a 100644 --- a/src/components/Activities/ReportTask.jsx +++ b/src/components/Activities/ReportTask.jsx @@ -76,20 +76,12 @@ useEffect(() => { }; return ( -
-
-
- -
+ +
+
+

Report Task

+
-
-
-
+ ); }; diff --git a/src/components/Activities/ReportTaskComments.jsx b/src/components/Activities/ReportTaskComments.jsx index a63a4fe6..f5db9434 100644 --- a/src/components/Activities/ReportTaskComments.jsx +++ b/src/components/Activities/ReportTaskComments.jsx @@ -9,35 +9,60 @@ import Avatar from "../common/Avatar"; import { getBgClassFromHash } from "../../utils/projectStatus"; import { cacheData, getCachedData } from "../../slices/apiDataManager"; import ImagePreview from "../common/ImagePreview"; +import { useAuditStatus } from "../../hooks/useTasks"; -const schema = z.object({ - comment: z.string().min(1, "Comment cannot be empty"), -}); +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"), + }); -/** - * 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 { status, loading: auditStatusLoading } = useAuditStatus(); + const [IsNeededSubTask, setIsNeededSubTask] = useState(false); + const { + watch, register, handleSubmit, + setValue, formState: { errors }, - reset, // Destructure reset from useForm + reset, } = useForm({ resolver: zodResolver(schema), }); const containerRef = useRef(null); const firstRender = useRef(true); - useEffect(() => { const taskList = getCachedData("taskList"); if (taskList && taskList.data && commentsData?.id) { @@ -64,42 +89,53 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { }, [comments]); const onSubmit = async (data) => { - let sendComment = { + let payload = { ...data, - taskAllocationId: commentsData?.id, - commentDate: new Date().toISOString(), + [actionAllow ? "id" : "taskAllocationId"]: commentsData?.id, + ...(actionAllow ? {} : { commentDate: new Date().toISOString() }), }; + try { setloading(true); - const resp = await TasksRepository.taskComments(sendComment); + const resp = actionAllow + ? await TasksRepository.auditTask(payload) + : await TasksRepository.taskComments(payload); setComment((prevItems) => [...prevItems, resp.data]); const taskList = getCachedData("taskList"); - if (taskList && taskList.data) { - 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; - }); + if (actionAllow) { + handleCloseAction(IsNeededSubTask); + showToast( + "Review submitted successfully. Record has been updated.", + "success" + ); + } else { + if (taskList && taskList.data) { + 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, - }); + cacheData("taskList", { + data: updatedTaskList, + projectId: taskList.projectId, + }); + } + showToast("Successfully Sent", "success"); } reset(); setloading(false); - showToast("Successfully Sent", "success"); } catch (error) { setloading(false); showToast( @@ -108,60 +144,101 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { ); } }; + + const selectedAuditStatus = watch("workStatus"); + + useEffect(() => { + reset({ + approvedTask: defaultCompletedTask || 0, + }); + }, [ defaultCompletedTask ] ); return ( -
+
Activity Summary
-

- Location : - +

+
+ + Location: +
+
{`${commentsData?.workItem?.workArea?.floor?.building?.name}`}{" "} {" "} - {`${commentsData?.workItem?.workArea?.floor?.floorName} `}{" "} - - {`${commentsData?.workItem?.workArea?.areaName}`} - - {` ${commentsData?.workItem?.activityMaster?.activityName}`} - -

+ {`${commentsData?.workItem?.workArea?.floor?.floorName}`}{" "} + {" "} + {`${commentsData?.workItem?.workArea?.areaName}`}{" "} + {" "} + {`${commentsData?.workItem?.activityMaster?.activityName}`} +
+
-

- Assigned By : - - {commentsData?.assignedBy?.firstName + - " " + - commentsData?.assignedBy?.lastName} - {" "} -

+
+
+ {commentsData?.assignedBy && ( +
+ + Assigned By: + + {commentsData.assignedBy.firstName}{" "} + {commentsData.assignedBy.lastName} + +
+ )} +
-

- Reported 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} + +
+ )} +
+
+ -

- Planned Work: {commentsData?.plannedTask}{" "} - {commentsData?.workItem?.activityMaster?.unitOfMeasurement} -

- {commentsData?.reportedDate != null && ( -

- {" "} - Completed Work : {commentsData?.completedTask}{" "} - {commentsData?.workItem?.activityMaster?.unitOfMeasurement} -

- )} - {!commentsData?.reportedDate && ( -

Completed Work :  -

- )}
-

Team :

+

+ Team : +

{commentsData?.teamMembers?.map((member, idx) => ( @@ -175,50 +252,174 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { ))}
+
- Note: -
+
+ Note: +
+
{commentsData?.description}
- {commentsData?.reportedPreSignedUrls?.length > 0 && ( -
-

Attachment

- + + {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} +
+ )} +
+
+ + + )} +