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:
parent
8608043fb2
commit
a7421eb6dc
@ -2,6 +2,9 @@ import React, { useState, useEffect } from "react";
|
|||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { z } from "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
|
// Define Zod validation schema
|
||||||
const taskSchema = z.object({
|
const taskSchema = z.object({
|
||||||
@ -23,11 +26,12 @@ const defaultModel = {
|
|||||||
|
|
||||||
const TaskModel = ({
|
const TaskModel = ({
|
||||||
project,
|
project,
|
||||||
activities,
|
|
||||||
onSubmit,
|
onSubmit,
|
||||||
clearTrigger,
|
clearTrigger,
|
||||||
onClearComplete,onClose
|
onClearComplete,onClose
|
||||||
}) => {
|
} )=>{
|
||||||
|
const dispatch = useDispatch()
|
||||||
|
const {data:activities,loading} = useMaster()
|
||||||
const [formData, setFormData] = useState(defaultModel);
|
const [formData, setFormData] = useState(defaultModel);
|
||||||
const [selectedBuilding, setSelectedBuilding] = useState(null);
|
const [selectedBuilding, setSelectedBuilding] = useState(null);
|
||||||
const [selectedFloor, setSelectedFloor] = useState(null);
|
const [selectedFloor, setSelectedFloor] = useState(null);
|
||||||
@ -90,8 +94,6 @@ const TaskModel = ({
|
|||||||
|
|
||||||
const onSubmitForm = ( data ) =>
|
const onSubmitForm = ( data ) =>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
onSubmit( data );
|
onSubmit( data );
|
||||||
setSelectedActivity(null),
|
setSelectedActivity(null),
|
||||||
setSelectedWorkArea(null)
|
setSelectedWorkArea(null)
|
||||||
@ -104,6 +106,8 @@ const TaskModel = ({
|
|||||||
|
|
||||||
useEffect( () =>
|
useEffect( () =>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
dispatch(changeMaster("Activity")),
|
||||||
() =>{
|
() =>{
|
||||||
resetVlaue ()
|
resetVlaue ()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,6 @@ import { useProjectDetails } from "../../hooks/useProjects";
|
|||||||
|
|
||||||
const ProjectInfra = ({
|
const ProjectInfra = ({
|
||||||
data,
|
data,
|
||||||
activityMaster,
|
|
||||||
onDataChange,
|
onDataChange,
|
||||||
eachSiteEngineer,
|
eachSiteEngineer,
|
||||||
}) => {
|
}) => {
|
||||||
@ -57,130 +56,6 @@ const ProjectInfra = ({
|
|||||||
setIsBuildingModalOpen(true);
|
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 = () => {
|
const closeBuildingModel = () => {
|
||||||
setIsBuildingModalOpen(false);
|
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) => {
|
const toggleBuilding = (id) => {
|
||||||
setExpandedBuildings((prev) =>
|
setExpandedBuildings((prev) =>
|
||||||
prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id]
|
prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id]
|
||||||
@ -400,7 +408,7 @@ const ProjectInfra = ({
|
|||||||
>
|
>
|
||||||
<TaskModel
|
<TaskModel
|
||||||
project={project}
|
project={project}
|
||||||
activities={activityMaster}
|
|
||||||
onClose={closeTaskModel}
|
onClose={closeTaskModel}
|
||||||
onSubmit={handleTaskModelFormSubmit}
|
onSubmit={handleTaskModelFormSubmit}
|
||||||
clearTrigger={clearFormTrigger}
|
clearTrigger={clearFormTrigger}
|
||||||
@ -463,6 +471,7 @@ const ProjectInfra = ({
|
|||||||
<InfraTable
|
<InfraTable
|
||||||
buildings={project?.buildings}
|
buildings={project?.buildings}
|
||||||
project={project}
|
project={project}
|
||||||
|
handleFloor={submitData}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -27,29 +27,10 @@ const ProjectDetails = () => {
|
|||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
const [project, setProject] = useState(null);
|
const [project, setProject] = useState(null);
|
||||||
const [ projectDetails, setProjectDetails ] = useState( null );
|
const [ projectDetails, setProjectDetails ] = useState( null );
|
||||||
const [activities, setActivities] = useState(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [ error, setError ] = useState( "" );
|
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 () => {
|
const fetchData = async () => {
|
||||||
|
|
||||||
@ -124,7 +105,6 @@ const ProjectDetails = () => {
|
|||||||
return (
|
return (
|
||||||
<ProjectInfra
|
<ProjectInfra
|
||||||
data={projectDetails}
|
data={projectDetails}
|
||||||
activityMaster={activities}
|
|
||||||
onDataChange={handleDataChange}
|
onDataChange={handleDataChange}
|
||||||
></ProjectInfra>
|
></ProjectInfra>
|
||||||
);
|
);
|
||||||
@ -134,7 +114,6 @@ const ProjectDetails = () => {
|
|||||||
return (
|
return (
|
||||||
<WorkPlan
|
<WorkPlan
|
||||||
data={projectDetails}
|
data={projectDetails}
|
||||||
activityMaster={activities}
|
|
||||||
onDataChange={handleDataChange}
|
onDataChange={handleDataChange}
|
||||||
></WorkPlan>
|
></WorkPlan>
|
||||||
);
|
);
|
||||||
@ -156,15 +135,12 @@ const ProjectDetails = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// fetchData();
|
|
||||||
dispatch(setProjectId(projectId))
|
dispatch(setProjectId(projectId))
|
||||||
setProject( projects_Details )
|
setProject( projects_Details )
|
||||||
setProjectDetails(projects_Details)
|
setProjectDetails(projects_Details)
|
||||||
fetchActivities();
|
|
||||||
}, [projects_Details,projectId]);
|
}, [projects_Details,projectId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<>
|
<>
|
||||||
{}
|
{}
|
||||||
<div className="container-xxl flex-grow-1 container-p-y">
|
<div className="container-xxl flex-grow-1 container-p-y">
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user