diff --git a/src/components/Project/AssignTask.jsx b/src/components/Project/AssignTask.jsx index abe44a41..8c906c13 100644 --- a/src/components/Project/AssignTask.jsx +++ b/src/components/Project/AssignTask.jsx @@ -21,19 +21,18 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { const schema = z.object({ selectedEmployees: z .array(z.string()) - .min(1, { message: "At least one employee must be selected" }), + .min(1, { message: "At least one employee must be selected" }), description: z.string().min(1, { message: "Description is required" }), plannedTask: z.preprocess( - (val) => parseInt(val, 10), + (val) => parseInt(val, 10), z .number({ required_error: "Planned task is required", invalid_type_error: "Target for Today must be a number", }) - .int() - .positive({ message: "Planned task must be a positive number" }) + .int() + .positive({ message: "Planned task must be a positive number" }) .max(maxPlanned, { - message: `Planned task cannot exceed ${maxPlanned}`, }) ), @@ -42,6 +41,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { const [isHelpVisibleTarget, setIsHelpVisibleTarget] = useState(false); const helpPopupRefTarget = useRef(null); const [isHelpVisible, setIsHelpVisible] = useState(false); + const [isSubmitting, setIsSubmitting] = useState(false); // Refs for Bootstrap Popovers const infoRef = useRef(null); @@ -151,7 +151,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { // 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) => { @@ -169,11 +169,12 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { }; try { - // Call API to assign task - // Close the modal + await TasksRepository.assignTask(formattedData); + setIsSubmitting(false); + closedModel(); } catch (error) { - console.error("Error assigning task:", error); - showToast("Something went wrong. Please try again.", "error"); + setIsSubmitting(false); + showToast("Something went wrong. Please try again.", "error"); } }; @@ -183,393 +184,387 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => { onClose(); }; return ( - -
-

Assign Task

-
-
-

- - Work Location : - {[ - assignData?.building?.name, - assignData?.floor?.floorName, - assignData?.workArea?.areaName, - assignData?.workItem?.workItem?.activityMaster - ?.activityName, - ] - .filter(Boolean) // Filter out any undefined/null values - .map((item, index, array) => ( - - {item} - {index < array.length - 1 && ( - - )} - - ))} +

+

Assign Task

+
+
+

+ + Work Location : + {[ + assignData?.building?.name, + assignData?.floor?.floorName, + assignData?.workArea?.areaName, + assignData?.workItem?.workItem?.activityMaster?.activityName, + ] + .filter(Boolean) // Filter out any undefined/null values + .map((item, index, array) => ( + + {item} + {index < array.length - 1 && ( + + )} -

+ ))} + +

-
-
-
-
-
-
- Select Team -
{displayedSelection}
- + +
+
+
+
+
+ Select Team +
{displayedSelection}
+ -
    -
  • - -
  • - {jobRoleData?.map((user) => ( -
  • - -
  • - ))} -
-
-
-
-
- -
-
- {selectedRole !== "" && ( -
- {loading ? ( // Assuming 'loading' here refers to master data loading -
-

Loading roles...

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

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

- - {loading ? ( - - - - ) : ( - jobRole?.name || "Unknown Role" - )} - -
-
-
- ); +
    +
  • +
- )} -
+ } + > + All Roles + + + {jobRoleData?.map((user) => ( +
  • + +
  • + ))} +
    +
    +
    +
    -
    - {watch("selectedEmployees")?.length > 0 && ( -
    -
    - {watch("selectedEmployees")?.map((empId) => { - const emp = employees.find( - (emp) => emp.id === empId - ); - return ( - emp && ( - +
    + {selectedRole !== "" && ( +
    + {loading ? ( // Assuming 'loading' here refers to master data loading +
    +

    Loading roles...

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

    {emp.firstName} {emp.lastName} -

    { - const updatedSelected = watch( - "selectedEmployees" - ).filter((id) => id !== empId); - setValue( - "selectedEmployees", - updatedSelected - ); - trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation on removing badge - }} - > - -

    - - ) - ); - })} -
    -
    - )} -
    - - {!loading && errors.selectedEmployees && ( -
    -

    {errors.selectedEmployees.message}

    {" "} - {/* Use message from Zod schema */} -
    - )} - - {/* Pending Task of Activity section */} -
    -
    - -
    -
    - - {/* Target for Today input and validation */} -
    -
    - -
    -
    - ( -
    - - - { - assignData?.workItem?.workItem?.activityMaster - ?.unitOfMeasurement - } - -
    -
    -   - + - - - + {loading ? ( + + + + ) : ( + jobRole?.name || "Unknown Role" + )} +
    - )} - /> -
    - - {errors.plannedTask && ( -
    - {errors.plannedTask.message} -
    - )} - - {isHelpVisible && ( -
    - {/* Add your help content here */} -

    - Enter the target value for today's task. + ); + }) + ) : ( +

    +

    + No employees found for the selected role.

    )}
    + )} +
    +
    +
    + {watch("selectedEmployees")?.length > 0 && ( +
    +
    + {watch("selectedEmployees")?.map((empId) => { + const emp = employees.find((emp) => emp.id === empId); + return ( + emp && ( + + {emp.firstName} {emp.lastName} +

    { + const updatedSelected = watch( + "selectedEmployees" + ).filter((id) => id !== empId); + setValue( + "selectedEmployees", + updatedSelected + ); + trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation on removing badge + }} + > + +

    +
    + ) + ); + })} +
    +
    + )} +
    + + {!loading && errors.selectedEmployees && ( +
    +

    {errors.selectedEmployees.message}

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