added loading behaviour for submitting form

This commit is contained in:
Pramod Mahajan 2025-06-20 12:37:21 +05:30
parent 0fad5a0276
commit 0dc79f8dfa

View File

@ -21,19 +21,18 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
const schema = z.object({ const schema = z.object({
selectedEmployees: z selectedEmployees: z
.array(z.string()) .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" }), description: z.string().min(1, { message: "Description is required" }),
plannedTask: z.preprocess( plannedTask: z.preprocess(
(val) => parseInt(val, 10), (val) => parseInt(val, 10),
z z
.number({ .number({
required_error: "Planned task is required", required_error: "Planned task is required",
invalid_type_error: "Target for Today must be a number", invalid_type_error: "Target for Today must be a number",
}) })
.int() .int()
.positive({ message: "Planned task must be a positive number" }) .positive({ message: "Planned task must be a positive number" })
.max(maxPlanned, { .max(maxPlanned, {
message: `Planned task cannot exceed ${maxPlanned}`, message: `Planned task cannot exceed ${maxPlanned}`,
}) })
), ),
@ -42,6 +41,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
const [isHelpVisibleTarget, setIsHelpVisibleTarget] = useState(false); const [isHelpVisibleTarget, setIsHelpVisibleTarget] = useState(false);
const helpPopupRefTarget = useRef(null); const helpPopupRefTarget = useRef(null);
const [isHelpVisible, setIsHelpVisible] = useState(false); const [isHelpVisible, setIsHelpVisible] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
// Refs for Bootstrap Popovers // Refs for Bootstrap Popovers
const infoRef = useRef(null); const infoRef = useRef(null);
@ -151,7 +151,7 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
// Form submission handler // Form submission handler
const onSubmit = async (data) => { const onSubmit = async (data) => {
const selectedEmployeeIds = data.selectedEmployees; const selectedEmployeeIds = data.selectedEmployees;
setIsSubmitting(true);
// Prepare taskTeam data (only IDs are needed for the backend based on previous context) // Prepare taskTeam data (only IDs are needed for the backend based on previous context)
const taskTeamWithDetails = selectedEmployeeIds const taskTeamWithDetails = selectedEmployeeIds
.map((empId) => { .map((empId) => {
@ -169,11 +169,12 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
}; };
try { try {
// Call API to assign task await TasksRepository.assignTask(formattedData);
// Close the modal setIsSubmitting(false);
closedModel();
} catch (error) { } catch (error) {
console.error("Error assigning task:", error); setIsSubmitting(false);
showToast("Something went wrong. Please try again.", "error"); showToast("Something went wrong. Please try again.", "error");
} }
}; };
@ -183,393 +184,387 @@ const AssignTask = ({ assignData, onClose, setAssigned }) => {
onClose(); onClose();
}; };
return ( return (
<div className="fs-5 text-dark text-center d-flex align-items-center justify-content-center flex-wrap">
<div className="fs-5 text-dark text-center d-flex align-items-center justify-content-center flex-wrap"> <p className="align-items-center flex-wrap m-0 ">Assign Task</p>
<p className="align-items-center flex-wrap m-0 ">Assign Task</p> <div className="container my-3">
<div className="container my-3"> <div className="mb-1">
<div className="mb-1"> <p className="mb-0">
<p className="mb-0"> <span className="text-dark text-start d-flex align-items-center flex-wrap form-text">
<span className="text-dark text-start d-flex align-items-center flex-wrap form-text"> <span className="me-2 m-0 font-bold">Work Location :</span>
<span className="me-2 m-0 font-bold">Work Location :</span> {[
{[ assignData?.building?.name,
assignData?.building?.name, assignData?.floor?.floorName,
assignData?.floor?.floorName, assignData?.workArea?.areaName,
assignData?.workArea?.areaName, assignData?.workItem?.workItem?.activityMaster?.activityName,
assignData?.workItem?.workItem?.activityMaster ]
?.activityName, .filter(Boolean) // Filter out any undefined/null values
] .map((item, index, array) => (
.filter(Boolean) // Filter out any undefined/null values <span key={index} className="d-flex align-items-center">
.map((item, index, array) => ( {item}
<span key={index} className="d-flex align-items-center"> {index < array.length - 1 && (
{item} <i className="bx bx-chevron-right mx-2"></i>
{index < array.length - 1 && ( )}
<i className="bx bx-chevron-right mx-2"></i>
)}
</span>
))}
</span> </span>
</p> ))}
</span>
</p>
<form onSubmit={handleSubmit(onSubmit)}> <form onSubmit={handleSubmit(onSubmit)}>
<div className="form-label text-start"> <div className="form-label text-start">
<div className="row mb-1"> <div className="row mb-1">
<div className="col-12"> <div className="col-12">
<div className="form-text text-start"> <div className="form-text text-start">
<div className="d-flex align-items-center form-text fs-7"> <div className="d-flex align-items-center form-text fs-7">
<span className="text-dark">Select Team</span> <span className="text-dark">Select Team</span>
<div className="me-2">{displayedSelection}</div> <div className="me-2">{displayedSelection}</div>
<a <a
className="dropdown-toggle hide-arrow cursor-pointer" className="dropdown-toggle hide-arrow cursor-pointer"
data-bs-toggle="dropdown" data-bs-toggle="dropdown"
aria-expanded="false" aria-expanded="false"
> >
<i className="bx bx-filter bx-lg text-primary"></i> <i className="bx bx-filter bx-lg text-primary"></i>
</a> </a>
<ul className="dropdown-menu p-2 text-capitalize"> <ul className="dropdown-menu p-2 text-capitalize">
<li key="all"> <li key="all">
<button <button
type="button" type="button"
className="dropdown-item py-1" className="dropdown-item py-1"
onClick={() => onClick={() =>
handleRoleChange({ handleRoleChange({
target: { value: "all" }, target: { value: "all" },
})
}
>
All Roles
</button>
</li>
{jobRoleData?.map((user) => (
<li key={user.id}>
<button
type="button"
className="dropdown-item py-1"
value={user.id}
onClick={handleRoleChange}
>
{user.name}
</button>
</li>
))}
</ul>
</div>
</div>
</div>
</div>
<div className="row">
<div className="col-12 h-sm-25 overflow-auto mt-2">
{selectedRole !== "" && (
<div className="row">
{loading ? ( // Assuming 'loading' here refers to master data loading
<div className="col-12">
<p className="text-center">Loading roles...</p>
</div>
) : filteredEmployees?.length > 0 ? (
filteredEmployees?.map((emp) => {
const jobRole = jobRoleData?.find(
(role) => role?.id === emp?.jobRoleId
);
return (
<div
key={emp.id}
className="col-6 col-md-4 col-lg-3 mb-3"
>
<div className="form-check d-flex align-items-start">
<Controller
name="selectedEmployees"
control={control}
render={({ field }) => (
<input
{...field}
className="form-check-input me-1 mt-1"
type="checkbox"
id={`employee-${emp?.id}`}
value={emp.id}
checked={field.value?.includes(
emp.id
)}
onChange={(e) => {
handleCheckboxChange(e, emp);
}}
/>
)}
/>
<div className="flex-grow-1">
<p
className="mb-0"
style={{ fontSize: "13px" }}
>
{emp.firstName} {emp.lastName}
</p>
<small
className="text-muted"
style={{ fontSize: "11px" }}
>
{loading ? (
<span className="placeholder-glow">
<span className="placeholder col-6"></span>
</span>
) : (
jobRole?.name || "Unknown Role"
)}
</small>
</div>
</div>
</div>
);
}) })
) : ( }
<div className="col-12"> >
<p className="text-center"> All Roles
No employees found for the selected role. </button>
</p> </li>
</div> {jobRoleData?.map((user) => (
)} <li key={user.id}>
</div> <button
)} type="button"
</div> className="dropdown-item py-1"
value={user.id}
onClick={handleRoleChange}
>
{user.name}
</button>
</li>
))}
</ul>
</div> </div>
</div>
</div>
</div>
<div <div className="row">
className="col-12 h-25 overflow-auto" <div className="col-12 h-sm-25 overflow-auto mt-2">
style={{ maxHeight: "200px" }} {selectedRole !== "" && (
> <div className="row">
{watch("selectedEmployees")?.length > 0 && ( {loading ? ( // Assuming 'loading' here refers to master data loading
<div className="mt-1"> <div className="col-12">
<div className="text-start px-2"> <p className="text-center">Loading roles...</p>
{watch("selectedEmployees")?.map((empId) => { </div>
const emp = employees.find( ) : filteredEmployees?.length > 0 ? (
(emp) => emp.id === empId filteredEmployees?.map((emp) => {
); const jobRole = jobRoleData?.find(
return ( (role) => role?.id === emp?.jobRoleId
emp && ( );
<span
key={empId} return (
className="badge rounded-pill bg-label-primary d-inline-flex align-items-center me-1 mb-1" <div
key={emp.id}
className="col-6 col-md-4 col-lg-3 mb-3"
>
<div className="form-check d-flex align-items-start">
<Controller
name="selectedEmployees"
control={control}
render={({ field }) => (
<input
{...field}
className="form-check-input me-1 mt-1"
type="checkbox"
id={`employee-${emp?.id}`}
value={emp.id}
checked={field.value?.includes(emp.id)}
onChange={(e) => {
handleCheckboxChange(e, emp);
}}
/>
)}
/>
<div className="flex-grow-1">
<p
className="mb-0"
style={{ fontSize: "13px" }}
> >
{emp.firstName} {emp.lastName} {emp.firstName} {emp.lastName}
<p </p>
type="button" <small
className=" btn-close-white p-0 m-0" className="text-muted"
aria-label="Close" style={{ fontSize: "11px" }}
onClick={() => {
const updatedSelected = watch(
"selectedEmployees"
).filter((id) => id !== empId);
setValue(
"selectedEmployees",
updatedSelected
);
trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation on removing badge
}}
>
<i className="icon-base bx bx-x icon-md "></i>
</p>
</span>
)
);
})}
</div>
</div>
)}
</div>
{!loading && errors.selectedEmployees && (
<div className="danger-text mt-1">
<p>{errors.selectedEmployees.message}</p>{" "}
{/* Use message from Zod schema */}
</div>
)}
{/* Pending Task of Activity section */}
<div className="col-md text-start mx-0 px-0">
<div className="form-check form-check-inline mt-3 px-1">
<label
className="form-text text-dark align-items-center d-flex"
htmlFor="inlineCheckbox1"
>
Pending Task of Activity :
<label
className="form-check-label fs-7 ms-4"
htmlFor="inlineCheckbox1"
>
<strong>
{assignData?.workItem?.workItem?.plannedWork -
assignData?.workItem?.workItem?.completedWork}
</strong>{" "}
<u>
{
assignData?.workItem?.workItem?.activityMaster
?.unitOfMeasurement
}
</u>
</label>
<div
style={{ display: "flex", alignItems: "center" }}
>
<div
ref={infoRef}
tabIndex="0"
className="d-flex align-items-center avatar-group justify-content-center ms-2"
data-bs-toggle="popover"
data-bs-trigger="focus"
data-bs-placement="right"
data-bs-html="true"
style={{ cursor: "pointer" }}
>
&nbsp;
<svg
xmlns="http://www.w3.org/2000/svg"
width="13"
height="13"
fill="currentColor"
className="bi bi-info-circle"
viewBox="0 0 16 16"
>
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" />
<path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.547 1.11l1.91-2.011c.241-.256.384-.592.287-.984-.172-.439-.58-.827-1.13-.967a.664.664 0 0 1-.58-.309l-.15-.241-.002-.002zM8 4c-.535 0-.943.372-.943.836 0 .464.408.836.943.836.535 0 .943-.372.943-.836 0-.464-.408-.836-.943-.836z" />
</svg>
</div>
</div>
</label>
</div>
</div>
{/* Target for Today input and validation */}
<div className="col-md text-start mx-0 px-0">
<div className="form-check form-check-inline mt-2 px-1 mb-2 text-start">
<label
className="text-dark text-start d-flex align-items-center flex-wrap form-text"
htmlFor="inlineCheckbox1"
>
<span>Target for Today</span>&nbsp;
<span style={{ marginLeft: "46px" }}>:</span>
</label>
</div>
<div
className="form-check form-check-inline col-sm-3 mt-2"
style={{ marginLeft: "-28px" }}
>
<Controller
name="plannedTask"
control={control}
render={({ field }) => (
<div className="d-flex align-items-center gap-1 ">
<input
type="text"
className="form-control form-control-sm"
{...field}
id="defaultFormControlInput"
aria-describedby="defaultFormControlHelp"
/>
<span style={{ paddingLeft: "6px" }}>
{
assignData?.workItem?.workItem?.activityMaster
?.unitOfMeasurement
}
</span>
<div
style={{
display: "flex",
alignItems: "center",
}}
>
<div
ref={infoRef1}
tabIndex="0"
className="d-flex align-items-center avatar-group justify-content-center ms-2"
data-bs-toggle="popover"
data-bs-trigger="focus"
data-bs-placement="right"
data-bs-html="true"
style={{ cursor: "pointer" }}
>
&nbsp;
<svg
xmlns="http://www.w3.org/2000/svg"
width="13"
height="13"
fill="currentColor"
className="bi bi-info-circle"
viewBox="0 0 16 16"
> >
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" /> {loading ? (
<path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.547 1.11l1.91-2.011c.241-.256.384-.592.287-.984-.172-.439-.58-.827-1.13-.967a.664.664 0 0 1-.58-.309l-.15-.241-.002-.002zM8 4c-.535 0-.943.372-.943.836 0 .464.408.836.943.836.535 0 .943-.372.943-.836 0-.464-.408-.836-.943-.836z" /> <span className="placeholder-glow">
</svg> <span className="placeholder col-6"></span>
</span>
) : (
jobRole?.name || "Unknown Role"
)}
</small>
</div> </div>
</div> </div>
</div> </div>
)} );
/> })
</div> ) : (
<div className="col-12">
{errors.plannedTask && ( <p className="text-center">
<div className="danger-text mt-1"> No employees found for the selected role.
{errors.plannedTask.message}
</div>
)}
{isHelpVisible && (
<div
className="position-absolute bg-white border p-2 rounded shadow"
style={{ zIndex: 10, marginLeft: "10px" }}
>
{/* Add your help content here */}
<p className="mb-0">
Enter the target value for today's task.
</p> </p>
</div> </div>
)} )}
</div> </div>
)}
</div>
</div>
<div
className="col-12 h-25 overflow-auto"
style={{ maxHeight: "200px" }}
>
{watch("selectedEmployees")?.length > 0 && (
<div className="mt-1">
<div className="text-start px-2">
{watch("selectedEmployees")?.map((empId) => {
const emp = employees.find((emp) => emp.id === empId);
return (
emp && (
<span
key={empId}
className="badge rounded-pill bg-label-primary d-inline-flex align-items-center me-1 mb-1"
>
{emp.firstName} {emp.lastName}
<p
type="button"
className=" btn-close-white p-0 m-0"
aria-label="Close"
onClick={() => {
const updatedSelected = watch(
"selectedEmployees"
).filter((id) => id !== empId);
setValue(
"selectedEmployees",
updatedSelected
);
trigger("selectedEmployees"); // <--- IMPORTANT: Trigger validation on removing badge
}}
>
<i className="icon-base bx bx-x icon-md "></i>
</p>
</span>
)
);
})}
</div>
</div>
)}
</div>
{!loading && errors.selectedEmployees && (
<div className="danger-text mt-1">
<p>{errors.selectedEmployees.message}</p>{" "}
{/* Use message from Zod schema */}
</div>
)}
{/* Pending Task of Activity section */}
<div className="col-md text-start mx-0 px-0">
<div className="form-check form-check-inline mt-3 px-1">
<label
className="form-text text-dark align-items-center d-flex"
htmlFor="inlineCheckbox1"
>
Pending Task of Activity :
<label <label
className="form-text fs-7 m-1 text-lg text-dark" className="form-check-label fs-7 ms-4"
htmlFor="descriptionTextarea" // Changed htmlFor for better accessibility htmlFor="inlineCheckbox1"
> >
Description <strong>
{assignData?.workItem?.workItem?.plannedWork -
assignData?.workItem?.workItem?.completedWork}
</strong>{" "}
<u>
{
assignData?.workItem?.workItem?.activityMaster
?.unitOfMeasurement
}
</u>
</label> </label>
<Controller <div style={{ display: "flex", alignItems: "center" }}>
name="description" <div
control={control} ref={infoRef}
render={({ field }) => ( tabIndex="0"
<textarea className="d-flex align-items-center avatar-group justify-content-center ms-2"
data-bs-toggle="popover"
data-bs-trigger="focus"
data-bs-placement="right"
data-bs-html="true"
style={{ cursor: "pointer" }}
>
&nbsp;
<svg
xmlns="http://www.w3.org/2000/svg"
width="13"
height="13"
fill="currentColor"
className="bi bi-info-circle"
viewBox="0 0 16 16"
>
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" />
<path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.547 1.11l1.91-2.011c.241-.256.384-.592.287-.984-.172-.439-.58-.827-1.13-.967a.664.664 0 0 1-.58-.309l-.15-.241-.002-.002zM8 4c-.535 0-.943.372-.943.836 0 .464.408.836.943.836.535 0 .943-.372.943-.836 0-.464-.408-.836-.943-.836z" />
</svg>
</div>
</div>
</label>
</div>
</div>
{/* Target for Today input and validation */}
<div className="col-md text-start mx-0 px-0">
<div className="form-check form-check-inline mt-2 px-1 mb-2 text-start">
<label
className="text-dark text-start d-flex align-items-center flex-wrap form-text"
htmlFor="inlineCheckbox1"
>
<span>Target for Today</span>&nbsp;
<span style={{ marginLeft: "46px" }}>:</span>
</label>
</div>
<div
className="form-check form-check-inline col-sm-3 mt-2"
style={{ marginLeft: "-28px" }}
>
<Controller
name="plannedTask"
control={control}
render={({ field }) => (
<div className="d-flex align-items-center gap-1 ">
<input
type="text"
className="form-control form-control-sm"
{...field} {...field}
className="form-control" id="defaultFormControlInput"
id="descriptionTextarea" // Changed id for better accessibility aria-describedby="defaultFormControlHelp"
rows="2"
/> />
)} <span style={{ paddingLeft: "6px" }}>
/> {
{errors.description && ( assignData?.workItem?.workItem?.activityMaster
<div className="danger-text"> ?.unitOfMeasurement
{errors.description.message} }
</span>
<div
style={{
display: "flex",
alignItems: "center",
}}
>
<div
ref={infoRef1}
tabIndex="0"
className="d-flex align-items-center avatar-group justify-content-center ms-2"
data-bs-toggle="popover"
data-bs-trigger="focus"
data-bs-placement="right"
data-bs-html="true"
style={{ cursor: "pointer" }}
>
&nbsp;
<svg
xmlns="http://www.w3.org/2000/svg"
width="13"
height="13"
fill="currentColor"
className="bi bi-info-circle"
viewBox="0 0 16 16"
>
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" />
<path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.547 1.11l1.91-2.011c.241-.256.384-.592.287-.984-.172-.439-.58-.827-1.13-.967a.664.664 0 0 1-.58-.309l-.15-.241-.002-.002zM8 4c-.535 0-.943.372-.943.836 0 .464.408.836.943.836.535 0 .943-.372.943-.836 0-.464-.408-.836-.943-.836z" />
</svg>
</div>
</div>
</div> </div>
)} )}
</div> />
</div>
{/* Submit and Cancel buttons */} {errors.plannedTask && (
<div className="col-12 d-flex justify-content-center align-items-center gap-sm-6 gap-8 text-center mt-1"> <div className="danger-text mt-1">
<button type="submit" className="btn btn-sm btn-primary "> {errors.plannedTask.message}
Submit
</button>
<button
type="reset"
className="btn btn-sm btn-label-secondary"
data-bs-dismiss="modal"
aria-label="Close"
onClick={closedModel}
>
Cancel
</button>
</div> </div>
</form> )}
{isHelpVisible && (
<div
className="position-absolute bg-white border p-2 rounded shadow"
style={{ zIndex: 10, marginLeft: "10px" }}
>
{/* Add your help content here */}
<p className="mb-0">
Enter the target value for today's task.
</p>
</div>
)}
</div> </div>
<label
className="form-text fs-7 m-1 text-lg text-dark"
htmlFor="descriptionTextarea" // Changed htmlFor for better accessibility
>
Description
</label>
<Controller
name="description"
control={control}
render={({ field }) => (
<textarea
{...field}
className="form-control"
id="descriptionTextarea" // Changed id for better accessibility
rows="2"
/>
)}
/>
{errors.description && (
<div className="danger-text">{errors.description.message}</div>
)}
</div> </div>
</div>
{/* Submit and Cancel buttons */}
<div className="col-12 d-flex justify-content-center align-items-center gap-sm-6 gap-8 text-center mt-1">
<button
type="submit"
className="btn btn-sm btn-primary "
disabled={(isSubmitting, loading)}
>
{isSubmitting ? "Please Wait" : "Submit"}
</button>
<button
type="reset"
className="btn btn-sm btn-label-secondary"
data-bs-dismiss="modal"
aria-label="Close"
onClick={closedModel}
disabled={isSubmitting || loading}
>
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
); );
}; };