From 80083aa5dbc8c6f28fc2017689ef2f1075ffa5ef Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Wed, 2 Jul 2025 19:42:43 +0530 Subject: [PATCH] updated some queries and mutation for prevent to duplicates calls --- src/components/Activities/InfraPlanning.jsx | 2 +- src/components/Employee/ManageEmployee.jsx | 44 +-- src/components/Layout/Header.jsx | 7 +- src/components/Project/AssignTask.jsx | 87 ++---- .../Infrastructure/EditActivityModal.jsx | 33 ++- .../Project/Infrastructure/WorkArea.jsx | 45 +-- .../Project/Infrastructure/WorkItem.jsx | 29 +- src/components/Project/ManageProjectInfo.jsx | 6 +- src/components/Project/ProjectCard.jsx | 39 --- src/hooks/masterHook/useMaster.js | 6 +- src/hooks/useEmployees.js | 256 +----------------- src/hooks/useProjects.js | 29 +- src/hooks/useTasks.js | 10 +- src/pages/project/ProjectList.jsx | 21 +- 14 files changed, 108 insertions(+), 506 deletions(-) diff --git a/src/components/Activities/InfraPlanning.jsx b/src/components/Activities/InfraPlanning.jsx index 0445f16b..384bce74 100644 --- a/src/components/Activities/InfraPlanning.jsx +++ b/src/components/Activities/InfraPlanning.jsx @@ -47,7 +47,7 @@ const InfraPlanning = () =>
{isLoading && (

Loading...

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

No Result Found

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

No Result Found

)} {(!isLoading && projectInfra?.length > 0) && ()}
diff --git a/src/components/Employee/ManageEmployee.jsx b/src/components/Employee/ManageEmployee.jsx index ce660061..604f5654 100644 --- a/src/components/Employee/ManageEmployee.jsx +++ b/src/components/Employee/ManageEmployee.jsx @@ -22,7 +22,7 @@ const mobileNumberRegex = /^[0-9]\d{9}$/; const ManageEmployee = ({ employeeId, onClosed,IsAllEmployee }) => { const dispatch = useDispatch(); -const { mutate: updateEmployee, isLoading } = useUpdateEmployee(); +const { mutate: updateEmployee, isPending } = useUpdateEmployee(); const { employee, @@ -163,40 +163,6 @@ const { mutate: updateEmployee, isLoading } = useUpdateEmployee(); }); const AadharNumberValue = watch("aadharNumber") || ""; - - // const onSubmit = (data) => { - // setLoading(true); - // if (data.email == "") { - // data.email = null; - // } - // EmployeeRepository.manageEmployee(data) - // .then((response) => { - // cacheData("employeeProfileInfo", data); - // showToast( - // `Employee details ${ - // data.id == null ? "created" : "updated" - // } successfully.`, - // "success" - // ); - // clearCacheKey("employeeListByProject"); - // clearCacheKey("allEmployeeList"); - // clearCacheKey("allInactiveEmployeeList"); - // clearCacheKey("employeeProfile"); - - // setLoading(false); - // reset(); - // // navigation("/employees"); - // onClosed(); - // }) - // .catch((error) => { - // const message = - // error?.response?.data?.message || - // error?.message || - // "Error occured during api calling"; - // showToast(message, "error"); - // setLoading(false); - // }); - // }; const onSubmit = (data) => { if (data.email === "") { @@ -240,7 +206,7 @@ const { mutate: updateEmployee, isLoading } = useUpdateEmployee(); phoneNumber: currentEmployee.phoneNumber || "", jobRoleId: currentEmployee.jobRoleId?.toString() || "", } - : {} // Empty object resets the form + : {} ); setCurrentAddressLength(currentEmployee?.currentAddress?.length || 0); setPermanentAddressLength(currentEmployee?.permanentAddress?.length || 0); @@ -625,9 +591,9 @@ const { mutate: updateEmployee, isLoading } = useUpdateEmployee(); aria-label="manage employee" type="submit" className="btn btn-sm btn-primary" - disabled={isloading} + disabled={isPending} > - {isloading + {isPending ? "Please Wait..." : employeeId ? "Update" @@ -638,7 +604,7 @@ const { mutate: updateEmployee, isLoading } = useUpdateEmployee(); aria-label="manage employee" type="reset" className="btn btn-sm btn-primary ms-2" - disabled={isloading} + disabled={isPending} > Clear diff --git a/src/components/Layout/Header.jsx b/src/components/Layout/Header.jsx index 87105854..16b5c6cf 100644 --- a/src/components/Layout/Header.jsx +++ b/src/components/Layout/Header.jsx @@ -22,7 +22,7 @@ import { MANAGE_PROJECT } from "../../utils/constants"; const Header = () => { const {profile} = useProfile(); const location = useLocation(); - const dispatch = useDispatch(changeMaster("Job Role")); + const dispatch = useDispatch(); const { data, loading } = useMaster(); const navigate = useNavigate(); const HasManageProjectPermission = useHasUserPermission(MANAGE_PROJECT); @@ -145,7 +145,10 @@ const Header = () => { // return () => eventBus.off("project", newProjectHandler); // }, [handler]); - + useDispatch( () => + { + dispatch(changeMaster("Job Role")) +},[]) useEffect(() => { eventBus.on("assign_project_one", handler); eventBus.on("project", newProjectHandler); diff --git a/src/components/Project/AssignTask.jsx b/src/components/Project/AssignTask.jsx index ea531d7a..48c7b182 100644 --- a/src/components/Project/AssignTask.jsx +++ b/src/components/Project/AssignTask.jsx @@ -15,10 +15,8 @@ import { useCreateTask } from "../../hooks/useTasks"; const AssignTask = ({ assignData, onClose, setAssigned }) => { const maxPlanned = - assignData?.workItem?.workItem?.plannedWork - - assignData?.workItem?.workItem?.completedWork; - - // Zod schema for form validation + assignData?.workItem?.plannedWork - + assignData?.workItem?.completedWork; const schema = z.object({ selectedEmployees: z .array(z.string()) @@ -51,9 +49,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { const infoRef = useRef(null); const infoRef1 = useRef(null); - // Initialize Bootstrap Popovers on component mount useEffect(() => { - // Check if Bootstrap is available globally if (typeof bootstrap !== "undefined") { if (infoRef.current) { new bootstrap.Popover(infoRef.current, { @@ -75,8 +71,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { } else { console.warn("Bootstrap is not available. Popovers might not function."); } - }, []); // Empty dependency array ensures this runs once on mount - // Redux state and hooks + }, []); const selectedProject = useSelector( (store) => store.localVariables.projectId ); @@ -86,14 +81,11 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { recallEmployeeData, } = useEmployeesAllOrByProjectId(selectedProject, false); const dispatch = useDispatch(); - const { loading } = useMaster(); // Assuming this is for jobRoleData loading - const jobRoleData = getCachedData("Job Role"); + const { loading } = useMaster(); + const {data:jobRoleData} = useMaster(); - // Local component states const [selectedRole, setSelectedRole] = useState("all"); - const [displayedSelection, setDisplayedSelection] = useState(""); // This state is not updated in the provided code, consider if it's still needed or how it should be updated - - // React Hook Form setup + const [displayedSelection, setDisplayedSelection] = useState(""); const { handleSubmit, control, @@ -101,50 +93,43 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { watch, formState: { errors }, reset, - trigger, // <--- IMPORTANT: Destructure 'trigger' here + trigger, } = useForm({ defaultValues: { selectedEmployees: [], description: "", plannedTask: "", }, - resolver: zodResolver(schema), // Integrate Zod schema with react-hook-form + resolver: zodResolver(schema), }); - // Handler for employee checkbox changes const handleCheckboxChange = (event, user) => { const isChecked = event.target.checked; - let updatedSelectedEmployees = watch("selectedEmployees") || []; // Get current selected employees from form state + let updatedSelectedEmployees = watch("selectedEmployees") || []; if (isChecked) { - // Add employee if checked and not already in the list if (!updatedSelectedEmployees.includes(user.id)) { updatedSelectedEmployees = [...updatedSelectedEmployees, user.id]; } } else { - // Remove employee if unchecked - updatedSelectedEmployees = updatedSelectedEmployees.filter( + updatedSelectedEmployees = updatedSelectedEmployees?.filter( (id) => id !== user.id ); } - // Update the form state with the new list of selected employees setValue("selectedEmployees", updatedSelectedEmployees); - trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation here + trigger("selectedEmployees"); }; - // Effect to dispatch action for Job Role master data useEffect(() => { dispatch(changeMaster("Job Role")); - // Cleanup function to reset selected role when component unmounts or dispatch changes + return () => setSelectedRole("all"); }, [dispatch]); - // Handler for role filter change const handleRoleChange = (event) => { setSelectedRole(event.target.value); }; - // Filter employees based on selected role const filteredEmployees = selectedRole === "all" ? employees @@ -152,36 +137,6 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { (emp) => String(emp.jobRoleId || "") === selectedRole ); - // Form submission handler - // const onSubmit = async (data) => { - // const selectedEmployeeIds = data.selectedEmployees; - // setIsSubmitting(true); - // // Prepare taskTeam data (only IDs are needed for the backend based on previous context) - // const taskTeamWithDetails = selectedEmployeeIds - // .map((empId) => { - // return empId; // Return just the ID as per previous discussions - // }) - // .filter(Boolean); // Ensure no nulls if employee not found (though unlikely with current logic) - - // // Format data for API call - // const formattedData = { - // taskTeam: taskTeamWithDetails, - // plannedTask: data.plannedTask, - // description: data.description, - // assignmentDate: new Date().toISOString(), // Current date/time - // workItemId: assignData?.workItem?.workItem.id, - // }; - - // try { - // await TasksRepository.assignTask(formattedData); - // setIsSubmitting( false ); - // showToast("Task Assined Successfully.", "success"); - // closedModel(); - // } catch (error) { - // setIsSubmitting(false); - // showToast("Something went wrong. Please try again.", "error"); - // } - // }; const onSubmit = (data) => { const selectedEmployeeIds = data.selectedEmployees; @@ -192,13 +147,12 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { plannedTask: data.plannedTask, description: data.description, assignmentDate: new Date().toISOString(), - workItemId: assignData?.workItem?.workItem.id, + workItemId: assignData?.workItem.id, }; assignTask(formattedData); }; - // Handler to close the modal and reset form const closedModel = () => { reset(); onClose(); @@ -212,10 +166,10 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { Work Location : {[ - assignData?.building?.name, + assignData?.building?.buildingName, assignData?.floor?.floorName, assignData?.workArea?.areaName, - assignData?.workItem?.workItem?.activityMaster?.activityName, + assignData?.workItem?.activityMaster?.activityName, ] .filter(Boolean) // Filter out any undefined/null values .map((item, index, array) => ( @@ -378,7 +332,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { "selectedEmployees", updatedSelected ); - trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation on removing badge + trigger("selectedEmployees"); }} > @@ -395,11 +349,10 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { {!loading && errors.selectedEmployees && (

{errors.selectedEmployees.message}

{" "} - {/* Use message from Zod schema */} +
)} - {/* Pending Task of Activity section */}