removed fetchActivity function , activity was needed for projectInfra, that can pass their child component TaskModel. instead of getting activity from ProjectInfra, Now TaskModel can directly calling to useMaster-Activity

This commit is contained in:
Pramod Mahajan 2025-04-11 15:26:50 +05:30
parent 8608043fb2
commit a7421eb6dc
3 changed files with 143 additions and 154 deletions

View File

@ -2,6 +2,9 @@ import React, { useState, useEffect } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from "zod";
import {useDispatch} from "react-redux";
import {changeMaster} from "../../../slices/localVariablesSlice";
import useMaster from "../../../hooks/masterHook/useMaster";
// Define Zod validation schema
const taskSchema = z.object({
@ -23,11 +26,12 @@ const defaultModel = {
const TaskModel = ({
project,
activities,
onSubmit,
clearTrigger,
onClearComplete,onClose
}) => {
} )=>{
const dispatch = useDispatch()
const {data:activities,loading} = useMaster()
const [formData, setFormData] = useState(defaultModel);
const [selectedBuilding, setSelectedBuilding] = useState(null);
const [selectedFloor, setSelectedFloor] = useState(null);
@ -90,8 +94,6 @@ const TaskModel = ({
const onSubmitForm = ( data ) =>
{
onSubmit( data );
setSelectedActivity(null),
setSelectedWorkArea(null)
@ -104,6 +106,8 @@ const TaskModel = ({
useEffect( () =>
{
dispatch(changeMaster("Activity")),
() =>{
resetVlaue ()
}

View File

@ -17,7 +17,6 @@ import { useProjectDetails } from "../../hooks/useProjects";
const ProjectInfra = ({
data,
activityMaster,
onDataChange,
eachSiteEngineer,
}) => {
@ -57,130 +56,6 @@ const ProjectInfra = ({
setIsBuildingModalOpen(true);
};
const submitData = async (infraObject) => {
try {
let response = await ProjectRepository.manageProjectInfra(infraObject);
const entity = response.data;
const updatedProject = { ...project };
// Handle the building data
if (entity.building) {
const { id, name, description } = entity.building;
const updatedBuildings = updatedProject?.buildings?.map((building) =>
building.id === id ? { ...building, name, description } : building
);
// Add building if it doesn't exist
if (!updatedProject.buildings.some((building) => building.id === id)) {
updatedBuildings.push({
id: id,
name,
description,
floors: [],
});
}
updatedProject.buildings = updatedBuildings;
// Update the cache for buildings
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject((prevProject) => ({
...prevProject,
buildings: updatedBuildings,
}));
}
// Handle the floor data
else if (entity.floor) {
const { buildingId, id, floorName } = entity.floor;
const updatedBuildings = updatedProject?.buildings?.map((building) =>
building.id == buildingId
? {
...building,
floors: building.floors
.map((floor) =>
floor.id === id
? {
...floor,
floorName, // Update the floor name only
// Keep other properties as they are (including workArea)
}
: floor
)
// Add the new floor if it doesn't already exist
.concat(
!building.floors.some((floor) => floor.id === id)
? [{ id: id, floorName, workAreas: [] }] // New floor added with workArea set to null
: []
),
}
: building
);
updatedProject.buildings = updatedBuildings;
// Cache the updated project
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject(updatedProject);
}
// Handle the work area data
else if (entity.workArea) {
let buildingId = infraObject[0].workArea.buildingId;
const { floorId, areaName, id } = entity.workArea;
// Check if the workArea exists, otherwise create a new one
const updatedBuildings = updatedProject.buildings.map((building) =>
building.id == buildingId
? {
...building,
floors: building.floors.map((floor) =>
floor.id == floorId
? {
...floor,
workAreas: floor.workAreas.some(
(workArea) => workArea.id === id
)
? floor.workAreas.map((workArea) =>
workArea.id === id
? { ...workArea, areaName }
: workArea
)
: [
...floor.workAreas,
{ id, areaName, workItems: [] },
],
}
: floor
),
}
: building
);
updatedProject.buildings = updatedBuildings;
// Update the cache for work areas
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject(updatedProject);
}
// Handle the task (workItem) data
else {
console.error("Unsupported data type for submitData", entity);
}
} catch (Err) {
console.log(Err);
showToast("Somthing wrong", "error");
}
handleClose();
};
const closeBuildingModel = () => {
setIsBuildingModalOpen(false);
};
@ -292,6 +167,139 @@ const ProjectInfra = ({
});
};
const submitData = async (infraObject) => {
try
{
debugger
let response = await ProjectRepository.manageProjectInfra(infraObject);
const entity = response.data;
const updatedProject = { ...project };
// Handle the building data
if (entity.building) {
const { id, name, description } = entity.building;
const updatedBuildings = updatedProject?.buildings?.map((building) =>
building.id === id ? { ...building, name, description } : building
);
// Add building if it doesn't exist
if (!updatedProject.buildings.some((building) => building.id === id)) {
updatedBuildings.push({
id: id,
name,
description,
floors: [],
});
}
updatedProject.buildings = updatedBuildings;
// Update the cache for buildings
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject((prevProject) => ({
...prevProject,
buildings: updatedBuildings,
} ) );
closeBuildingModel()
}
// Handle the floor data
else if (entity.floor) {
const { buildingId, id, floorName } = entity.floor;
const updatedBuildings = updatedProject?.buildings?.map((building) =>
building.id == buildingId
? {
...building,
floors: building.floors
.map((floor) =>
floor.id === id
? {
...floor,
floorName, // Update the floor name only
// Keep other properties as they are (including workArea)
}
: floor
)
// Add the new floor if it doesn't already exist
.concat(
!building.floors.some((floor) => floor.id === id)
? [{ id: id, floorName, workAreas: [] }] // New floor added with workArea set to null
: []
),
}
: building
);
updatedProject.buildings = updatedBuildings;
// Cache the updated project
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject( updatedProject );
closeFloorModel()
}
// Handle the work area data
else if ( entity.workArea )
{
let buildingId = infraObject[0].workArea.buildingId;
const { floorId, areaName, id } = entity.workArea;
// Check if the workArea exists, otherwise create a new one
const updatedBuildings = updatedProject.buildings.map((building) =>
building.id == buildingId
? {
...building,
floors: building.floors.map((floor) =>
floor.id == floorId
? {
...floor,
workAreas: floor.workAreas.some(
(workArea) => workArea.id === id
)
? floor.workAreas.map((workArea) =>
workArea.id === id
? { ...workArea, areaName }
: workArea
)
: [
...floor.workAreas,
{ id, areaName, workItems: [] },
],
}
: floor
),
}
: building
);
updatedProject.buildings = updatedBuildings;
// Update the cache for work areas
cacheData("projectInfo", {
projectId: updatedProject.id,
data: updatedProject,
});
setProject( updatedProject );
closeWorkAreaModel()
}
// Handle the task (workItem) data
else {
console.error("Unsupported data type for submitData", entity);
}
} catch (Err) {
console.log(Err);
showToast("Somthing wrong", "error");
}
handleClose();
};
const toggleBuilding = (id) => {
setExpandedBuildings((prev) =>
prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id]
@ -400,7 +408,7 @@ const ProjectInfra = ({
>
<TaskModel
project={project}
activities={activityMaster}
onClose={closeTaskModel}
onSubmit={handleTaskModelFormSubmit}
clearTrigger={clearFormTrigger}
@ -463,6 +471,7 @@ const ProjectInfra = ({
<InfraTable
buildings={project?.buildings}
project={project}
handleFloor={submitData}
/>
)}
</div>

View File

@ -27,29 +27,10 @@ const ProjectDetails = () => {
const dispatch = useDispatch()
const [project, setProject] = useState(null);
const [ projectDetails, setProjectDetails ] = useState( null );
const [activities, setActivities] = useState(null);
const [loading, setLoading] = useState(true);
const [ error, setError ] = useState( "" );
const fetchActivities = async () => {
const activities_cache = getCachedData("activitiesMaster");
if (!activities_cache) {
ActivityeRepository.getActivities()
.then((response) => {
setActivities(response.data);
cacheData("activitiesMaster", response.data);
})
.catch((error) => {
setError("Failed to fetch data.");
});
} else {
setActivities(activities_cache);
}
};
const fetchData = async () => {
@ -124,7 +105,6 @@ const ProjectDetails = () => {
return (
<ProjectInfra
data={projectDetails}
activityMaster={activities}
onDataChange={handleDataChange}
></ProjectInfra>
);
@ -134,7 +114,6 @@ const ProjectDetails = () => {
return (
<WorkPlan
data={projectDetails}
activityMaster={activities}
onDataChange={handleDataChange}
></WorkPlan>
);
@ -156,15 +135,12 @@ const ProjectDetails = () => {
};
useEffect(() => {
// fetchData();
dispatch(setProjectId(projectId))
setProject( projects_Details )
setProjectDetails(projects_Details)
fetchActivities();
}, [projects_Details,projectId]);
return (
<>
{}
<div className="container-xxl flex-grow-1 container-p-y">