diff --git a/src/components/Activities/InfraPlanning.jsx b/src/components/Activities/InfraPlanning.jsx index 49f00567..0445f16b 100644 --- a/src/components/Activities/InfraPlanning.jsx +++ b/src/components/Activities/InfraPlanning.jsx @@ -8,7 +8,7 @@ import WorkAreaModel from "../Project/Infrastructure/WorkAreaModel"; import TaskModel from "../Project/Infrastructure/TaskModel"; import ProjectRepository from "../../repositories/ProjectRepository"; import Breadcrumb from "../../components/common/Breadcrumb"; -import {useProjectDetails, useProjects} from "../../hooks/useProjects"; +import {useProjectDetails, useProjectInfra, useProjects} from "../../hooks/useProjects"; import {useHasUserPermission} from "../../hooks/useHasUserPermission"; import {MANAGE_PROJECT_INFRA} from "../../utils/constants"; import {useDispatch, useSelector} from "react-redux"; @@ -21,11 +21,12 @@ const InfraPlanning = () => { const {profile: LoggedUser, refetch : fetchData} = useProfile() const dispatch = useDispatch() - // const {projects,loading:project_listLoader,error:projects_error} = useProjects() - const selectedProject = useSelector((store)=>store.localVariables.projectId) + const {projectInfra, isLoading, error} = useProjectInfra( selectedProject ) + + + const ManageInfra = useHasUserPermission( MANAGE_PROJECT_INFRA ) - const {projects_Details, loading: project_deatilsLoader, error: project_error,refetch} = useProjectDetails( selectedProject ) const reloadedData = useSelector( ( store ) => store.localVariables.reload ) @@ -45,12 +46,9 @@ const InfraPlanning = () =>
- {project_deatilsLoader && (

Loading...

)} - {( !project_deatilsLoader && projects_Details?.buildings.length === 0 ) && (

No Result Found

)} - - - - {(!project_deatilsLoader && projects_Details?.buildings?.length > 0) && ()} + {isLoading && (

Loading...

)} + {( !isLoading && projectInfra.length === 0 ) && (

No Result Found

)} + {(!isLoading && projectInfra?.length > 0) && ()}
diff --git a/src/components/Project/AboutProject.jsx b/src/components/Project/AboutProject.jsx index 5802e4d9..e66b8c03 100644 --- a/src/components/Project/AboutProject.jsx +++ b/src/components/Project/AboutProject.jsx @@ -1,16 +1,14 @@ import React, { useEffect, useState } from "react"; import moment from "moment"; import { getProjectStatusName } from "../../utils/projectStatus"; -const AboutProject = ({ data }) => { - const [CurrentProject, setCurrentProject] = useState(data); - - useEffect(() => { - setCurrentProject(data); - }, [data]); - +import {useProjectDetails} from "../../hooks/useProjects"; +import {useParams} from "react-router-dom"; +const AboutProject = () => { + const {projectId} = useParams(); + const {projects_Details,isLoading,error} = useProjectDetails(projectId) return ( <> - {data && ( + {projects_Details && (
@@ -21,8 +19,8 @@ const AboutProject = ({ data }) => { Start Date:{" "} - {data.startDate - ? moment(data.startDate).format("DD-MMM-YYYY") + {projects_Details.startDate + ? moment(projects_Details.startDate).format("DD-MMM-YYYY") : "N/A"} @@ -30,31 +28,32 @@ const AboutProject = ({ data }) => { {" "} End Date:{" "} - {data.endDate - ? moment(data.endDate).format("DD-MMM-YYYY") + {projects_Details.endDate + ? moment(projects_Details.endDate).format("DD-MMM-YYYY") : "N/A"}
  • Status:{" "} - {getProjectStatusName(data.projectStatusId)} + {projects_Details?.projectStatus?.status +}
  • Contact:{" "} - {data.contactPerson} + {projects_Details.contactPerson}
  • Address: - {data.projectAddress?.length <= 20 && ( - {data.projectAddress} + {projects_Details.projectAddress?.length <= 20 && ( + {projects_Details.projectAddress} )}
    - {data.projectAddress?.length > 20 && ( -
    {data.projectAddress}
    + {projects_Details.projectAddress?.length > 20 && ( +
    {projects_Details.projectAddress}
    )}
  • @@ -62,7 +61,7 @@ const AboutProject = ({ data }) => {
    )} - {!data && loading...} + {isLoading && loading...} ); }; diff --git a/src/components/Project/Infrastructure/Building.jsx b/src/components/Project/Infrastructure/Building.jsx index 62b8f533..4a7e1792 100644 --- a/src/components/Project/Infrastructure/Building.jsx +++ b/src/components/Project/Infrastructure/Building.jsx @@ -4,7 +4,8 @@ const Building = ({ toggleBuilding, expandedBuildings, getContent, -}) => { +} ) => +{ return ( @@ -20,7 +21,7 @@ const Building = ({ >
    - {building.name}   + {building.buildingName}   {expandedBuildings.includes(building.id) ? ( ) : ( diff --git a/src/components/Project/Infrastructure/BuildingModel.jsx b/src/components/Project/Infrastructure/BuildingModel.jsx index e701155d..34c4b24d 100644 --- a/src/components/Project/Infrastructure/BuildingModel.jsx +++ b/src/components/Project/Infrastructure/BuildingModel.jsx @@ -1,14 +1,13 @@ -import React, { useState, useEffect } from "react"; +import React, { useEffect, useMemo } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; -import ProjectRepository from "../../../repositories/ProjectRepository"; import { useSelector } from "react-redux"; -import { useProjectDetails } from "../../../hooks/useProjects"; import { getCachedData } from "../../../slices/apiDataManager"; import showToast from "../../../services/toastService"; +import { useManageProjectInfra } from "../../../hooks/useProjects"; +import useSelect from "../../common/useSelect"; -// Zod validation schema const buildingSchema = z.object({ Id: z.string().optional(), name: z.string().min(1, "Building name is required"), @@ -18,194 +17,165 @@ const buildingSchema = z.object({ .max(160, "Description cannot exceed 160 characters"), }); -const BuildingModel = ({ - project, - onClose, - onSubmit, - clearTrigger, - onClearComplete, - editingBuilding = null, -}) => { +const BuildingModel = ({ project, onClose, editingBuilding = null }) => { const selectedProject = useSelector( (store) => store.localVariables.projectId ); - const [buildings, setBuildings] = useState([]); - const projects_Details = getCachedData("projectInfo"); - const [formData, setFormData] = useState({ - id: "", - name: "", - description: "", - projectId: project?.id, - }); - - useEffect(() => { - if (clearTrigger) { - setFormData({ id: null, name: "", description: "", projectId: project.id }); - onClearComplete(); - } else if (editingBuilding) { - setFormData({ ...editingBuilding, projectId: project.id }); - } - - return () => { - setValue("name", ""); - }; - }, [clearTrigger, onClearComplete, editingBuilding, project?.id]); - const { register, handleSubmit, formState: { errors }, setValue, + watch, reset, - getValues, } = useForm({ resolver: zodResolver(buildingSchema), - defaultValues: formData, // Set default values from formData state - }); - - const handleBuildingChange = (e) => { - const selectedBuilding = project.buildings.find( - (b) => b.id === +e.target.value - ); - if (selectedBuilding) { - setFormData({ ...selectedBuilding, projectId: project.id }); - setValue("name", selectedBuilding.name); // Update name field - setValue("description", selectedBuilding.description); // Update description field - } else { - setFormData({ id: null, name: "", description: "", projectId: project.id }); - setValue("name", ""); - setValue("description", ""); - } - }; - - const onSubmitHandler = async (data) => { - if (String(data.Id) === "0") { - data.Id = null; - } - onSubmit({ ...data, projectId: project.id }); - reset({ + defaultValues: { Id: "0", name: "", description: "", - }); - if (data.Id !== null) { - showToast("Building updated successfully.", "success"); - } else { - showToast("Building created successfully.", "success"); - } - }; + }, + }); + const watchedId = watch("Id"); + + const { mutate: ManageBuilding, isPending } = useManageProjectInfra({ + onSuccessCallback: (data, variables) => { + showToast( + watchedId != "0" + ? "Building updated Successfully" + : "Building created Successfully", + "success" + ); + reset({ Id: "0", name: "", description: "" }); + onClose?.(); + }, + }); + + const sortedBuildings = useMemo(() => { + return (project || []) + .filter((b) => b?.buildingName) + .sort((a, b) => a.buildingName.localeCompare(b?.buildingName)); + }, [project]); useEffect(() => { - if(projects_Details){ - setBuildings(projects_Details.data?.buildings); + if (!watchedId || watchedId === "0") { + setValue("name", ""); + setValue("description", ""); + } else { + const selected = sortedBuildings.find((b) => String(b.id) === watchedId); + if (selected) { + setValue("name", selected.buildingName || ""); + setValue("description", selected.description || ""); + } } - }, [projects_Details]); + }, [watchedId, sortedBuildings, setValue]); + + useEffect(() => { + if (editingBuilding) { + reset({ + Id: String(editingBuilding.id), + name: editingBuilding.name, + description: editingBuilding.description, + }); + } + }, [editingBuilding]); + + const onSubmitHandler = (data) => { + const payload = { + ...data, + Id: data.Id === "0" ? null : data.Id, + projectId: selectedProject, + }; + + let infraObject = [ + { + building: payload, + floor: null, + workArea: null, + }, + ]; + ManageBuilding({ infraObject, projectId: selectedProject }); + }; + return ( -
    -
    -
    - -
    - Manage Buildings - {project?.name} -
    -
    -
    - - - {errors.Id && ( - {errors.Id.message} - )} -
    - -
    - - - {errors.name && ( - {errors.name.message} - )} -
    - -
    - -