From fb774f45d186f4844fc497cfe1fa02ebcb26ddd1 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Fri, 18 Apr 2025 19:46:33 +0530 Subject: [PATCH] modified cache for immiedtky reflacted on screen. --- .../Activities/ReportTaskComments.jsx | 183 +++++++++--------- 1 file changed, 95 insertions(+), 88 deletions(-) diff --git a/src/components/Activities/ReportTaskComments.jsx b/src/components/Activities/ReportTaskComments.jsx index f116eb07..0c2f7a43 100644 --- a/src/components/Activities/ReportTaskComments.jsx +++ b/src/components/Activities/ReportTaskComments.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useState, useRef } from "react"; import { useProfile } from "../../hooks/useProfile"; import moment from "moment"; import { useForm } from "react-hook-form"; @@ -6,6 +6,9 @@ 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"), @@ -15,6 +18,7 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { const [loading, setloading] = useState(false); const { profile } = useProfile(); const [comments, setComment] = useState([]); + const [bgClass, setBgClass] = useState(""); const { register, handleSubmit, @@ -24,10 +28,17 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { resolver: zodResolver(schema), }); + const containerRef = useRef(null); useEffect(() => { setComment(commentsData?.comments); }, [commentsData]); - const isLoggedUser = (usrId) => profile?.employeeInfo.id; + + useEffect(() => { + if (containerRef.current) { + containerRef.current.scrollTop = containerRef.current.scrollHeight; + } + }, [comments]); + const isLoggedUser = (usrId) => profile?.employeeInfo.id === usrId; const onSubmit = async (data) => { let sendComment = { @@ -35,117 +46,113 @@ const ReportTaskComments = ({ commentsData, closeModal }) => { taskAllocationId: commentsData?.id, commentDate: new Date().toISOString(), }; - try { setloading(true); - // const resp = await TasksRepository.taskComments( sendComment ); - // console.timeLog( resp ) + 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 (err) { - setloading(false); + // closeModal(); + } catch (error) { + setloading( false ); + console.log(error) showToast(error.response.data?.message || "Something wrong", "error"); } }; return (
-
+
-
+

{`${ commentsData?.workItem?.workArea?.floor?.building?.name }`} {`${ commentsData?.workItem?.workArea?.floor?.floorName } `} {`${ commentsData?.workItem?.workArea?.areaName }`}{` ${commentsData?.workItem?.activityMaster?.activityName}`}

- { - comments && comments.map( ( data ) => - ( -
-
+
    + {comments && + comments.map((data) => { + const fullName = `${data?.employee?.firstName} ${data?.employee?.lastName}`; + const bgClass = getBgClassFromHash(fullName); + return ( +
  • +
    - - {data?.employee?.firstName.slice(0,1)} + + {`${data?.employee?.firstName.slice(0,1)} ${data?.employee?.lastName.slice(0,1)}`}
    -
    -

    - { `${data?.employee?.firstName} ${data?.employee?.lastName}`} -

    - { moment(data?.commentDate).fromNow()} -
    -
    -

    { data?.comment - }

    -
- ))} - {/* by other users */} - {/*
-
-
- - M - -
-
-

- Mahajan -

- 2 hour ago -
-
-

Stylized implementation of HTML’s element for abbreviations and acronyms to show the expanded version on hover. Abbreviations have a default underline and gain a help cursor to provide additional context on hov

-
*/} - {/* by login usrer */} - {/*
-
-
- - M - -
-
-

- Pramod Mahajan +

+

{fullName}

+

+ + {moment.utc(data?.commentDate).local().fromNow()} + +

+
+
+

+ {data?.comment}

- 2 hour ago -
-
-

Stylized implementation of HTML’s element for abbreviations and acronyms to show the expanded version on hover. Abbreviations have a default underline and gain a help cursor to provide additional context on hov

-
*/} -
-