Merge branch 'Issue_May_2W' of https://git.marcoaiot.com/admin/marco.pms.web into pramod_Task#207,

This commit is contained in:
Pramod Mahajan 2025-05-10 19:06:34 +05:30
parent 2892b803d5
commit 6bb1f98ae7
2 changed files with 151 additions and 126 deletions

View File

@ -1,22 +1,83 @@
import React, { useEffect, useState } from "react";
import WorkItem from "./WorkItem";
import { useProjectDetails } from "../../../hooks/useProjects";
import { getCachedData } from "../../../slices/apiDataManager";
import { cacheData, getCachedData } from "../../../slices/apiDataManager";
import {useDispatch} from "react-redux";
import {refreshData} from "../../../slices/localVariablesSlice";
import ProjectRepository from "../../../repositories/ProjectRepository";
import showToast from "../../../services/toastService";
const WorkArea = ({ workArea, floor, forBuilding }) => {
const [workItems, setWorkItems] = useState([]);
const [ workItems, setWorkItems ] = useState( [] );
const dispatch = useDispatch()
const [Project,setProject] = useState()
useEffect(() => {
const project = getCachedData("projectInfo");
const project = getCachedData( "projectInfo" );
setProject(project)
if (!project || !forBuilding?.id || !floor?.id || !workArea?.id) return;
const building = project.buildings?.find((b) => b.id === forBuilding.id);
const floors = building?.floors?.find((f) => f.id === floor.id);
const workAreas = floor?.workAreas?.find((wa) => wa.id === workArea.id);
setWorkItems(workAreas?.workItems || []);
}, [workArea, floor, floor]);
}, [ workArea, floor, floor ] );
const HanldeDeleteActivity = async (workItemId) => {
try
{
const updatedProject = { ...Project.data };
const response = await ProjectRepository.deleteProjectTask( workItemId);
const newProject = {
...updatedProject,
buildings: updatedProject?.buildings.map((building) =>
building?.id === building?.id
? {
...building,
floors: building?.floors?.map((floor) =>
floor.id === floor?.id
? {
...floor,
workAreas: floor.workAreas.map((workArea) =>
workArea.id === workArea?.id
? {
...workArea,
workItems: workArea.workItems.filter(
(item) =>
String(item?.workItem?.id ?? item?.id) !==
String(workItemId)
),
}
: workArea
),
}
: floor
),
}
: building
),
};
cacheData("projectInfo", {
projectId: newProject.id,
data: newProject,
} );
dispatch(refreshData(true));
showToast("Activity Deleted Successfully", "success");
} catch (error) {
const message =
error.response?.data?.message ||
error.message ||
"An unexpected error occurred";
showToast(message, "error");
}
};
return (
<React.Fragment key={workArea.id}>
<tr>
@ -70,7 +131,7 @@ const WorkArea = ({ workArea, floor, forBuilding }) => {
{/* ************************** */}
<th className="infra-activity-table-header">Progress</th>
<th className="infra-activity-table-header text-end ">
<span className="px-3"> Actions</span>
<span className="px-3">Actions</span>
</th>
</tr>
</thead>
@ -83,6 +144,7 @@ const WorkArea = ({ workArea, floor, forBuilding }) => {
forBuilding={forBuilding}
forFloor={floor}
forWorkArea={workArea}
deleteHandleTask={HanldeDeleteActivity}
/>
))
) : (

View File

@ -9,11 +9,22 @@ import ConfirmModal from "../../common/ConfirmModal";
import ProjectRepository from "../../../repositories/ProjectRepository";
import { useProjectDetails } from "../../../hooks/useProjects";
import showToast from "../../../services/toastService";
import { cacheData, getCachedData } from "../../../slices/apiDataManager";
import {
cacheData,
clearCacheKey,
getCachedData,
} from "../../../slices/apiDataManager";
import { useDispatch } from "react-redux";
import { refreshData } from "../../../slices/localVariablesSlice";
const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => {
const WorkItem = ( {
key,
workItem,
forBuilding,
forFloor,
forWorkArea,
deleteHandleTask,
}) => {
const { projectId } = useParams();
const [itemName, setItemName] = useState("");
const [NewWorkItem, setNewWorkItem] = useState();
@ -26,10 +37,6 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => {
const project = getCachedData("projectInfo");
const dispatch = useDispatch();
const { projects_Details } = useProjectDetails(
projectId || project?.projectId
);
const openModal = () => setIsModalOpen(true);
const closeModal = () => setIsModalOpen(false);
const getProgress = (planned, completed) => {
@ -66,62 +73,17 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => {
const handleSubmit = async () => {
setLoadingDelete(true);
try
{
const updatedProject = { ...projects_Details };
const response = await ProjectRepository.deleteProjectTask( workItem.workItemId || workItem.id);
const newProject = {
...updatedProject,
buildings: updatedProject.buildings.map((building) =>
building.id === forBuilding?.id
? {
...building,
floors: building.floors.map((floor) =>
floor.id === forFloor?.id
? {
...floor,
workAreas: floor.workAreas.map((workArea) =>
workArea.id === forWorkArea?.id
? {
...workArea,
workItems: workArea.workItems.filter(
(item) =>
String(item?.workItem?.id ?? item?.id) !==
String(workItem.workItemId)
),
}
: workArea
),
}
: floor
),
}
: building
),
};
cacheData("projectInfo", {
projectId: newProject.id,
data: newProject,
});
dispatch(refreshData(true));
closeModalDelete();
setLoadingDelete(false);
showToast("Activity Deleted Successfully", "success");
} catch (error) {
setLoadingDelete(false);
closeModalDelete();
const message =
error.response?.data?.message ||
error.message ||
"An unexpected error occurred";
showToast(message, "error");
}
let WorkItemId = workItem.workItemId || workItem.id;
deleteHandleTask( WorkItemId );
setLoadingDelete(false);
closeModalDelete();
};
const PlannedWork = NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork;
const CompletedWork = NewWorkItem?.workItem?.completedWork ?? workItem?.completedWork;
const PlannedWork =
NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork;
const CompletedWork =
NewWorkItem?.workItem?.completedWork ?? workItem?.completedWork;
return (
<>
{isModalOpen && (
@ -176,7 +138,7 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => {
</div>
)}
<tr>
<tr key={key}>
<td className="text-start table-cell-small">
<i className="bx bx-right-arrow-alt"></i>
<span className="fw-light">
@ -236,65 +198,66 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => {
></div>
</div>
</td>
<td className="text-end align-middle">
<div className="dropdown w-auto d-inline-flex align-items-center gap-1">
{/* Reserve space for Assign button */}
<div style={{ width: 'auto', minWidth: '60px' }}>
{(!projectId && ManageTasks) && (PlannedWork !== CompletedWork) ? (
<button
aria-label="Modify"
type="button"
className="btn p-0"
data-bs-toggle="modal"
data-bs-target="#project-modal"
onClick={openModal}
>
<span className="badge badge-md bg-label-primary me-1">Assign</span>
</button>
) : (
// Hidden placeholder to preserve layout
<span className="invisible">
<span className="badge badge-md bg-label-primary me-1">Assign</span>
</span>
)}
</div>
{/* Edit and Delete buttons */}
{ManageInfra && (
<>
<button
aria-label="Modify"
type="button"
className="btn p-0"
onClick={showModal1}
>
<i
className="bx bxs-edit me-2 text-primary"
data-bs-toggle="tooltip"
data-bs-placement="top"
title="Edit Activity"
></i>
</button>
<button
aria-label="Delete"
type="button"
className="btn p-0"
onClick={showModalDelete}
>
<i
className="bx bx-trash me-1 text-danger"
data-bs-toggle="tooltip"
data-bs-placement="top"
title="Delete Activity"
></i>
</button>
</>
)}
</div>
</td>
<td className="text-end align-middle">
<div className="dropdown w-auto d-inline-flex align-items-center gap-1">
{/* Reserve space for Assign button */}
<div style={{ width: "auto", minWidth: "60px" }}>
{!projectId && ManageTasks && PlannedWork !== CompletedWork ? (
<button
aria-label="Modify"
type="button"
className="btn p-0"
data-bs-toggle="modal"
data-bs-target="#project-modal"
onClick={openModal}
>
<span className="badge badge-md bg-label-primary me-1">
Assign
</span>
</button>
) : (
// Hidden placeholder to preserve layout
<span className="invisible">
<span className="badge badge-md bg-label-primary me-1">
Assign
</span>
</span>
)}
</div>
{/* Edit and Delete buttons */}
{ManageInfra && (
<>
<button
aria-label="Modify"
type="button"
className="btn p-0"
onClick={showModal1}
>
<i
className="bx bxs-edit me-2 text-primary"
data-bs-toggle="tooltip"
data-bs-placement="top"
title="Edit Activity"
></i>
</button>
<button
aria-label="Delete"
type="button"
className="btn p-0"
onClick={showModalDelete}
>
<i
className="bx bx-trash me-1 text-danger"
data-bs-toggle="tooltip"
data-bs-placement="top"
title="Delete Activity"
></i>
</button>
</>
)}
</div>
</td>
</tr>
</>
);