From a7421eb6dce5612d973542fc17df44aaf45316f2 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Fri, 11 Apr 2025 15:26:50 +0530 Subject: [PATCH] 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 --- .../Project/Infrastructure/TaskModel.jsx | 12 +- src/components/Project/ProjectInfra.jsx | 261 +++++++++--------- src/pages/project/ProjectDetails.jsx | 24 -- 3 files changed, 143 insertions(+), 154 deletions(-) diff --git a/src/components/Project/Infrastructure/TaskModel.jsx b/src/components/Project/Infrastructure/TaskModel.jsx index 79b95e4f..791b99ee 100644 --- a/src/components/Project/Infrastructure/TaskModel.jsx +++ b/src/components/Project/Infrastructure/TaskModel.jsx @@ -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 () } diff --git a/src/components/Project/ProjectInfra.jsx b/src/components/Project/ProjectInfra.jsx index 70e84b6a..9f6bc6e7 100644 --- a/src/components/Project/ProjectInfra.jsx +++ b/src/components/Project/ProjectInfra.jsx @@ -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 = ({ > )} diff --git a/src/pages/project/ProjectDetails.jsx b/src/pages/project/ProjectDetails.jsx index 3385b585..3dbd7bb0 100644 --- a/src/pages/project/ProjectDetails.jsx +++ b/src/pages/project/ProjectDetails.jsx @@ -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 ( ); @@ -134,7 +114,6 @@ const ProjectDetails = () => { return ( ); @@ -156,15 +135,12 @@ const ProjectDetails = () => { }; useEffect(() => { - // fetchData(); dispatch(setProjectId(projectId)) setProject( projects_Details ) setProjectDetails(projects_Details) - fetchActivities(); }, [projects_Details,projectId]); return ( - <> {}