From 0cb661687a431a3587ec6f624b732b8d837ee6f5 Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Fri, 25 Jul 2025 11:52:30 +0530 Subject: [PATCH] Search input works for both name and job role filtering. Dropdown correctly shows checkboxes and supports multi-selection. Updated icon appears, and filtering updates the employee list as expected. --- src/components/Project/AssignTask.jsx | 389 ++++++++++++++++++-------- 1 file changed, 266 insertions(+), 123 deletions(-) diff --git a/src/components/Project/AssignTask.jsx b/src/components/Project/AssignTask.jsx index 0d922c29..91ec4f9c 100644 --- a/src/components/Project/AssignTask.jsx +++ b/src/components/Project/AssignTask.jsx @@ -48,6 +48,9 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { const infoRef = useRef(null); const infoRef1 = useRef(null); + // State for search term + const [searchTerm, setSearchTerm] = useState(""); + useEffect(() => { if (typeof bootstrap !== "undefined") { if (infoRef.current) { @@ -83,7 +86,8 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { const { loading } = useMaster(); const { data: jobRoleData } = useMaster(); - const [selectedRole, setSelectedRole] = useState("all"); + // Changed to an array to hold multiple selected roles + const [selectedRoles, setSelectedRoles] = useState(["all"]); const [displayedSelection, setDisplayedSelection] = useState(""); const { handleSubmit, @@ -121,20 +125,79 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { useEffect(() => { dispatch(changeMaster("Job Role")); - - return () => setSelectedRole("all"); + // Initial state should reflect "All Roles" selected + setSelectedRoles(["all"]); }, [dispatch]); - const handleRoleChange = (event) => { - setSelectedRole(event.target.value); + // Modified handleRoleChange to handle multiple selections + const handleRoleChange = (event, roleId) => { + // If 'all' is selected, clear other selections + if (roleId === "all") { + setSelectedRoles(["all"]); + } else { + setSelectedRoles((prevSelectedRoles) => { + // If "all" was previously selected, remove it + const newRoles = prevSelectedRoles.filter((role) => role !== "all"); + if (newRoles.includes(roleId)) { + // If role is already selected, unselect it + return newRoles.filter((id) => id !== roleId); + } else { + // If role is not selected, add it + return [...newRoles, roleId]; + } + }); + } }; - const filteredEmployees = - selectedRole === "all" - ? employees - : employees?.filter( - (emp) => String(emp.jobRoleId || "") === selectedRole - ); + useEffect(() => { + // Update displayedSelection based on selectedRoles + if (selectedRoles.includes("all")) { + setDisplayedSelection("All Roles"); + } else if (selectedRoles.length > 0) { + const selectedRoleNames = selectedRoles.map(roleId => { + const role = jobRoleData?.find(r => String(r.id) === roleId); + return role ? role.name : ''; + }).filter(Boolean); // Filter out empty strings for roles not found + setDisplayedSelection(selectedRoleNames.join(', ')); + } else { + setDisplayedSelection("Select Roles"); + } + }, [selectedRoles, jobRoleData]); + + + const handleSearchChange = (event) => { + setSearchTerm(event.target.value); + }; + + // Filter employees first by role, then by search term AND job role name + const filteredEmployees = employees?.filter((emp) => { + const matchesRole = + selectedRoles.includes("all") || selectedRoles.includes(String(emp.jobRoleId)); + // Convert both first and last names and job role name to lowercase for case-insensitive matching + const fullName = `${emp.firstName} ${emp.lastName}`.toLowerCase(); + + const jobRoleName = jobRoleData?.find((role) => role.id === emp.jobRoleId)?.name?.toLowerCase() || ""; + + const searchLower = searchTerm.toLowerCase(); + // Check if the full name OR job role name includes the search term + const matchesSearch = fullName.includes(searchLower) || jobRoleName.includes(searchLower); + return matchesRole && matchesSearch; + }); + + // Determine unique job role IDs from the filtered employees (for dropdown options) + const uniqueJobRoleIdsInFilteredEmployees = new Set( + employees?.map(emp => emp.jobRoleId).filter(Boolean) + ); + + // Filter jobRoleData to only include roles present in the uniqueJobRoleIdsInFilteredEmployees + const jobRolesForDropdown = jobRoleData?.filter(role => + uniqueJobRoleIdsInFilteredEmployees.has(role.id) + ); + + // Calculate the count of selected roles for display + const selectedRolesCount = selectedRoles.includes("all") + ? 0 // "All Roles" doesn't contribute to a specific count + : selectedRoles.length; const onSubmit = (data) => { const selectedEmployeeIds = data.selectedEmployees; @@ -159,6 +222,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { reset(); onClose(); }; + return (

Assign Task

@@ -192,125 +256,207 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
Select Team -
{displayedSelection}
- +
+ - {/* --- Scrollbar applied here --- */} + {/* Badge */} + {selectedRolesCount > 0 && ( + + {selectedRolesCount} + + )} + + {/* Dropdown Menu */} +
    +
  • +
    + handleRoleChange(e, e.target.value)} + /> + +
    +
  • + + {jobRolesForDropdown?.map((role) => ( +
  • +
    + handleRoleChange(e, e.target.value)} + /> + +
    +
  • + ))} +
+
  • - -
  • - {jobRoleData?.map((user) => ( -
  • - + All Roles + +
+ + {jobRolesForDropdown?.map((role) => ( +
  • +
    + + handleRoleChange(e, e.target.value) + } + /> + +
  • ))} +
    +
    + {selectedRoles?.length > 0 && ( +
    + {employeeLoading ? ( +
    +

    Loading employees...

    +
    + ) : filteredEmployees?.length > 0 ? ( + filteredEmployees.map((emp) => { + const jobRole = jobRoleData?.find( + (role) => role?.id === emp?.jobRoleId + ); -
    -
    - {selectedRole !== "" && ( -
    - {employeeLoading ? ( -
    -

    Loading employees...

    -
    - ) : filteredEmployees?.length > 0 ? ( - filteredEmployees.map((emp) => { - const jobRole = jobRoleData?.find( - (role) => role?.id === emp?.jobRoleId - ); - - return ( -
    -
    - ( - { - handleCheckboxChange(e, emp); - }} - /> + return ( +
    +
    + ( + { + handleCheckboxChange(e, emp); + }} + /> + )} + /> +
    +

    + {emp.firstName} {emp.lastName} +

    + + {loading ? ( + + + + ) : ( + jobRole?.name || "Unknown Role" )} - /> -
    -

    - {emp.firstName} {emp.lastName} -

    - - {loading ? ( - - - - ) : ( - jobRole?.name || "Unknown Role" - )} - -
    +
    - ); - }) - ) : ( -
    -

    - No employees found for the selected role. -

    -
    - )} -
    - )} -
    +
    + ); + }) + ) : ( +
    +

    + No employees found for the selected role(s). +

    +
    + )} +
    + )}
    { className="bi bi-info-circle" viewBox="0 0 16 16" > - +
    @@ -439,11 +585,11 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { id="defaultFormControlInput" aria-describedby="defaultFormControlHelp" /> - - { - assignData?.workItem?.workItem?.activityMaster + + { + assignData?.workItem?.activityMaster ?.unitOfMeasurement - } + }
    { className="position-absolute bg-white border p-2 rounded shadow" style={{ zIndex: 10, marginLeft: "10px" }} > - {/* Add your help content here */}

    Enter the target value for today's task.

    @@ -501,7 +646,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { @@ -512,7 +657,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {